1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

multidimensions within multidimensional arrays..??

Discussion in 'C' started by Jatsui, Oct 16, 2008.

  1. Jatsui

    Jatsui New Member

    Joined:
    Feb 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    London, UK
    Hi there experts,

    Probably a bit of a stupid question...
    Was wondering if it is possible to create a multidimensional array with sub multidimensional arrays.
    So for eg: the array would look something like the one below:

    array01[array02[ax1]][array03[ax2[b2]][ax3[b3]][ax4[b4]]]

    array01 - 2D array
    array02 - 1D array within array01
    array03 - 3D array within array01
    ax1/2/3 - all 1D arrays within array03

    Thanks, appreciate it.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yes, but you'd need to use structures/classes; you can't do it with the simple array syntax. Array dimensions are simple numbers; you can't have an array of dimensions.

    However your "spec" doesn't seem to make a lot of sense. You seem to be trying to define array01 as 2D by 3D, that's a bizarre object which is 2D down one side and 3D down the other. If you mean array01 is a 2D array that contains 1D arrays and 3D arrays then you'll need to use polymorphism (array01 would be a 2D array of "base", from which array02 and array03 would be derived).

    So if array01 is a 2D array of array02's and array03's, and array03 is a 3D array of 1D arrays, and array02 is a 1D array of 1D arrays, which is one possible way to make sense of your post, then you might get something like this:
    Code:
    // array01 contains both array02s and array03s so we need a base to derive them from
    class array23base
    {
    };
    
    // array03 contains AX2s, AX3s and AX4s so we need a base to derive them from
    class AX234base
    {
    };
    
    // The AXn's (2-4):
    class AX2 : public AX234base
    {
      int num[10];
    };
    class AX3 : public AX234base
    {
      int num[10];
    };
    class AX4 : public AX234base
    {
      int num[10];
    };
    
    // AX1 is "simple"
    class AX1
    {
      int num[10];
    };
    
    // array02 is an array of AX1s:
    class Array02 : public array23base
    {
      AX1 ax1[10];
    };
    
    // array03 is a 3D array containing AX2s, AX3s and AX4s:
    class Array03 : public array23base
    {
      AX234base *ax234s[10][10][10];
    };
    
    // Finally Array01 is a 2D array of array02s and array03s:
    array23base *Array01[10][10];
    
    Then at runtime you would need to initialise all those pointers.
     
    Last edited: Oct 16, 2008
  3. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    First, a quick re-write of your "spec". (I'm assuming this is what you meant.)

    a1[ a2[ b1 ] ] [ a3[ ax1[ b2 ] ] [ ax2[ b3 ] ] [ ax3[ b4 ] ] ]

    a1 - 2D array
    a2 - 1D array within a1
    a3 - 3D array within a1
    ax1/2/3 - 1D arrays within a3
    b1/2/3/4 - indices

    Exactly what you have will work just fine, except that it is not correct to say that a2 and a3 are "within" a1. They are simply being used as indices for a1.

    If that is not what you mean, then I agree with xpi0t0s that your "spec" makes very little sense. If a1 is 2D, it needs two indices. Where are they if they aren't a2 and a3? The same for a3: if ax1/2/3 aren't its indices, then where are its indices?

    If you really want the "sub-arrays" to be "within" other arrays, xpi0t0s has demonstrated the polymorphic version. Another version is something like this (with 5 as an arbitrary size):
    Code:
    struct {
      int a2[5];
      struct {
        int ax1[5];
        int ax2[5];
        int ax3[5];
      } a3[5][5][5];
    } a1[5][5];
    
    // Access is like this:
    
    a1[2][4].a2[1] = 1;
    a1[2][4].a3[3][2][0].ax1[1] = 2;
     
  4. Jatsui

    Jatsui New Member

    Joined:
    Feb 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    London, UK
    Hi, firstly thanks for all the comments.

    Sorry for the vague representation, but below is what i meant:

    a1[ a2[ b1 ] ] [ a3[ ax1[ b2 ] ] [ ax2[ b3 ] ] [ ax3[ b4 ] ] ]


    So oogabooga - your first representation is right, thanks.
     

Share This Page