Tuesday, 22 October 2013

C-Program to Find Mean Median Mode of The Elements of a Set

#include<stdio.h>
main ()
{
int n,i,a[50]={0},sum=0,j,t,k=0,b[50]={0},max=0,c=1,mode;
float median=0.0,mean=0.0;
printf("Enter the no.of elements in the set\t");
scanf("%d",&n);
printf("\nEnter the elements\t");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}



/*SORTING(Bubble sort method)*/

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("Ordered set is\t");
for(i=0;i<n;i++)
printf("%d\t",a[i]);



/*MEAN*/

mean=(float)sum/n;
printf("\n\n1.MEAN of the elements of the set:\t%f",mean);




/*MEDIAN*/

if(n%2==0)
median=(float)(a[(n/2)]+a[(n-1)/2])/2;
else
median=a[(n-1)/2];
printf("\n2.MEDIAN is\t%f",median);





/*MODE*/

for(i=0;i<n-1;i++)
{
mode=0;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
mode++;
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i];
k++;
}
}

for(i=0;i<n;i++)
{
if(a[i]==b[i])
c++;
}  
if(c==n)
printf("\n3.There is no MODE");
else
{
printf("\n3.MODE is \t");
for(i=0;i<k;i++)
printf("%d\t",b[i]);
}
}





sakoncepts.blogspot.com
output




LOGIC REFERENCE(WIKIPEDIA):
       1.Mode(Statistics)
              2.Median‎
              3.Mean‎



































No comments:

Post a Comment