multidimentional array

B

Bangalore

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

Thanks, in advance
Bangalore
 
R

Robbie Hatley

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
 
U

upashu2

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).
 
G

Gernot Frisch

im Newsbeitrag
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.
 
A

Axter

Bangalore said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top