Declaring the array as you're trying to do, you MUST declare the sizes of the row and column dimensions*.
To allow the user to enter the size at runtime, you'll have to use dynamically allocated array, that is, pointers.
Generally speaking, this is the pattern:
int **game ;
int size = 0;
cin >> size;
game = new int* [size];
for( int i = 0; i < size; i++ )
{
game[i] = new int [size];
}
This assumes you want a square matrix. If you have differing row and column dimensions, remember that the first allocation is the number of rows, you will set the column size inside the loop.
* - yes, some compilers are now allowing you to allocate array with variables, following the new C standard. It's not C++ standard, yet.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
Here is incorrect syntax. You must have to specify the bounds for all dimensions.
int game[][];
Correct way is,
int game[4][4];
Or use pointer to pointer (dynamic memory allocation). http://www.cplusplus.com/doc/tutorial/dynamic/
Another problem is with calling a function,
genrand(game,size); // do not add subscripts
Arrays are not passed using operator&, because the name of the array is the starting location in memory of the array.
i.e the name of array is alread a pointer. The name of an array, for example, foo, is equivalent to &foo[0].
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
okay, i understand...juz wanna ask...is it possible to create a huge array then minimize the array according to the users input...
for example
game[500][500];
then minimize from there...
Why on earth would you want to do that?? First you ask your OS to reserve an enormous amount of memory for your program, but then when the user has inputted the real dimensions, the OS can free the useless memory again. That sounds a bit in-efficient doesn't it?
Just use the code that vmanes already posted.
Don't forget to delete[] the memory when you're done with it!
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
I could try, but you should probably read this first. I'll the code a lot clearer to you!
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
okay, i understand...juz wanna ask...is it possible to create a huge array then minimize the array according to the users input...
for example
game[500][500];
then minimize from there...
I think what you are looking for is vector or this vector
They have the capability of resizing to whatever size you would like
them to be.
The function vector::push_back, add elements to the end of the
vector.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608