This C Programs Finds the Average, Sum and Mean Deviation of Elements of Array.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int arrsum(int *,int);
float average(int,int);
float deviation(int *,float,int);
void main()
{
int a[10],n,i,sum;
float avg,mean;
clrscr();
printf("Enter number of elements of an array (upto 10)\n");
scanf("%d",&n);
printf("Enter elements of an array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum=arrsum(a,n);
avg=average(sum,n);
mean=deviation(a,avg,n);
printf("Sum = %d\nAverage = %f\nMean Deviation = %f\n",sum,avg,mean);
getch();
}
int arrsum(int *a,int n)
{
int i,s=0;
for(i=0;i<n;i++)
{
s+=*(a+i);
}
return s;
}
float average(int s,int n)
{
float avg=0.0;
avg=(float)s/n;
return avg;
}
float deviation(int *a,float avg,int n)
{
int i;
float s=0.0,dev=0.0;
for(i=0;i<n;i++)
{
s+=((*(a+i)-avg)*(*(a+i)-avg));
}
dev=sqrt(s/n);
return dev;
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
int arrsum(int *,int);
float average(int,int);
float deviation(int *,float,int);
void main()
{
int a[10],n,i,sum;
float avg,mean;
clrscr();
printf("Enter number of elements of an array (upto 10)\n");
scanf("%d",&n);
printf("Enter elements of an array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum=arrsum(a,n);
avg=average(sum,n);
mean=deviation(a,avg,n);
printf("Sum = %d\nAverage = %f\nMean Deviation = %f\n",sum,avg,mean);
getch();
}
int arrsum(int *a,int n)
{
int i,s=0;
for(i=0;i<n;i++)
{
s+=*(a+i);
}
return s;
}
float average(int s,int n)
{
float avg=0.0;
avg=(float)s/n;
return avg;
}
float deviation(int *a,float avg,int n)
{
int i;
float s=0.0,dev=0.0;
for(i=0;i<n;i++)
{
s+=((*(a+i)-avg)*(*(a+i)-avg));
}
dev=sqrt(s/n);
return dev;
}