| | |
Initializing Multidimensional Arrays plz help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 6
Reputation:
Solved Threads: 0
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:
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.
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:
C++ Syntax (Toggle Plain Text)
// 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.
Last edited by jimbobint; Nov 1st, 2006 at 10:04 am.
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 !
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 !
DarkCoder+
I agree that this isn't the easiest subject for a beginning programmer, but I'll try to explain:
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:
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
gr Niek
C++ Syntax (Toggle Plain Text)
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
gr Niek
•
•
•
•
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
From my understanding dont you think array[2][2] should be :
•
•
•
•
0 0 0
0 0 0
0 0 1
•
•
•
•
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]
Last edited by ~s.o.s~; Nov 1st, 2006 at 12:53 pm.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Oct 2006
Posts: 57
Reputation:
Solved Threads: 2
C++ Syntax (Toggle Plain Text)
int SomeArray[5][2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}}
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
•
•
Join Date: Oct 2006
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Help with binary search tree insertion
- Next Thread: need help for saving high scores
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






