Passing a User Define Multidimensional Array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 23
Reputation: gopi17 is an unknown quantity at this point 
Solved Threads: 0
gopi17 gopi17 is offline Offline
Newbie Poster

Passing a User Define Multidimensional Array

 
0
  #1
Aug 24th, 2009
hey...i'm having trouble passing a user define 2d array...
  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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Passing a User Define Multidimensional Array

 
1
  #2
Aug 24th, 2009
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:
  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
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,616
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 469
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Passing a User Define Multidimensional Array

 
0
  #3
Aug 24th, 2009
Here is incorrect syntax. You must have to specify the bounds for all dimensions.
  1. int game[][];
Correct way is,
  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,
  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.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 23
Reputation: gopi17 is an unknown quantity at this point 
Solved Threads: 0
gopi17 gopi17 is offline Offline
Newbie Poster

Re: Passing a User Define Multidimensional Array

 
0
  #4
Aug 24th, 2009
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
  1. game[500][500];
  2. then minimize from there...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,859
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Passing a User Define Multidimensional Array

 
0
  #5
Aug 24th, 2009
Originally Posted by gopi17 View Post
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
  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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 23
Reputation: gopi17 is an unknown quantity at this point 
Solved Threads: 0
gopi17 gopi17 is offline Offline
Newbie Poster

Re: Passing a User Define Multidimensional Array

 
0
  #6
Aug 24th, 2009
hmmm....ic...thanks for the info....
do you mind explaining the code...i don't quite get it...
  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,859
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Passing a User Define Multidimensional Array

 
0
  #7
Aug 24th, 2009
I could try, but you should probably read this first. I'll the code a lot clearer to you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,205
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 149
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Passing a User Define Multidimensional Array

 
0
  #8
Aug 24th, 2009
Originally Posted by gopi17 View Post
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
  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.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC