943,571 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 587
  • C++ RSS
Aug 24th, 2009
0

Passing a User Define Multidimensional Array

Expand Post »
hey...i'm having trouble passing a user define 2d array...
C++ Syntax (Toggle Plain Text)
  1. void genrand(int &game[][],int &size)
  2. {
  3. int i,j;
  4. for(i=0;i<size;i++)
  5. {
  6. for(j=0;size;j++)
  7. {
  8. game[i][j]=(rand()%100);
  9. }
  10. }
  11. }
  12.  
  13. int main()
  14. {
  15. int size;
  16. int game[][];
  17. cout<<"Please enter your Game Size";
  18. cin>>size;
  19.  
  20. genrand(game[][],size);
  21.  
  22. return 0;
  23. system("pause");
  24. }

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...
Reputation Points: 10
Solved Threads: 0
Light Poster
gopi17 is offline Offline
39 posts
since Sep 2008
Aug 24th, 2009
1

Re: Passing a User Define Multidimensional Array

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:
C++ Syntax (Toggle Plain Text)
  1. int **game ;
  2. int size = 0;
  3. cin >> size;
  4. game = new int* [size];
  5. for( int i = 0; i < size; i++ )
  6. {
  7. game[i] = new int [size];
  8. }
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.
Last edited by vmanes; Aug 24th, 2009 at 3:01 am. Reason: added footnote
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Aug 24th, 2009
0

Re: Passing a User Define Multidimensional Array

Here is incorrect syntax. You must have to specify the bounds for all dimensions.
C++ Syntax (Toggle Plain Text)
  1. int game[][];
Correct way is,
C++ Syntax (Toggle Plain Text)
  1. 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,
C++ Syntax (Toggle Plain Text)
  1. 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].
Last edited by adatapost; Aug 24th, 2009 at 3:05 am.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 24th, 2009
0

Re: Passing a User Define Multidimensional Array

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
C++ Syntax (Toggle Plain Text)
  1. game[500][500];
  2. then minimize from there...
Reputation Points: 10
Solved Threads: 0
Light Poster
gopi17 is offline Offline
39 posts
since Sep 2008
Aug 24th, 2009
0

Re: Passing a User Define Multidimensional Array

Click to Expand / Collapse  Quote originally posted by gopi17 ...
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
C++ Syntax (Toggle Plain Text)
  1. game[500][500];
  2. 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!
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Aug 24th, 2009
0

Re: Passing a User Define Multidimensional Array

hmmm....ic...thanks for the info....
do you mind explaining the code...i don't quite get it...
C++ Syntax (Toggle Plain Text)
  1. int **game ; <---------This line
  2. int size = 0;
  3. cin >> size;
  4. game = new int* [size]; <---------This line
  5. for( int i = 0; i < size; i++ )
  6. {
  7. game[i] = new int [size]; <---------This line
  8. }
Reputation Points: 10
Solved Threads: 0
Light Poster
gopi17 is offline Offline
39 posts
since Sep 2008
Aug 24th, 2009
0

Re: Passing a User Define Multidimensional Array

I could try, but you should probably read this first. I'll the code a lot clearer to you!
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Aug 24th, 2009
0

Re: Passing a User Define Multidimensional Array

Click to Expand / Collapse  Quote originally posted by gopi17 ...
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
C++ Syntax (Toggle Plain Text)
  1. game[500][500];
  2. 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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Starting graphics
Next Thread in C++ Forum Timeline: How to create makefile that compiles .c file as .cpp file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC