C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

multidimentional array

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Bangalore
Guest





PostPosted: Tue Sep 27, 2005 6:22 am    Post subject: multidimentional array Reply with quote



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

Thanks, in advance
Bangalore

Back to top
Robbie Hatley
Guest





PostPosted: Tue Sep 27, 2005 11:07 am    Post subject: Re: multidimentional array Reply with quote



"Bangalore" <siddesh_049 (AT) rediffmail-dot-com (DOT) no-spam.invalid> wrote
in message news:4338e514$0$45264$892e7fe2 (AT) authen (DOT) white.readfreenews.net...

Quote:
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



Back to top
upashu2
Guest





PostPosted: Tue Sep 27, 2005 11:14 am    Post subject: Re: multidimentional array Reply with quote



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

Back to top
E. Robert Tisdale
Guest





PostPosted: Tue Sep 27, 2005 4:20 pm    Post subject: Re: multidimentional array Reply with quote

Bangalore wrote:

Quote:
Please clarify for me,
the implementation of two or three dimensional array
using overloaded operator[].

Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/

Also, take a look at

The Object-Oriented Numerics Page

http://www.oonumerics.org/oon/

Back to top
Gernot Frisch
Guest





PostPosted: Wed Sep 28, 2005 7:32 am    Post subject: Re: multidimentional array Reply with quote


"Bangalore" <siddesh_049 (AT) rediffmail-dot-com (DOT) no-spam.invalid> schrieb
im Newsbeitrag
news:4338e514$0$45264$892e7fe2 (AT) authen (DOT) white.readfreenews.net...
Quote:
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 >
{
};

and then handle the allocation of each row somehow. Bad idea. Use the
() operator.



Back to top
Marcus Kwok
Guest





PostPosted: Wed Oct 05, 2005 6:42 pm    Post subject: Re: multidimentional array Reply with quote

Bangalore <siddesh_049 (AT) rediffmail-dot-com (DOT) no-spam.invalid> wrote:
Quote:
Hi all,
Plz clarify me, on the implementation of two or three dimensional
array using overloaded [ ] operator.

As others have mentioned, you should probably be overloading
"operator()". Have a look at the FAQ (13.10 - 13.12):

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10

--
Marcus Kwok

Back to top
Axter
Guest





PostPosted: Thu Oct 06, 2005 5:09 am    Post subject: Re: multidimentional array Reply with quote

Bangalore wrote:
Quote:
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 My2D_Array;
vector<vector > 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.


Back to top
Marcus Kwok
Guest





PostPosted: Thu Oct 06, 2005 2:13 pm    Post subject: Re: multidimentional array Reply with quote

Bangalore <siddesh_049 (AT) rediffmail-dot-com (DOT) no-spam.invalid>
wrote:
Quote:
Hi all,
Plz clarify me, on the implementation of two or three dimensional
array using overloaded [ ] operator.

As others have mentioned, you should probably be overloading

"operator()". Have a look at the FAQ (13.10 - 13.12):

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10

--
Marcus Kwok


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.