# Array Question

Printable View

• 06-07-2010
wtaplin
Array Question
Hey guys, came across this in a book tonight. I think I understand it but I'd like to be sure.

Code:

```BOOL g_bTileStates[4][4]; int g_iTiles[4][4]; for(int i =  0; i < 4; i++)     for(int j = 0; j < 4 j++) {       g_bTileStates[i][j] = FALSE;       g_iTiles[i][j] = 0; }```
the two for loops I get, they index through each separate element of the array. The part that threw me is the use of the = operator. What effect does
g_iTiles[i][j] = 0; have on the array? It doesn't seem to be setting the values of i and j. Does this mean that an array can hold a third piece of information other than it's elements?
• 06-07-2010
Programmer_P
Quote:

Originally Posted by wtaplin
Hey guys, came across this in a book tonight. I think I understand it but I'd like to be sure.

Code:

```BOOL g_bTileStates[4][4]; int g_iTiles[4][4]; for(int i =  0; i < 4; i++)     for(int j = 0; j < 4 j++) {       g_bTileStates[i][j] = FALSE;       g_iTiles[i][j] = 0; }```
the two for loops I get, they index through each separate element of the array. The part that threw me is the use of the = operator. What effect does
g_iTiles[i][j] = 0; have on the array?
It doesn't seem to be setting the values of i and j. Does this mean that an array can hold a third piece of information other than it's elements?

Its setting the current element of the two-dimensional array to 0. The inner loop will loop 4 times for each iteration of the outer one, which also loops 4 times. That means the inner loop is going to loop 16 times in total, but it will only access elements of the array 0 through 3 for each iteration of the outer loop. The memory locations stored at each one of those elements in memory are what's being assigned the values of FALSE and 0.

Think of the 2-dimensional array as a checkerboard in memory:

There are 4 rows of that checkerboard (i.e. those elements accessed by 'i'), and 4 columns (i.e. those elements accessed by 'j').
So you have a 4 x 4 array. Now how many squares of that checkerboard does that give you? And which ones are "checkers" being placed on first? The rows or the columns?
• 06-07-2010
wtaplin
Ahhh ok, I was making it more complicated in my head for some reason XD So, at the end of the loops, each element of g_iTiles will be set to 0. g_bTiles[0][0] == 0; g_bTiles[0][1] == 0; and so forth. I think I thought about it too hard and screwed myself up. Thank you! =D