hey...i'm having trouble passing a user define 2d array...

void genrand(int &game[][],int &size)
{
     int i,j;
     for(i=0;i<size;i++)
     {
       for(j=0;size;j++)
        {
          game[i][j]=(rand()%100);
          }
     }
}
 
int main()
{
    int size;
    int game[][];
    cout<<"Please enter your Game Size";
    cin>>size;

    genrand(game[][],size);
    
    return 0;
    system("pause");
}

This is my code...i read in another c++ forum where they stated that i need to specify the array...i'm not sure either...

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.

Comments
Beautiful post :)

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].

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...

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!

hmmm....ic...thanks for the info....
do you mind explaining the code...i don't quite get it...

int **game ;                        <---------This line
   int size = 0;
   cin >> size;
   game = new int* [size];        <---------This line
   for( int i = 0; i < size; i++ )
   {
         game[i] = new int  [size];               <---------This line
    }

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.

This article has been dead for over six months. Start a new discussion instead.