I am trying to make a 2D array where user inputs the number of elements that array can take, also the elements inside the array. I think I manage to create the array, but when I try to put some elements inside it, for example 2×2 array and putting 2 as all of its elements i get this as the output. Here is the code:
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int rowCount,colCount;
cout<<"Enter the number of rows in Grid-Land:";
cin>>rowCount;
cout<<"Enter the number of columns in Grid-Land:";
cin>>colCount;
int** arr = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
arr[i] = new int[colCount];
cout<<"Enter the garbage amounts at the nodes of the MxN Grid-Land:"<<endl; //Elements of the array
for(int i=0; i<rowCount; i++){
for (int j=0; i<colCount; i++)
cin>>arr[i][j];
}
cout<<"\nThe 2-D Array is:\n";
for(int i=0;i<rowCount;i++){
for(int j=0;j<colCount;j++){
cout<<"\t"<<arr[i][j];
}
cout<<endl;
}
return 0;
}
>Solution :
It’s a typo. Instead of using the "j" variable in the inner loop while taking the input, you have used the "i" variable. 🙂
Happy coding.