| | |
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
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter 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






