| | |
Passing a User Define Multidimensional Array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 23
Reputation:
Solved Threads: 0
hey...i'm having trouble passing a user define 2d array...
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...
C++ Syntax (Toggle Plain Text)
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:
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.
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)
int **game ; int size = 0; cin >> size; game = new int* [size]; for( int i = 0; i < size; i++ ) { game[i] = new int [size]; }
* - 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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Here is incorrect syntax. You must have to specify the bounds for all dimensions.
Correct way is,
Or use pointer to pointer (dynamic memory allocation). http://www.cplusplus.com/doc/tutorial/dynamic/
Another problem is with calling a function,
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].
C++ Syntax (Toggle Plain Text)
int game[][];
C++ Syntax (Toggle Plain Text)
int game[4][4];
Another problem is with calling a function,
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Sep 2008
Posts: 23
Reputation:
Solved Threads: 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
for example
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
game[500][500]; then minimize from there...
Just use the code that vmanes already posted.
Don't forget to
delete[] the memory when you're done with it! •
•
Join Date: Sep 2008
Posts: 23
Reputation:
Solved Threads: 0
hmmm....ic...thanks for the info....
do you mind explaining the code...i don't quite get it...
do you mind explaining the code...i don't quite get it...
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
game[500][500]; then minimize from there...
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
![]() |
Similar Threads
- Student Average With Multidimensional Array (C++)
- Help Needed ASAP to read csv & populate a String Multidimensional Array (Java)
- typedef or a multidimensional array? (C)
- Question regarding multidimensional array for navigation bar (Graphics and Multimedia)
- Sort multidimensional array on more than one column? (ASP)
- multidimensional array merge using PHP (PHP)
- How to Sort a MultiDimensional Array (C)
- multidimensional array (C)
Other Threads in the C++ Forum
- Previous Thread: Starting graphics
- Next Thread: How to create makefile that compiles .c file as .cpp file
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






