user defined multidimensional array

black hole sun

New member
Hi guys. I'm writng a simple program for my C++ class to print a magic square. To do this I need a mutlidimensional array, but I can't let the user type in the array dimensions without the compiler giving me crap about it. It will only let me use literal numbers as the "column" value in my multidimensional array, like so:

void fillMagicSquare(int [][3]);

It disallows, for example, this;

void fillMagicSquare(int [][num]); // error
...
cout << "Give me the dimension:";
cin >> num;
fillMagicSquare(ar[num][num]);

How can I work around this? I guess I could settle for making the array a predefined size but that's kinda lame.
 
Last edited:
the prototype does not accept details about an array argument. your prototype will look like this:

Code:
void foo( int [][], int [][] );

the head of your implementation will look like this:

Code:
void foo( int ray1 [][], int ray2[][] )
{
   //code here
}

and then the call would look like this:

Code:
foo( ray1, ray2 );
 
built-in arrays and matrices cannot have variable indices
if you want variable-length arrays and matrices, write your own class ;)
or use the stl vector<vector<int> or vector<valarray<int>>


black hole sun said:
cout << "Give me the dimension:";
cin >> num;
fillMagicSquare(ar[num][num]);
I'm surprised that didn't fail, or at least error. what a bad compiler
 
Last edited:
seeker that did fail, I was just trying to show what I was trying to do. Since I won't be learning vectors for a few months that's not an option. Guess I'll just have to wait. It's okay I guess. :)

Zenitram: gcc won't let me declare a multidimensional array without specifying the column length (the second subscript)

error: multidimensional arrays must have bounds for all dimensions except the first
 
Last edited:
black hole sun said:
print a magic square

void fillMagicSquare(int [][3]);

It disallows, for example, this;

void fillMagicSquare(int [][num]); // error
...
cout << "Give me the dimension:";
cin >> num;
fillMagicSquare(ar[num][num]);

How can I work around this? I guess I could settle for making the array a predefined size but that's kinda lame.

Where do you live???? I looked at the URL and thats to the website of one of the instructors at the school that I work at... Are you in the face to face class or the online class?
 
Hemet is a 15 drive from me. I live in Menifee, and yeah, I take Glenn's level 1 C++ class face-to-face.
 
Last edited:
black hole sun said:
seeker that did fail, I was just trying to show what I was trying to do. Since I won't be learning vectors for a few months that's not an option. Guess I'll just have to wait. It's okay I guess. :)

Zenitram: gcc won't let me declare a multidimensional array without specifying the column length (the second subscript)

error: multidimensional arrays must have bounds for all dimensions except the first

bah! sensitive ol gcc. Accompanying the error should be a line number. May I see the line that is throwing the compiler error?
 
black hole sun said:
Hemet is a 15 drive from me. I live in Menifee, and yeah, I take Glenn's level 1 C++ class face-to-face.

thats funny, the monday wednesday morning class huh... you've probably seen me and I've probably seen you.... I'm one of the guys that has probably popped in once or twice for one reason or another.... long dyed black hair, mustache and goatee, my office looks right into your class.. wasn't there yesterday though was at meetings at the other campus....

Anyways I think this assignment is allready over but, me trying not to write all your code for you..... your function should just take 2 ints 1 if it's always going to be a square

void foo(int x, int y)
{
int** your_array = new array*[x];

// for loop to create the new y portion.. be something like your_array[for_counter] = new int[y]

// code to populate the multidementional array and do whatever else

//for loop to delete the contents of the array ( no lost memory )

delete [] your_array; // free up the memory used by the variable
}

probably not something that you would be likely to come to your own conclution in the intro level class
 
Yeah, I don't know what you did there. Glenn doesn't cover new and delete until the second semester. I already finished that lab, but I left the dimensions hard coded. Thanks anyway though.

I think I've seen you outside taking a smoke from time to time (might be someone else, but from your description thats who comes to mind). It's funny seeing someone from rage on campus. Small world.
 
yeah I figured that would lab would be over with, and didn't really figure you would have been to that point but I knew that pointers would have been covered...... figured new and delete would not be covered till the second class when classes are covered.

Yeah thats would probably be me.... Thats what I said... a rage member at my school??? wow..... this planet just gets smaller all the time
 
Back
Top