Friday, March 4, 2011

User-Defined Functions

Design and code an interactive modular program that will use functions to a matrix of m by n size, compute column average and row averages, and then print the entire matrix with average shown in respective rows and columns.

#include <stdio.h>
void Average(int A[20][20]);
int m,n;
void main()
{
int i,j,A[20][20];
clrscr();
printf("Enter the number of Rows:");
scanf("%d",&m);
printf("Enter the number of Column:");
scanf("%d",&n);
printf("Enter elements of %dX%d matrix elements\n",m,n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&A[i][j]);
Average(A);
getch();
}
void Average(int A[20][20])
{
int i,j,Row[10]={0},Col[10]={0};
float avg[10],avg1[10];
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
Col[i]+=A[i][j];
Row[j]+=A[i][j];
}
avg[i]=(float)Col[i]/n;
}
for(j=1;j<=n;j++)
avg1[j]=(float)Row[j]/m;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("%5d",A[i][j]);
}
printf("%7.2f",avg[i]);
putchar('\n');
}
putchar('\n');
for(j=1;j<=n;j++)
printf("%5.2f",avg1[j]);
}

No comments:

Post a Comment