Monday, 26 December 2011

C program to find the mean,median and mode of a set of numbers



#include<stdio.h>
main()
{
int i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,mode;
float x=0.0,y=0.0;
printf("\nEnter the limit\n");
scanf("%d",&n);
printf("Enter the set of numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
x=(float)sum/(float)n;
printf("Mean\t= %f",x);

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;
}
}
}
if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2];
printf("\nMedian\t= %f",y);

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("\nThere is no mode");
else
{
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}




You Like It!? Then kindly share with your Friends.
Comments
5 Comments

5 comments:

Anus Baig said...

It's wrong for high limit

RAGHAVENTHRAN.T.R NIKHIL R said...

It's wrong for mode, you guys don't even know what is mode. Mode is nothing but most occurred element.
So what u have written for mode is wrong.
Pls correct it.

Shibu said...

int mode(int a[],int n) {
int maxValue = 0, maxCount = 0, i, j;

for (i = 0; i < n; ++i) {
int count = 0;

for (j = 0; j < n; ++j) {
if (a[j] == a[i])
++count;
}

if (count > maxCount) {
maxCount = count;
maxValue = a[i];
}
}

return maxValue;
}

int main() {
int n = 5;
int a[] = {0,6,7,2,7};

printf("Mode = %d ", mode(a,n));

return 0;
}

Shibu said...

int mode(int a[],int n) {
int maxValue = 0, maxCount = 0, i, j;

for (i = 0; i < n; ++i) {
int count = 0;

for (j = 0; j < n; ++j) {
if (a[j] == a[i])
++count;
}

if (count > maxCount) {
maxCount = count;
maxValue = a[i];
}
}

return maxValue;
}

int main() {
int n = 5;
int a[] = {0,6,7,2,7};

printf("Mode = %d ", mode(a,n));

return 0;
}

Shibu said...

#include

int mode(int a[],int n) {
int maxValue = 0, maxCount = 0, i, j;

for (i = 0; i < n; ++i) {
int count = 0;

for (j = 0; j < n; ++j) {
if (a[j] == a[i])
++count;
}

if (count > maxCount) {
maxCount = count;
maxValue = a[i];
}
}

return maxValue;
}

int main() {
int n = 5;
int a[] = {0,6,7,2,7};

printf("Mode = %d ", mode(a,n));

return 0;
}

Post a Comment