I'm really having trouble understanding how to initialize multidimensional arrays.
The way it is explained in the book I'm reading is:
You assign the list of values to array elements in order, with the last array subscript changing and each of the former ones holding steady.

Therefore, if u have an array
int theArray [5] [3], the first three elements go into theArray [0], the next three into the array [1] and so forth. I'm finding this concept rather hard to understand. I had an example in another book where an int array is initialised like this:

int array [4] [3] = { 1, 2, 3, 4,5, 6, 7, 8, 9, 10, 11, 12 };
and it results in the following assignments:

array [0] [0] is equal to 1
array [0] [1] is equal to 2
array [0] [2] is equal to 3
array [1] [0] is equal to 4
array [1] [1] is equal to 5
array [1] [2] is equal to 6
array [2] [0] is equal to 7
array [2] [1] is equal to 8
array [2] [2] is equal to 9
array [3] [0] is equal to 10
array [3] [1] is equal to 11
array [3] [2] is equal to 12

Now i worked this out not by the explanation above because I couldn't understand it but by a pattern i noticed emerge in that in terms of elements the first subscript goes to 4- i.e 0,1,2,3
and the second subscript goes to 3- i.e 0,1,2.

The patern i noticed is the first subscript
counts to 3 i.e 0,0,0 and then changes to 1,1,1 counting to 3 and changes again etc and the second subscript
counts from 0-2 and then starts over again. I still don't understand why or how that pattern is happening tho etc.

The next exercise in the book then gets even more confusing where a nested for loop is used to create a multidimentional array,
that is also initialised in doubles:

// Listing 15.3 - Creating A Multidimensional Array
#include <iostream>
 
int main()
{
int SomeArray[5][2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}};
for (int i = 0; i<5; i++)
for (int j=0; j<2; j++)
{
std::cout << "SomeArray[" << i << "][" << j << "]: ";
std::cout << SomeArray[i][j]<< std::endl;
}
return 0;
}

SomeArray [0] [0]: 0
SomeArray [0] [1]: 0
SomeArray [1] [0]: 1
SomeArray [1] [1]: 2
SomeArray [2] [0]: 2
SomeArray [2] [1]: 4
SomeArray [3] [0]: 3
SomeArray [3] [1]: 6
SomeArray [4] [0]: 4
SomeArray [4] [1]: 8

If anyone can explain this concept to me to try and help me understand it or give me good webpages that explain it
it would be much appreciated as it's confusing the heck out of me. Many thnx jimbobint.

try this MDA
you want my advise read every c++ program in ya book and you will understand it coz ya cant hit it from the 1st step... its not that easy !

I agree that this isn't the easiest subject for a beginning programmer, but I'll try to explain:

int Array[3][3]

this means you're setting up a 2d array, an array with 2 dimensions. so like this:

array[3][3] (means)

000
000
000

so you can just use 'coordinates' to set and get data! For example if you say: array[2][2] = 1 ; You array will now look like this:

000
010
000

Now for your 2nd example, you say you have problems with nested loops, but all that that piece of code is doing, is running through the array:
every time that the outer loops increases, the inner loop will count from 0 to 2. So the outer loop is for the 1st element and the inner loop is for the 2nd element of the array.

Hope you get the idea

Edited 6 Years Ago by Nick Evan: n/a

I agree that this isn't the easiest subject for a beginning programmer, but I'll try to explain:

so you can just use 'coordinates' to set and get data! For example if you say: array[2][2] = 1 ; You array will now look like this:

000
010
000

Oh so is it like that, too bad I didnt know about it.

From my understanding dont you think array[2][2] should be :

0 0 0
0 0 0
0 0 1

and not

0 0 0
0 1 0
0 0 0

;)

Indexing of array starts at zero since they are internally treated as pointers.

int array [3] -- array is single dimensional array, containing 3 elemnts
which are indexed as array [0], array[1] and array[2]

int SomeArray[5][2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}}

This creates a 2-dimensional array. All together this array has 10 elements. Which is the first index of this array? SomeArray[0][0]. The initialization states that this is equal to 0 (the first 0 in the {0,0}).
Which is the 7th index of this array? SomeArray[3][1] and the value is 3 because we are counting in twos. This array has 5 twos. If you understand this you'll soon get your head around it. Try it this way, which is the most straight forward answer you'll prob get:
Element# Index number Value
1 SomeArray[0][0] 0
2 SomeArray[0][1] 0
3 SomeArray[1][0] 1
4 SomeArray[1][1] 2
5 SomeArray[2][0] 2
6 SomeArray[2][1] 4
7 SomeArray[3][0] 3
8 SomeArray[3][1] 6
9 SomeArray[4][0] 4
10 SomeArray[4][1] 8

Thx for ur help everyone, I think it's a little clearer I'm just going to read over it till hopefully something clicks lol. The last post was particularly helpfull after reading it I managed to work out the assignments just by looking at :

int SomeArray [5] [2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8} } so think thats a good sign have just got to try and work out how to apply it to the loop now. thnx a lot for all ur help jimbobint.

Oh so is it like that, too bad I didnt know about it.

Oh my god I'must be losing it. Your rigth offcourse, maybe I should stop drinking or something.... :sad:

Oh my god I'must be losing it. Your rigth offcourse, maybe I should stop drinking or something....

Heh happens buddy, it frequently happens. :cheesy:

The main thing is you admitted it which most people dont.

This article has been dead for over six months. Start a new discussion instead.