I have a 5x5 matrix of integers, and I want to compare a line at a time to see if they are the same or different. I've got an if/else statement within a for loop. The for loop checks each "y" coordinate of the two-dimensional array I've set up. The if statement says "if they're the same, then cout!"
The way it's set up now, the cout statement is shown 5 times. Is there a way I can alter my if statement so that it is
"if (array[0][x] == array[3][x] for all values of x)"
I've tried to do this by making the parenthetical part of the if statement its own for loop, but it doesn't seem to work. Any ideas?
[code=c++]
...
for (x=0; x<5; x++)
{
if (array[0][x] == array[3][x])
{
cout << "Arrays 0 and 3 are the same!" << endl;
}
else
{
cout << "Nope." << endl;
}
}
[/code]
To compare a serie of elements to eachother, I normally use the following construction:
1 2 3 4 5 6 7 8 9 10 11 12
int value[SIZE]; //array of elements; whey're going to check whether they're all the same or not
bool check=true; //creatte bool and set it to true
//go through all elements and compare them with the first:
for (int i=1;i<SIZE;i++)
if (value[0] != value[i]) //if they're not the same
{
check=false; //set our test-variable to false
break; //no need to check the other elements
}
if (check) //if all elements are the same
{} //do whatever need to be done