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++)
4 Réponses
+ 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];
+ 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;
}
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?
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];
}