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.