Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a 2D array with user input

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading