Program to fined mean median mode and range in c using array
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[100],i,n,c=0,sum=0,j,b[100],s;
float avg,median,range,mode,wavg,sd,mean,vsum,ksum;
clrscr();
printf("\nEnter the size of array:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
while(c!=7)
{
printf("\n1.Mean of array element");
printf("\n2.Weighted average array element");
printf("\n3.Median of array element");
printf("\n4.Mode of array element");
printf("\n5.Range of array element");
printf("\n6.Standard deviation of array element");
printf("\n7.Exit");
printf("\nSelect the choice:");
scanf("%d",&c);
switch(c)
{
case 1:
for(i=0;i<n;i++)
sum+=a[i];
avg=sum/n;
printf("\nMean of array element:%f",avg);
break;
case 2:
for(i=0;i<n;i++)
{
printf("b%d=",i+1);
scanf("%d",&b[i]);
wavg+=a[i]*b[i];
}
printf("\nWeighted average is= %f",wavg);
break;
case 3:
if(n%2==0)
median=(a[n/2]+a[n/2-1])/2;
else
median=a[n/2];
printf("\nMedian of array is:%f",median);
break;
case 4:
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
mode=a[i];
}
}
printf("\nThe mode of array is:%f",mode);
break;
case 5:
range=a[n-1]-a[0];
printf("\nThe range of array is:%f",range);
break;
case 6:
for(i=0;i<n;i++)
s=s+a[i];
mean=s/n;
for(i=0;i<n;i++)
vsum=vsum+((a[i]-mean)*(a[i]-mean));
ksum=vsum/n;
sd=sqrt(ksum);
printf("the SD is %f",sd);
break;
}
}
getch();
}
Output:
Enter
the size of array:4
1
2
3
4
1.Mean
of array element
2.Weighted
average array element
3.Median
of array element
4.Mode
of array element
5.Range
of array element
6.Standard
deviation of array element
7.Exit
Select
the choice:1
Mean
of array element:2.000000
1.Mean
of array element
2.Weighted
average array element
3.Median
of array element
4.Mode
of array element
5.Range
of array element
6.Standard
deviation of array element
7.Exit
Select
the choice:2
b1=1
b2=2
b3=3
b4=4
Weighted
average is= 30.000000
1.Mean
of array element
2.Weighted
average array element
3.Median
of array element
4.Mode
of array element
5.Range
of array element
6.Standard
deviation of array element
7.Exit
Select
the choice:3
Median
of array is:2.000000
1.Mean
of array element
2.Weighted
average array element
3.Median
of array element
4.Mode
of array element
5.Range
of array element
6.Standard
deviation of array element
7.Exit
Select
the choice:4
The
mode of array is:0.000000
1.Mean
of array element
2.Weighted
average array element
3.Median
of array element
4.Mode
of array element
5.Range
of array element
6.Standard
deviation of array element
7.Exit
Select
the choice:5
The
range of array is:3.000000
1.Mean
of array element
2.Weighted
average array element
3.Median
of array element
4.Mode
of array element
5.Range
of array element
6.Standard
deviation of array element
7.Exit
Select
the choice:6
the
SD is 1.224745
1.Mean
of array element
2.Weighted
average array element
3.Median
of array element
4.Mode
of array element
5.Range
of array element
6.Standard
deviation of array element
7.Exit
No comments:
Post a Comment