0

I was solving this particular problem where I was supposed to modify a 3* 3 matrix to a 4* 4 matrix.

->, where the 4th column will contain the sum of rows and similarly 4th row, will contain the sum of columns. but I was getting a very confusing output...

This is my code:

#include<iostream>
using namespace std;                             // need to fix errors

int main()
{
    int mat[3][3];
    cout<<" Enter elements :\n";
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            cin>>mat[i][j];
        }
    }

    mat[4][4];
    

    int column=0,row=0;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
        
            column=column+mat[j][i];
            row=row+mat[i][j];

        }
        mat[i][3]=row;
        mat[3][i]=column;
        row=0,column=0;
    }
    int ans=0;

    for(int i=0;i<4;i++)    //this part is for calculating the bottom left most value mat[3][3]
    {
        ans=ans+mat[i][3];
  }
  mat[3][3]=ans;

    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            cout<<mat[i][j]<<" ";
        }
        cout<<endl;
    }


}

This code gives the following output: enter image description here

someone, please tell me what's wrong with the code.

0 Answers0