C Program: Replacing Element by 1 or 0 Based on Average of Matrix Elements
Question: Write a program in C to read square matrix of order n, find average of elements and then replace each element by 1 if it is greater than average otherwise replace by 0.
#include<stdio.h>
int main()
{
float a[10][10],avg, sum=0.0;
int i,j,n;
printf("Enter n:\n");
scanf("%d",&n);
printf("Enter matrix:\n");
for(i=0;i< n;i++)
{
for(j=0;j< n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%f",&a[i][j]);
sum = sum + a[i][j];
}
}
avg = sum/(n*n);
for(i=0;i< n;i++)
{
for(j=0;j< n;j++)
{
if(a[i][j] >avg)
{
a[i][j] = 1;
}
else
{
a[i][j] = 0;
}
}
}
printf("Resultant matrix is:\n");
for(i=0;i< n;i++)
{
for(j=0;j< n;j++)
{
printf("%0.1f\t", a[i][j]);
}
printf("\n");
}
}