Thread: Multidimensional arrays

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Hallo, I am trying to understand how multidimensional arrays work but can't seem to figure it out. I would like help in understanding them. Yeah googled already but what I have seen so far seem to say "a multidimensional is an array of an array" which doesn't help much in understanding. I have a code here and its output. I would like someone to explain it to me the outcome well. Thanks in advance.

    Code:
    #include <iostream>
    using namespace std;
    intmain()
    {
    intar[]={123,456,789};
    intind[]={2,1,2,0};
    intk;
    for (k=0;k<4;k=k+1) {
    cout<< ar[ind[k]];
    }
    return 0;
    }
    Output: 789456789123

    OK I think I figure it out.


    OK the loop produces the values 0, 1, 2 and 3. Putting this in the array the statement becomes:

    cout<< ar[ind[0]]; and so on till array 3.

    From here then it is easier. Because ind[0] = 2, so it becomes array[2]. Although I understand this but intar[] has only 3 values while ind has 4, so when intar runs out of values to loop over, what is supposed to happen? Does it go back to zero and start it all over again or?
    Last edited by Dontgiveup; 03-25-2011 at 01:24 AM.

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    First, these are not multidimensional arrays but only single arrays.
    when intar runs out of values to loop over, what is supposed to happen?
    Definiterly, crash..... Boooooooooommmmmmmm!!!
    No it will not go back to zero, a runtime error.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Erm, nothing of the sort.
    It doesn't matter that ind has more indices than array. The simple reason is that ind stores the indices in array that you're going to access. So ind can be of infinite length and it will all be well so long as the contents of ind is 0 < i < length of array - 1.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Thank you Elysia and Mr.777. I now understood it. So you basically output the values in ar[0],ar[1],ar[2],and finally again ar[0]. Ooh and I thought it was a multidimensional array.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Dontgiveup View Post
    Thank you Elysia and Mr.777. I now understood it. So you basically output the values in ar[0],ar[1],ar[2],and finally again ar[0]. Ooh and I thought it was a multidimensional array.
    It actually outputs ar[2], ar[1], ar[2], ar[0] as you can see from the result.

    You can always take it step by step and figure it out.
    1st loop: k=0
    ar[ind[k]] = ar[ind[0]]
    you know that ind[0] = 2
    ar[ind[0]] = ar[2]
    you know that ar[2] = 789 thus
    ar[ind[k]] = 789
    which is what you get

    2nd loop: k=1
    etc etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM