Pages

Friday, 3 July 2015

Mean of elements

C Program to Calculate Average Using Arrays

Coding:

#include<stdio.h>
#include<stdlib.h>
int main()
{
float *a,mean,sum=0;
int n,i;
printf("Please enter total number of elements:");
scanf("%d",&n);
a=(float*)malloc(n*sizeof(float));
for(i=0;i<n;i++)
{
printf("Enter element %d:",i+1);
scanf("%f",&a[i]);
sum=sum+a[i];
}
mean=sum/n;
printf("Mean is=%f",mean);
return 0;
}
Output:
Please enter total number of elements:5
Enter element 1:10
Enter element 2:20
Enter element 3:30
Enter element 4:15
Enter element 5:24
Mean is=19.799999