在我的程序即时通讯尝试添加我的2D数组的一个子部分的值,从9乘9矩阵的3乘3的小方块。我选择这个小盒子的行和列的百分比除以3(模数3)。 (这意味着它需要像[0] [0],[0] [3]等等) 这样的单元格,我希望这些单元格是框的右上角,然后再添加2行和列以使其适用于如果我们从[0] [0]开始,我们将添加[0-2] [0-2](3乘3箱)。我通过函数来计算(作为使用函数的练习)。问题在于程序似乎只从该小框中获取第一个单元格的值,并且当我尝试在该小框的其余部分上循环并添加它们的值时,它不会正确地(或在所有) 我想知道如果我的参数是错误的,或者即时通讯给功能错误的参数。 任何帮助,将不胜感激。传入一个函数的一部分我的矩阵(二维数组)
//------------including section-----------
#include <iostream>
#include <cstdlib>
//------------using section---------------
using std::cin;
using std::cout;
using std::endl;
//-----our constants and variables---------
const int N=3; //initializing our rows and cols as constants
int counter=0, arr[N*N][N*N];
int sumofrow=0, sumofcol=0,sumsquare=0;
//-------prototypes----------------
void READ_MATRIX(int arr[][N*N]);
bool issquare(int arr[][N*N],int row, int col);
//-------main-------------
int main()
{
//calling on the function to input our matrix
READ_MATRIX(arr);
//checking what functions returned
if(counter==0)
cout<<1;
else
cout <<0;
return EXIT_SUCCESS;
}
//-----functions--------
//----readmatrix------
void READ_MATRIX(int arr[][N*N])
{
for (int row=0; row<N*N; row++)
for (int col=0; col<N*N; col++) {
cin >> arr[row][col];
if (row%3==0&&col%3==0)
issquare(arr, row, col);
}
}
//---------issquare-------------
bool issquare(int arr[][N*N],int row, int col)
{
sumsquare=0;
for (int r=0;r<3;r++) //trying to loop on values of array
for (int c=0;c<3;c++)//trying to loop {
//r+row(because row is passed into the function at 0,3,6)
//same for col.
sumsquare+=arr[r+row][c+col]; // this is where it goes wrong
}
//checking to see if sum reached a certain value..
if (sumsquare==45)
return true;
else {
counter++;
return false;
}
}
这是伟大的!我意识到它只是输出该小盒子中的第一个数字,但我会做出这些改变!谢谢老兄,非常感谢。 –
我不认为在相同函数内将读数与属性测量('issquare')结合是一个好主意(除非这些属性对于读取更多数据很重要,这在这里不适用)。因此,应该避免这种概念混乱(阅读和测量)。 – Walter