Hey guys, learning about multi-dimensional arrays and am tracing a code that my teacher gave us, but I'm failing to see or conceptualize it.

for instance: Why does the following code output 3?

int a[2][4][2] = {16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
cout << a[1][2][1] << endl; //output is 3

How are the dimensions created? I am trying to draw it on paper, but it becomes convoluted and I get confused. Could someone offer clarify this array for me? Thanks for your guys' help.

Hey guys, learning about multi-dimensional arrays and am tracing a code that my teacher gave us, but I'm failing to see or conceptualize it.

for instance: Why does the following code output 3?

int a[2][4][2] = {16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
cout << a[1][2][1] << endl; //output is 3

How are the dimensions created? I am trying to draw it on paper, but it becomes convoluted and I get confused. Could someone offer clarify this array for me? Thanks for your guys' help.

OK, first of all, keep in mind that, regardless of how many dimensions that you have, they are all just contiguous values in memory, since that is the way RAM memory is in a computer. The data for your array is placed in memory contiguously, just as you have it in your array initialization:

Now when you look at your array, you have to view the right most index as the main "unit", and in this case, it is a pair of something, let's say a pair of shoes (0 = left shoe, 1 = right shoe). Now move to the left one index and we have 4, which means we have 4 pairs of shoes. Now, moving to the left again with our indexes, we have 2, which means we have 2 sets of 4 pairs of shoes (0 = 4 pair ladies shoes and 1 = 4 pair of men shoes). So we have 4 pairs men shoes and 4 pairs of ladies shoes. OK, now this is how you would view the data which is contiguous in memory:

Now looking at your output statement, a[1][2][1], the left most index selects either the ladies shoes (left group of 4) or the men shoes (right group of 4). The middle index selects which pair of shoes (i.e,. one of the pairs in parenthesis) and the right most index selects either the left or right shoe.

Keep in mind that the indices start at zero, so 1 is really two places over, and 2 is really 3 places over. For example, to get 12 for an output, you would write: cout << a[0][2][0];

No. With a 3 dimensional array, the last 2 indices (the middle one and the right-most one) form a 2D array. In fact, for any number of dimensions the last 2 indices on the right form a 2D array, and then the third index from the right gives the third dimension, and the forth index from the right gives the forth dimension, etc. So on a 3D grid, the last 2 indices on the right form the flat 2D grid, and the left-most index number would add more identical 2D grids stacked vertically. For example:

a[5][4][2] = five 4 X 2 grids stacked one above the other.

Do you see how the two dimensional 4 X 2 array is repeated 3 times to make 3 dimensions? The left most index selects one of the three groups, while the middle index selects one of the four pairs, and the right most index selects one of the numbers within the parenthesis. Also, if you were to run through all of the numbers in the array, the right most index would change the fastest, and the left most index would change the slowest - just like a three digit odometer does.

Ok I get it now, so I can just continue to break down the information from left to right index until I'm done. Thank you again for your help I appreciate it so much.

In this assignment you are to write a program that finds a path through a “maze”. The maze will be represented by a 2-dimensional array of ints. You will be ...