This C++ program calculates the sum of all the rows and columns of a given Matrix. As you can see in the image below, the sum of the rows and columns are given on the Right side with green arrows and Bottom with red arrows respectively. See code below:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int A[10][10], i, j, r[10], c[10], row, col;
cout<<"Enter the number of rows & columns of Matrix :\n";
cin>> row >> col;
cout << "Enter the Elements of Matrix: \n";
for(i=0; i<row; ++i)
{
for(j=0; j<col; ++j)
{
cin>> A[i][j];
}
}
cout<< "\nGiven Matrix is : ";
for(i = 0; i<row; ++i)
{
cout<< "\n";
for(j = 0; j<col; ++j)
{
cout<< A[i][j]<< " ";
}
}
cout<<"\n";
for(i=0; i<row; i++)
{
r[i] =0;
for(j=0; j<col; ++j)
{
r[i] += A[i][j]; //Row Sum
}
}
for(j=0; j<col; ++j)
{
c[j] = 0;
for(i=0; i<row; ++i)
{
c[j] += A[i][j]; //Column Sum
}
}
for(i=0; i<row; ++i)
{
cout<< "\nSum of Row# " << i+1 <<" : " << r[i];
}
for(i=0; i<col; ++i)
{
cout<< "\nSum of Col# " << i+1 <<" : " << c[i];
}
}
No comments:
Post a Comment