944,089 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12108
  • C++ RSS
Nov 1st, 2006
0

Initializing Multidimensional Arrays plz help!

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. // Listing 15.3 - Creating A Multidimensional Array
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. int SomeArray[5][2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}};
  7. for (int i = 0; i<5; i++)
  8. for (int j=0; j<2; j++)
  9. {
  10. std::cout << "SomeArray[" << i << "][" << j << "]: ";
  11. std::cout << SomeArray[i][j]<< std::endl;
  12. }
  13. return 0;
  14. }


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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimbobint is offline Offline
6 posts
since Oct 2006
Nov 1st, 2006
0

Re: Initializing Multidimensional Arrays plz help!

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 !
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Nov 1st, 2006
0

Re: Initializing Multidimensional Arrays plz help!

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

C++ Syntax (Toggle Plain Text)
  1. 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
Last edited by Nick Evan; Mar 26th, 2010 at 9:39 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 1st, 2006
0

Re: Initializing Multidimensional Arrays plz help!

Click to Expand / Collapse  Quote originally posted by niek_e ...
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 :

Quote ...
0 0 0
0 0 0
0 0 1
and not

Quote ...
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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 1st, 2006
0

Re: Initializing Multidimensional Arrays plz help!

C++ Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
may4life is offline Offline
57 posts
since Oct 2006
Nov 1st, 2006
0

Re: Initializing Multidimensional Arrays plz help!

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimbobint is offline Offline
6 posts
since Oct 2006
Nov 2nd, 2006
0

Re: Initializing Multidimensional Arrays plz help!

Quote ...
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....
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 2nd, 2006
0

Re: Initializing Multidimensional Arrays plz help!

Click to Expand / Collapse  Quote originally posted by niek_e ...
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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with binary search tree insertion
Next Thread in C++ Forum Timeline: need help for saving high scores





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC