multidimentional array

Discussion in 'C++' started by Bangalore, Sep 27, 2005.

  1. Bangalore

    Bangalore Guest

    Hi all,
    Plz clarify me, on the implementation of two or three dimensional
    array using overloaded [ ] operator.

    Thanks, in advance
    Bangalore
    Bangalore, Sep 27, 2005
    #1
    1. Advertising

  2. "Bangalore" <-spam.invalid> wrote
    in message news:4338e514$0$45264$...

    > Hi all,
    > Plz clarify me, on the implementation of two or three dimensional
    > array using overloaded [ ] operator.
    >
    > Thanks, in advance
    > Bangalore


    Do you mean an actual array? Or an array-like Class?

    Re. multidimensional built-in arrays, I made copious notes on
    those a few years ago. For reference, I've included those notes
    at the end of this message.

    As for an array-like class, I think the best way to impliement
    that might be using an actual multidimensional built-in array,
    with added safety features (esp. bounds checking on indexes).
    This would actually be easier and cleaner than using vectors,
    I think.

    I don't know if I'd recommend using an overloaded [] operator,
    though. Trying to get MyArray[7][37][6] to work right would be
    challenging to impliment. (Would almost force you to write
    your array class recursively, so that a 3d MyArray is a 1d MyArray
    of 2d MyArrays, etc.) Sounds messy.

    The application operator() might work better for you. That way
    you'd use just ONE operator to access an element of your array,
    regardless of the number of dimensions:

    dValue = MyArray(13,7,5,3); // (example for 4d Array)

    Also consider templatizing you class for different types, since
    it's basically just a container. You might also pass the
    number of dimensions as a template parameter, and pass the
    dimensions as arguments to a constructor. Something like this:

    MyArray<double, 3> Fred (13, 7, 7); // 3d array of doubles, 13x7x7



    Postscript: What follows are some very lengthy detailed
    notes on mulitdimensionally arrays I made some years ago,
    when I was first learning C. Anyone who would be bored
    by this please stop reading here. You've been warned.



    Wednesday July 4, 2001:
    I've been thinking-about and experimenting-with arrays today. I've
    learned that the nomenclature:
    int array[5][7][11];
    means: "an array of 5 elements, each of which is an array of 7 elements,
    each of which is an array of 11 elements, each of which is an integer".

    That is pretty much backwards from how I had thought that arrays work!

    In memory, a [3][4] array is stored in a contiguous block, in this
    order:
    [0][0] [0][1] [0][2] [0][3]
    [1][0] [1][1] [1][2] [1][3]
    [2][0] [2][1] [2][2] [2][3]
    Another way to look at multi-dimensional arrays is that they are
    multi-level n-ary trees. A [l][m][n] array is a tree with one trunk,
    l limbs, m branches per limb, and n twigs per branch. So, element
    array[3][5][7] is (from left to right): limb 3, branch 5, twig 7;
    or (from right to left): twig 7 of branch 5 of limb 3 of the array.

    Behind the scenes, I believe element address for an array such as:

    int Array[Size1][Size2][Size3];

    is implimented by pointers, like so:

    Array[x][y][z] =
    *(&(Array[0][0][0]) + x*Size2*Size3 + y*Size3 + z);

    All sizes except for the first are needed to calculate the RAM address of each element.

    (
    And still deeper behind the scenes, at the assembly level, we need to know the size of
    the actual elements (this is, sizeof(int)), so that we can translate this C statement:

    IntPtr += 4; /* step forward over 4 "int" elements */

    Into the equivalent pseudo-machine-language statement:

    BytePtr += 16 /* step forward over 16 bytes */

    In other words,

    MachLangArray[x][y][z] =
    *
    (
    sizeof(int)
    *
    (
    &(Array[0][0][0])
    + x*Size2*Size3
    + y*Size3
    + z
    )
    );

    )

    This is why, when passing n-dimensional arrays to functions, all but the first size
    number must be passed:

    int MyArray[3][16][6][82];
    /* ... */
    QuidBlitz = GurzBlunkt(Fidgit, MyArray[][16][6][82]);

    The first number is not needed, because the RAM address of any one element is independent
    of the number of items in the top-level grouping. (This is because we don't need to pass
    over the end boundary of the top-level grouping, but we DO pass over end-boundaries
    for all the other grouping levels, so we need to know how long they all are in order to
    calculate the RAM addresses in bytes.)






    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    email: lonewolfintj at pacbell dot net
    web: home dot pacbell dot net slant earnur slant
    Robbie Hatley, Sep 27, 2005
    #2
    1. Advertising

  3. Bangalore

    upashu2 Guest

    if u mean [j] and [j][k], you can't overload it, you can only
    define for your custom class.
    p[j] means that it is defined something like this - int **p;
    and p[j] = *( *(p+i)+j)
    and memory is allocated like
    p = new int *[ i];
    for(int k=0; k<i;i++) *(p+i) = new int[j];

    Instead of overloading [ ] [ ] or [ ] [ ] [ ] , overload ' () '
    operator, and access the element as (i,j) or (i,j,k).
    upashu2, Sep 27, 2005
    #3
  4. E. Robert Tisdale, Sep 27, 2005
    #4
  5. "Bangalore" <-spam.invalid> schrieb
    im Newsbeitrag
    news:4338e514$0$45264$...
    > Hi all,
    > Plz clarify me, on the implementation of two or three dimensional
    > array using overloaded [ ] operator.


    If you really want this, you can make a class:

    class Mat3x3 : public std:vector<std::vector<std::vector<double> > >
    {
    };

    and then handle the allocation of each row somehow. Bad idea. Use the
    () operator.
    Gernot Frisch, Sep 28, 2005
    #5
  6. Bangalore

    Marcus Kwok Guest

    Marcus Kwok, Oct 5, 2005
    #6
  7. Bangalore

    Axter Guest

    Bangalore wrote:
    > Hi all,
    > Plz clarify me, on the implementation of two or three dimensional
    > array using overloaded [ ] operator.
    >
    > Thanks, in advance
    > Bangalore


    You can use a vector of vector, like the following:

    vector<vector<int> > My2D_Array;
    vector<vector<vector<int> > > My3D_Array;

    Or you can use a simple class like the following:
    http://code.axter.com/dynamic_2d_array.h

    The above class can be access via [][] index, and does not require a
    helper class.
    For more information, check out the following links:
    http://www.codeguru.com/forum/showthread.php?t=231046
    http://faqs.cfm/?fid=5575

    I recommend against using the operator() method that is suggested in
    the C++ FAQ.
    It's not consistent with normal array indexing.
    IMHO, using a method that gives consistent syntax is preferable, and
    easier to read.
    Axter, Oct 6, 2005
    #7
  8. Bangalore

    Marcus Kwok Guest

    Marcus Kwok, Oct 6, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. willem
    Replies:
    2
    Views:
    521
    cristian
    Feb 15, 2005
  2. Replies:
    1
    Views:
    392
    Victor Bazarov
    Jan 24, 2005
  3. vertigo
    Replies:
    1
    Views:
    241
    Mathias Panzenboeck
    Dec 10, 2006
  4. cedric
    Replies:
    2
    Views:
    316
    Michael Mair
    Dec 30, 2004
  5. Sébastien Cottalorda

    IPC::Shareable Problem with multidimentional hash

    Sébastien Cottalorda, Sep 9, 2005, in forum: Perl Misc
    Replies:
    8
    Views:
    210
    Sébastien Cottalorda
    Sep 13, 2005
Loading...

Share This Page