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 = {16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};

cout << a << 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.

## Recommended Answers

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 = {16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};

cout << a << endl; //output is 3``````

How are the dimensions …

Now I'm confused all over again.

How would I break up this data:

``int a = {1, 2, 3, ... 24}``

would it be right to say that it would look like this?

[(1,2)(3,4)(5,6)...(7,8)] [(9,10)(11,12)...(13,14)(15,16)] [(17,18)...(19,20)(21,22)(23,24)]

No. With a 3 dimensional array, the last 2 indices (the middle one …

## All 6 Replies

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 = {16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};

cout << a << 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:

Memory 1 = 16, mem2 = 15, mem3 = 14, mem4 = 13, etc,..

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:

(16,15) (14,13) (12,11) (10,9) ...................... (8,7) (6,5) (4,3) (2,1)

Now looking at your output statement, a, 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;

You can experiment with this:

``````#include<iostream>
using std::cin; using std::cout; using std::endl;

int main()
{
int a = {16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};

int ladiesORmen = 0;
int whichPair = 0;
int leftORright = 0;

cout << "\nEnter 0 for ladies shoes or else 1 for men: ";
cin >> ladiesORmen;
cout << "\nEnter 0 - 3 to select pair: ";
cin >> whichPair;
cout << "\nEnter 0 for left shoe or 1 for right shoe: ";
cin >> leftORright;

cout << "\n\n\n         LADIES SHOES                          MEN SHOES" << endl;
cout << "\n(16,15) (14,13) (12,11) (10,9)         (8,7) (6,5) (4,3) (2,1)" << endl << endl;
cout << "\nYour selection: " << "a[" << ladiesORmen << "][" << whichPair << \
"][" << leftORright <<"] = " << a[ladiesORmen][whichPair][leftORright] << endl << endl << endl<< endl;

return 0;
}``````

Wow, thank you for clarifying that. I was trying to draw the numbers in a 3 dimensional grid and it was just confusing me. Thanks again.

Now I'm confused all over again.

How would I break up this data:

``int a = {1, 2, 3, ... 24}``

would it be right to say that it would look like this?

[(1,2)(3,4)(5,6)...(7,8)] [(9,10)(11,12)...(13,14)(15,16)] [(17,18)...(19,20)(21,22)(23,24)]

Now I'm confused all over again.

How would I break up this data:

``int a = {1, 2, 3, ... 24}``

would it be right to say that it would look like this?

[(1,2)(3,4)(5,6)...(7,8)] [(9,10)(11,12)...(13,14)(15,16)] [(17,18)...(19,20)(21,22)(23,24)]

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 = five 4 X 2 grids stacked one above the other.

And this array, a, would look like this:

(1,2)(3,4)(5,6)(7,8)...........(9,10)(11,12)(13,14)(15,16)...........(17,18)(19,20)(21,22)(23,24)

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.

commented: Thanks for clarifying! +1

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.