Reputation: 2248
I am trying to create an grid based array in c++, when I create the grid via console, it comes out fine but when I try to implement it inside an array, it messes up.
int i,j;
for(i=0;i<100;i++) {
for(j=0;j<100;j++) {
cout << j << " ";
};
cout << endl;
};
When I used to create and cout array grid, it doesnt work the same way:
int i,j, img[100][100];
for(i=0;i<100;i++) {
for(j=0;j<100;j++) {
cout << img[i][j] << " ";
};
cout << endl;
};
What am I doing wrong exactly?
This should be the correct output:
//99 rows of the following code
0 1 2 3 4 5 6 7 8 9 10 11 12 ... 99
0 1 2 3 4 5 6 7 8 9 10 11 12 ... 99
0 1 2 3 4 5 6 7 8 9 10 11 12 ... 99
...
x99
NOTE: this consoles out fine and writes fine to file, but when I try to reproduce this using an array it gets messed up. mostly shows 0's. I need to incorporate this into an array because I need to modify specific parts of the grid.
Upvotes: 1
Views: 3164
Reputation: 7910
You're printing out values in an uninitialized array. The value of img[i][j]
is not j
by default - you shouldn't assume anything about its default state. If you do this to initialize the array then you should get your expected output:
int i,j, img[100][100];
for(i=0;i<100;i++) {
for(j=0;j<100;j++) {
img[i][j]=j;
}
}
//then print it out.
Upvotes: 3