Column sum is a matrix operation in which the elements of each column are added and stored
in a different array.......
Program:
#include<iostream>
#include<iomanip>
#include<windows.h>
using namespace std;
/*
This is a function to compute
the column sum....
*/
void colsum(int a[][30],int b[],int m,int n)
{
int i,j;
b[n];
for(j=0;j<n;++j)
{
b[j]=0; // Initializes sum to zero at begining of each column
for(i=0;i<m;++i)
{
b[j]+=a[i][j];
}
}
}
int main()
{
system("cls");
cout<<setw(54)<<"*****----------------------*****\n";
cout<<setw(54)<<"*****COLUMN SUM OF A MATRIX*****\n";
cout<<setw(54)<<"*****----------------------*****\n";
cout<<"\n\n";
int a[30][30],b[30],m,n,i,j;
cout<<"\tEnter row size: ";
cin>>m;
cout<<"\tEnter column size: ";
cin>>n;
cout<<"\n\n\tEnter the matrix: \n\n";
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
cout<<"\tEnter ["<<i<<"]["<<j<<"] : ";
cin>>a[i][j];
}
}
Sleep(1000);
system("cls");
cout<<"\n\n\n\t\t\tThe matrix is: \n\n";
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n\n\t\t\tProcessing.......";
Sleep(2200);
cout<<"\n\n\n\t\tThe column sum of the matrix is: \n\n";
colsum(a,b,m,n);
for(i=0;i<n;++i)
cout<<"Column "<<i+1<<" : "<<b[i]<<"\n";
return 0;
}
Output:
0 comments:
Post a Comment