#include <iostream>
using namespace std;
int main()
{
int row , col, c = 0;
cout<<"Enter the dimensions of the array\n";
cout<<"Enter the rows of the 2D array\n";
cin>>row;
cout<<"Enter the columns of the 2D array\n";
cin>>col;
int** arr = new int*[row];
for (int i = 0; i < row; i++) {
arr[i] = new int[col];
}
cout<<"Enter the elements of the 2D array\n. Press -2 to stop inserting elements\n";
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cin>>arr[i][j];
if(arr[i][j] == -2){
break;
}
c++;
break;
}
}
int arr1[row][col];
for(int i=0; i<row; i++){
for(int j=0; j<col; j++ ){
arr1[i][j] = arr[i][j];
}
delete [] arr[i];
}
cout<<"The array contains "<<c<<" elements\n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
for(int i=0;i<row;i++)
delete [] arr;
return 0;
}
Comments
Leave a comment