0

I want to use 2 size multidimentional array with the size that user would input . How can i do that ?

I take two number s from edits components , then I need that number s to be the size of that array (using borland c++)

26th Oct 2016, 7:07 PM
JohnPond
4 Respuestas
+ 1
You can take 2 numbers (m, n) from user and make array of arrays. Every array inside will be of size n and external array will be of size m. Example in C#: int[][] yourArray = new int[m][]; for (int i = 0; i < m; i++) { yourArray[i] = new int[n]; } Or a little bit easier: int[,] yourArray = new int[m][n];
26th Oct 2016, 7:36 PM
Oleg Tretiakov
Oleg Tretiakov - avatar
+ 1
int main () { int m,n; cin>>m; cin>>n; int a[m][n]; for (int i = 0; i < m; i++) { for(int j=0; j<n; j++) { cin>>a[i][j]; } } for ( int i = 0; i < m; i++ ) for ( int j = 0; j < n; j++ ) { cout << "a[" << i << "][" << j << "]: "; cout << a[i][j]<< endl; } return 0; }
26th Oct 2016, 8:01 PM
Kaushik Pankhaniya
Kaushik Pankhaniya - avatar
0
Don't use arrays, use std::vector instead. vector<vector<int>> is 2 dimensional. Make sure you sanitize your user input before handling it over to vector / to new(): What if the user inputs "12a"? What if the user inputs a negative or really big number?
26th Oct 2016, 8:03 PM
Ullrich Franke
Ullrich Franke - avatar
0
int main () { int m,n; cin>>m; cin>>n; int a[m][n]; for (int i = 0; i < m; i++) { for(int j=0; j<n; j++) { cin>>a[i][j]; }
27th Oct 2016, 9:01 AM
Gaurab Bhattacharya
Gaurab Bhattacharya - avatar