I'm having a problem making a multidimensional array. I want the size of the array to correspond to some variables. I'm having trouble making this work.
Edit: I've tried using vectors.
This is a discussion on Array problem within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem making a multidimensional array. I want the size of the array to correspond to some variables. ...
I'm having a problem making a multidimensional array. I want the size of the array to correspond to some variables. I'm having trouble making this work.
Edit: I've tried using vectors.
Last edited by mrme44; 08-03-2010 at 06:05 PM.
Your problem is poorly stated. Ask a better question. Show examples of what you are attempting.
Mac and Windows cross platform programmer. Ruby lover.
Quote of the Day
12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.
Amen brother!
This is what I'm trying to do
It gives me the errorCode:int room_width = 100; int room_height = 100; double grid[room_width][room_height];
expression must have a constant value
I want to use variables in the array but it won't let me. How would I get around this?
Last edited by mrme44; 08-04-2010 at 08:29 AM.
What compiler are you using, and what are the options you are specifying?
Mac and Windows cross platform programmer. Ruby lover.
Quote of the Day
12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.
Amen brother!
I'm using VS and I'm creating a dll
Does it have a C99 option?
Mac and Windows cross platform programmer. Ruby lover.
Quote of the Day
12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.
Amen brother!
What's a C99 option?
It's an option that allows you to do what you want to do. Google it.
Mac and Windows cross platform programmer. Ruby lover.
Quote of the Day
12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.
Amen brother!
This is the C++ board. Make the doubles const and ints. Arrays must be a fixed size.
And VS doesn't support C99.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
C99 refers to the C standard as released in 1999. That standard supports what you want - if you have a compiler that complies with that standard, which VS does not. Your dimensions need to be of integral type, not double though.
However, neither C++ nor C89 (the preceding C standard of 1989) do, unless the array dimensions are fixed at compile time (and are integers with a const attribute). If you want the dimensions to be determined at run time (eg based on user input) you will need to use dynamic memory allocation in C++ or C89.
Right 98% of the time, and don't care about the other 3%.
Or just std::vector. Or boost::multi_array.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Thanks for the help. I'll look into using dynamic memory allocation or boost::multi_array.
Last edited by mrme44; 08-04-2010 at 08:40 AM.
Which is the point. You don't have to do it yourself. It's easier, safer and faster.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Mrme,
We always had fun with this problem when I was working in the lab in college, because it is such a great example of the memory handling skills that so often hamper a great implementation. It is a little bit of a pain, and it does look like we're re-inventing the wheel. But the point of the exercise was to avoid complex class interfaces, and of course hone the memory "bookkeeping" skills. I threw this together using your specifications:
The two dimensional array, which you called grid, can be allocated and de-allocated as necessary. I must admit that it is a little stilted, but it is very readable and can be applied to aggregate data types too. The output of this code is:Code:#include<iostream> #include<iomanip> using namespace std; double **getMatrix( int width, int height ); void releaseMatrix( double **grid, int width, int height ); void printMatrix( double **grid, int width, int height ); int main(int argc, char *argv[]) { double **grid; int j; int room_width = 10; int room_height = 10; grid = getMatrix( room_width, room_height ); // perform operations on your grid... for( j = 0; j < room_width; j++ ) { grid[ j ][ j ] = j*2.0 + 1.0; } printMatrix( grid, room_width, room_height ); releaseMatrix( grid, room_width, room_height ); system("pause"); return 0; } double **getMatrix( int width, int height ) { double **allocate; int j; int k; allocate = new double*[width]; if( NULL == allocate ) { cerr << "Memory allocation problem...exiting." << endl; exit( 1 ); } for( k = 0; k < width; k++ ) { allocate[k] = new double[height]; if( NULL == allocate[k] ) { cerr << "Memory allocation problem...exiting." << endl; exit( 1 ); } for( j = 0; j < height; j++ ) { allocate[k][j] = 0.0; } } return allocate; } void releaseMatrix( double **grid, int width, int height ) { int k; if( NULL != grid ) { for( k = 0; k < width; k++ ) { delete [] grid[k]; } delete [] grid; } return; } void printMatrix( double **grid, int width, int height ) { int j; int k; for( j = 0; j < height; j++ ) { for( k = 0; k < width; k++ ) { cout << setw(4) << grid[k][j]; } cout << endl; } return; }
I think that the syntax I used accounts for the update to the standard (C99), where the delete operator required the braces...I think that arrays got an extra atom associated with them for the heap management, but I am only speculating. The text I am using is Victor Shtern's CORE C++: A Software Engineering Approach. It was printed in 2000, and has been accurate in every application I have identified. Is this what you had in mind, Mrme?Code:1 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0 0 0 0 15 0 0 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 19 Press any key to continue . . .
Best Regards,
New Ink -- Henry