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 2x2 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;
}
CodePudding user response:
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.
CodePudding user response:
You got a typo in the for loop in which you prompt the user for the values of the array. You switched j's for i's so you're really only iterating over one column and never prompting the user for the rest of the values.
Change this
for(int i=0; i<rowCount; i ){
for (int j=0; i<colCount; i )
cin>>arr[i][j];
}
}
For this
for(int i=0; i<rowCount; i ){
for (int j=0; j<colCount; j )
cin>>arr[i][j];
}
}