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 matrix using array

I made an array matrix and used for loops, the problem is that it only displays my last input. Please refer to the sample result below.

#include<iostream>
using namespace std;
int main()
 {
   int x = 0, y = 0;
   int a[x][y], i, j;

cout<<"Enter number of Columns: ";
cin>>y;

cout<<"Enter number of Rows: ";
cin>>x;

       cout<<endl;

 for(i=0; i<x; i++)
{
        cout<<endl;
        cout<<"Enter values or row/s: ";
        cout<<endl;
                            
for(j=0; j<y; j++)
{
    cout<<endl;
    
    cout<<"Row "<< i+1 << ": ";
    cin >> a[i][j]; 
   }
   }

cout<<endl;

cout<<"Entered Matrix is: ";

cout<<endl;

for(i=0; i<x; i++)
{
    for(j=0; j<y; j++)
    
        cout<<a[i][j]<<"  ";
        
    cout<<endl;
    
     }


   }

SAMPLE RESULT:

Enter number of Columns: 3

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

Enter number of Rows: 2

Enter values or row/s:

Row 1: -1

Row 1: -2

Row 1: -3

Enter values or row/s:

Row 2: 4

Row 2: 5

Row 2: -6

Entered Matrix is:

4 5 -6

4 5 -6

>Solution :

Just change declaration of array like this:

int[x][y] to int[10][10]

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