| | |
Multi-dim arrays
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
can you do something like this but with a 2-dim array?
C++ Syntax (Toggle Plain Text)
int main() { int size; int *pointer; cout<<"Enter size\n"; cin>>size; pointer=new int[size]; for(int i=0;i<size;i++) { pointer[i]=i; } for(int i=0;i<size;i++) { cout<<pointer[i]<<endl; } return 0; }
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
You have to be a little more careful, but this is how you can do it.
C++ Syntax (Toggle Plain Text)
int main() { int rows, cols; int **pointer; cout<<"Enter rows and columns for your 2-D array\n"; cin >> rows >> cols; // allocate a row of pointers. pointer = new int*[rows]; // for each row allocate the columns for(int i = 0; i < rows; i++){ pointer[i] = new int[cols]; } // add values to the 2-D array for(int i=0;i < rows;i++){ for(int j = 0; j < cols; j++){ pointer[i][j]=(i + 1) * (j + 1); } } // print out the values for(int i=0;i<rows;i++){ for(int j = 0; j < cols; j++){ cout << pointer[i][j] << '\t'; } cout << endl; } //free all the columns for each row for(int i = 0; i < rows; i++){ delete pointer[i]; } // free the rows delete pointer; return 0; }
or you could do something like ...
then go on adding elements to it ...
C++ Syntax (Toggle Plain Text)
int* arr = new int[rows * cols];
then go on adding elements to it ...
Last edited by Agni; Oct 10th, 2008 at 1:03 am.
thanks
-chandra
-chandra
![]() |
Similar Threads
- Chilisoft ASP script for my church - I need help (ASP)
- Password Generator (Visual Basic 4 / 5 / 6)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Referencing a single dimension of a multi-dimensional array (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: While Loop
- Next Thread: Not understanding Class Scope
Views: 525 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding stream string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






