Program to find the sum of each row & column of a matrix of size n x m

Started by aruljothi, Jun 11, 2009, 04:30 PM

Previous topic - Next topic

aruljothi

#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int A[10][10],m,n,x,y,sum=0;
//Create a Matrix A
cout << "Enter number of rows and columns in Matrix A : \n";
cin>>n>>m;
cout << "Enter elements of Matrix A : \n";
for(x=1;x<n+1;++x)
for(y=1;y<m+1;++y)
cin>>A
  • [y];
    //Find sum of each row
    for(x=1;x<n+1;++x)
    {
    A
  • [m+1]=0;
    for(y=1;y<m+1;++y)
    A
  • [m+1]=A
  • [m+1]+A
  • [y];
    }
    //Find sum of each column
    for(y=1;y<m+1;++y)
    {
    A[n+1][y]=0;
    for(x=1;x<n+1;++x)
    A[n+1][y]+=A
  • [y];
    }
    cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n";
    for(x=1;x<n+1;++x)
    {
    for(y=1;y<m+2;++y)
    cout << A
  • [y] << "     ";
    cout << "\n";
    }
    //Print sum of each column
    x=n+1;
    for(y=1;y<m+1;++y)
    cout << A
  • [y] << "     ";
    cout << "\n";
    if(m==n)
    {
    for(x=1;x<m+1;x++)
    for(y=1;y<n+1;y++)
    if(x==y)
    sum+=A
  • [y];
    else
    if(y==m-(x+1))
    sum+=A
  • [y];
    }
    cout << "Sum of diagonal elements is : " << sum << endl;
    getch();
    return 0;
    }