我必须做这个练习:
Write a C + + function that, given an array with two NxN size of integers, returns true if the rows to have all the same elements (even in different locations), and false otherwise.
这是我的代码:
#include <iostream>
using namespace std;
const int N = 5;
bool fool ( const int a [N][N])
{
int x,y = 0;
int j=0;
int i=0;
bool check[N] = {false};
bool verify = false;
for (i=0; i<N; i++)
{
check[i] = false;
}
while(j<N && !verify )
{
if(a[x][i]==a[y][j] && !check[j] )
{
check[j]=true;
verify=true;
}
j++;
}
return verify;
}
int main ()
{
const int a[N][N] = {{1,3,5,6,7},
{5,6,7,1,3},
{1,6,5,3,7},
{6,1,3,5,7},
{6,5,1,7,3}};
if ( fool(a))
{
cout << " in all rows there are the same elements";
}
else
{
cout << " wrong, . ";
}
return 0;
}
但是程序崩溃了。我该如何解决?
最佳答案
while( int j<N && !verify )
不要在循环内部声明j
,它应该在外部声明。我很惊讶这甚至可以编译(它不能在 Visual Studio C++ 中编译)。
关于c++ - 数组的行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10350253/