(C++) CPP Programs - 27
Friday, December 14, 2007
27. Write a C program to read a matrix A (M*N) and to find the following using functions.
a) Sum of elements of each row.
b) Sum of elements of each column.
c) Find the sum of all the elements of the matrix. Output the computed results with suitable heading.
/* Sum of rows and coloumns*/
#include
#include
int a[5][5],m,n;
void main()
{
int i,j;
clrscr();
printf("\nEnter the order of the matrix\n");
scanf("%d%d",&m,&n);
printf("\nEnter the elements\n\b");
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf("%d",&a[i][j]);
printf("\nThe given matrix is\n\a");
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
rowsum(a);
colsum(a);
allsum(a);
getch();
}
rowsum()
{
int rsum,i,j;
for(i=0;i < m;i++)
{
rsum=0;
for(j=0;j < n;j++)
rsum=rsum+a[i][j];
printf("\nSum of %d row = %d",i+1,rsum);
}
printf("\n");
}
colsum()
{
int csum,i,j;
for(j=0;j < n;j++)
{
csum=0;
for(i=0;i < m;i++)
csum=csum+a[i][j];
printf("\nSum of %d col = %d",j+1,csum);
}
printf("\n");
}
allsum()
{
int i,j,asum=0;
for(i=0;i < m;i++)
for(j=0;j < n;j++)
asum=asum+a[i][j];
printf("\nThe sum of entire matrix = %d",asum);
}
0 comments:
Post a Comment