954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passing a User Define Multidimensional Array

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

gopi17
Junior Poster
100 posts since Sep 2008
Reputation Points: 18
Solved Threads: 3
 

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)
Moderator
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...
gopi17
Junior Poster
100 posts since Sep 2008
Reputation Points: 18
Solved Threads: 3
 

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
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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
    }
gopi17
Junior Poster
100 posts since Sep 2008
Reputation Points: 18
Solved Threads: 3
 

I could try, but you should probably read this first. I'll the code a lot clearer to you!

Nick Evan
Not a Llama
Moderator
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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You