user take input in 2d array dynamic 2d

i want to take the input in 2d array but how

1
2
3
4
5
6
7
 int n1,n2;
 int *arr;
cout<<"\nEnter The Row : ";
cin>>n1;
cout<<"\nEnter The Col : ;
cin>>n2;
arr=new int[n1][n2]; 


error [n2] must be const but how to take input in it

plz help
closed account (oy0Gy60M)
One way you can create 2d array using your approach (but it is still an 1d array) :
1
2
3
4
5
6
7
int n1, n2;
int *arr;
cout<<"\nEnter The Row : ";
cin>>n1;
cout<<"\nEnter The Col : ;
cin>>n2;
arr = new int[n1 * n2]; 


If you want to create an 2d array, you need a two-dimensional pointer. So you can do this :
1
2
3
4
5
6
7
8
9
10
int n1, n2;
int **arr;
cout<<"\nEnter The Row : ";
cin >> n1;
cout<<"\nEnter The Col : ";
cin >> n2;

int i;
arr = new int*[n1];
for(i = 0; i < n1; i++) arr[i] = new int[n2];

Last edited on
thnx
Topic archived. No new replies allowed.