# Thread: mean, mode median calculation..

1. ## mean, mode median calculation..

hey there..
this is my code...

Code:
```#define SIZE 100
#include<stdio.h>
float mean_function(float[],int);
float median_function(float[],int);
float mode_function(float[],int);
int main()
{
int i,n,choice;
float array[SIZE],mean,median,mode;
printf("Enter No of Elements\n");
scanf("%d",&n);
printf("Enter Elements\n");
for(i=0;i<n;i++)
scanf("%f",&array[i]);
do
{
printf("\n\tEnter Choice\n\t1.Mean\n\t2.Median\n\t3.Mode\n4.Exit");
scanf("%d",&choice);
switch(choice)
{
case 1: mean=mean_function(array,n);
printf("\n\tMean = %f\n",mean);
break;
case 2: median=median_function(array,n);
printf("\n\tMedian = %f\n",median);
break;
case 3: mode=mode_function(array,n);
printf("\n\tMode = %f\n",mode);
break;
case 4: break;
default:printf("Wrong Option");
break;
}
}while(choice!=4);
}

/***************************************************************
Function Name : mean_function
Purpose : to find mean
Input : array of elements,no of elements
Return Value : Mean
Return Type : Float
****************************************************************/
float mean_function(float array[],int n)
{
int i;
float sum=0;
for(i=0;i<n;i++)
sum=sum+array[i];
return (sum/n);
}

/***************************************************************
Function Name : median_function
Purpose : to find median
Input : array of elements,no of elements
Return Value : Median
Return Type : Float
****************************************************************/

float median_function(float a[],int n)
{
float temp;
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
if(n%2==0)
return (a[n/2]+a[n/2-1])/2;
else
return a[n/2];
}

float mode_function(float a[],int n)
{
return (3*median_function(a,n)-2*mean_function(a,n));
}```

i've got problem with "mode" formula..
the program didn't give me the correct answer..
should i use "sort()"?

2. Is this what you were trying for?
In continuous unimodal distributions the median lies, as a rule of thumb, between the mean and the mode, about one third of the way going from mean to mode. In a formula, median ≈ (2 × mean + mode)/3. This rule, due to Karl Pearson, is however not always true and the three statistics can appear in any order.[1] It often applies to slightly non-symmetric distributions that resemble a normal distribution.
Since it "is not always true", I wouldn't use it for your program.

I would set up your categories (the range of each), and then just count the instance of each in your data, using a simple integer array. Similar to a histogram.

Wikipedia article mentions sorting in their article on mode (from which the above quote is taken), but I see no reason to sort the data to find the mode of your data.

3. I dunno, 30+ posts, and still can't indent your way out of a wet paper bag.

It's an unreadable mess that is going ignored!