RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Multidimensional Array as a parameter

Joeku

Lord
Multidimensional Array as a parameter

Alright, so I have the following:
Code:
// Sort the Table.
void SortTable( int dims, int Table[][], int pos, int start, int end )
{
It's giving me a compile error...
declaration of `Table' as multidimensional array must have bounds for all dimensions except the first
Table should have a size of 100 for the first dimension, and "dims" for the second.

How should I write this?
 

Sep102

Page
You can't specify it like that if you need the second dimension to vary. Basically, C++ doesn't support multi-dimensional arrays. It supports arrays of arrays and not very well at that. Arrays are essentially just pointers, therefore it follows that arrays are just pointers to pointers. That is how you will need to pass it, as an int **.

Also, that error was referring to the fact that you don't actually specify the length of the rightmost dimensions of the array. What it wants you to do is specify the array like: int a[][10], with the rightmost dimensions explicit. Adding a third dimension gives something like: int a[][10][10]. If you don't know what the rightmost dimensions are, then you can't specify the parameter as an array.
 

Joeku

Lord
So it's completely impossible to have a multidimensional array with variable rightmost dimensions as a parameter?

:(
 

Zippy

Razor Creator
No, he gave you the answer:

Code:
void SortTable( int dims, int **Table, int pos, int start, int end )
Add one star for every dimension of the array.

The empty square bracket (int blah[]) notation is less widely used and means *almost* the exact same thing... its usually better to avoid it.
 

Nochte

Wanderer
On this same topic, do any of ya'll have any idea how to teach the concept of 4+D arrays? Hell, 3D arrays are hard enough to draw on a whiteboard...
 

daat99

Moderator
Staff member
Nochte;671278 said:
On this same topic, do any of ya'll have any idea how to teach the concept of 4+D arrays? Hell, 3D arrays are hard enough to draw on a whiteboard...

Well, I don't know of an easy way to draw it on a whiteboard but assuming the students already know what 1d array is I describe it as follows:
2 dimensional array is simply 1 dimensional array where each item in it isn't a normal item but another 1 dimensional array.
The same can be said that any N dimensional array is simply 1 dimensional array where each item in it is an N-1 dimensional array (recursion should bump in just about now to fill the rest).

If you really have to draw it I would draw 2 dimensional array (matrix) and mark each item with a letter (line) and number (row), then I would draw 2 dimensional arrays (matrices) and title them with the letter and number in the first matrix (I won't suggest doing it for more then 2x2x2x2).

Just tell them to imagine that each matrix is a zoom-in presentation of 1 block in the original matrix.

Personally I won't even try to have the students imagine a 4 dimensional array, it'll eat up all lesson time and leave them totally confused.


P.S.
Zippy;671255 said:
The empty square bracket (int blah[]) notation is less widely used and means *almost* the exact same thing... its usually better to avoid it.
Zippy can you please elaborate on the differences between the [] operator and the *operator in those cases?
 

Nochte

Wanderer
daat99;671288 said:

Doh' I feel like a dunce. I went away for about 5 minutes in the middle of typing that message and didn't proof read. I read your reply and was thinking "wtf is he explaining this?" Then i noticed my error.
"how to teach" Should have been "how hard it is to teach"
Sorry 'bout that, daat. Very good explanation, though!
 

daat99

Moderator
Staff member
Nochte;671301 said:
Doh' I feel like a dunce. I went away for about 5 minutes in the middle of typing that message and didn't proof read. I read your reply and was thinking "wtf is he explaining this?" Then i noticed my error.
"how to teach" Should have been "how hard it is to teach"
Sorry 'bout that, daat. Very good explanation, though!

It's ok :)
 

Joeku

Lord
Ugh I feel like a dufus for missing that last part of what Sep102 said, about the **'s.

Thanks for clarifying, Zippy. You rock hard :eek:
 
Top