Thread: Array Question

  1. #1
    Registered User wtaplin's Avatar
    Join Date
    Dec 2009
    Posts
    13

    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?

  2. #2
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by wtaplin View Post
    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?
    Last edited by Programmer_P; 06-07-2010 at 09:26 PM.

  3. #3
    Registered User wtaplin's Avatar
    Join Date
    Dec 2009
    Posts
    13
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM