I have to do this exercise :
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.
This is my code:
#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;
}
But the program crashes. How can I fix it?
Don’t declare
jinside the loop, it should be declared outside. I’m surprised this even compiles (it doesn’t compile in Visual Studio C++).