I have the following code
#include <bits/stdc++.h>
using namespace std;
int main() {
int r = 0, c = 0;
int best = 0;
cin >> r >> c;
int myGrid[r + 2][c + 2] = {};
for (int i = 1; i < r + 1; i++) {
for (int j = 1; j < c + 1; j++) {
cin >> myGrid[i][j];
}
}
bool stillIn = false;
int di[] = {-1,-1, -1, 0, 0, 1, 1, 1};
int dj[] = {-1,0, 1, -1, 1, -1, 0, 1};
for (int i = 1; i < r + 1; i++) {
for (int J = 1; J < c + 1; J++) {
stillIn = false;
for (int k = 0; k < 8; k++) {
// cout << myGrid[i][J] << " " << endl;
if (myGrid[i][J] == myGrid[di[k]][dj[k]]) {
stillIn = true;
}
}
if (stillIn == true) {
best = myGrid[i][J];
}
}
}
cout << best;
return 0;
}
If I run the code with the following input:
4 3
0 1 0
1 2 0
1 5 1
2 3 4
It prints 4. However, if I uncomment line 28, which is
// cout << myGrid[i][J] << " " << endl;
Then it gives me 1, which is the correct answer. Why is this happening!? How does using cout change the final answer?
Thanks in advance for any help.