Program to find Mean,Median,and Mode

/***************************************************************
******* Program to find Mean,Median,and Mode *******
****************************************************************/

#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));

}

 


Output
————–

Enter Elements
2
3
4

Enter Choice
1.Mean
2.Median
3.Mode
4.Exit
1

Mean = 3.000000

Enter Choice
1.Mean
2.Median
3.Mode
4.Exit
2

Median = 3.000000

Enter Choice
1.Mean
2.Median
3.Mode
4.Exit
3

Mode = 3.000000

Enter Choice
1.Mean
2.Median
3.Mode
4.Exit
4

——————-
Enter No of Elements
4
Enter Elements
9 8 7 6

Enter Choice
1.Mean
2.Median
3.Mode
4.Exit
1

Mean = 7.500000

Enter Choice
1.Mean
2.Median
3.Mode
4.Exit
2

Median = 7.500000

Enter Choice
1.Mean
2.Median
3.Mode
4.Exit
3

Mode = 7.500000

Enter Choice
1.Mean
2.Median
3.Mode
4.Exit
4


Keys : program to find mean, program to find median, program to find mode, mean median mode program, program to find mean median mode, find the median, finding the median and mode, c programming median, "find mode" array median, find mode, array median,median program in c, c program finding mode of a list, c program on mean median and mode, program to find mean median and mode, mean median c programming, how to find the median, median in c programming.

VN:F [1.9.22_1171]
Rating: 6.5/10 (26 votes cast)
VN:F [1.9.22_1171]
Rating: +6 (from 16 votes)
Program to find Mean,Median,and Mode, 6.5 out of 10 based on 26 ratings

22. August 2008 by Jishnu
Categories: C Programming | Tags: | 25 comments

Comments (25)

  1. i wanted a program to find mean, median and mode WITHOUT using switch function!!!!!!!! its a nice program but i think its DULL!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  2. make just a simple but correct program to find something….. dont just make it look like very complicated because of just adding nonsense codes!!!!!! make it easy to understand!!!!!!!

  3. Nice program, though shouldn’t the mode be the most frequent number?

  4. Nica and easy program………this program totally help me to complete my project……

  5. saya pengen tahu yang jelas tentang contoh code programnya bisa tidak di cantumkan,terimakasih sebelumnya ^^

  6. the mode is wrong.

  7. how about the mode???

  8. @j
    Can you tell me what is the problem?

  9. Nice and elegant program. Mr. Jishnu, mind if I use this source code for a project we have in college ????

  10. you are a very good programmer.
    thanks to you.
    can you write program for insertion,deletion an element from stack,queue using singly linked list.
    please help me.

  11. Your median is completely wrong. It only works if you have the numbers in order from least to greatest. Other than that it fails every other time

  12. @Steve,
    are you sure ?
    can you post your output ?

    Here is my output (not in the least to greatest order as you said)

    Enter No of Elements
    4
    Enter Elements
    6
    4
    4
    1

    Enter Choice
    1.Mean
    2.Median
    3.Mode
    4.Exit2

    Median = 4.000000

  13. hai jishnu
    can i ask one questain.pls give me a answer.how u get the logic to do like this.
    i’am also a mca student butI can’t pls answer me pls pls pls

  14. Hi! I am shan. I am working in a school[Teacher].
    I need perfect answer and also that Short answer type in “C-Program”. Becoz, the pupils have not more capacity about this “C-Program”. Becoz, this is the foundation. So plz provide a very short type program about “Related Important All Program”. So don’t mistaken me. This is my “KIND” request. —> By Shanthi

  15. i didn’t understand what u mean..

  16. Write a C++ program that will compute the mean, median, mode of a given data

  17. I just want to run a program that shows me the median betwhin 3 integer numbers, I wrote it but it doesnt work. here it is: what do yo you think about it?
    #include
    using namespace std;
    int main()
    {

    long int a,b,c,med;
    cout<<"please enter 3 integer numbers:";
    if ((a-b)*(a-c)<0)
    {
    med=a;
    }
    if ((b-a)*(b-c)<0)
    {
    med=b;
    }
    if ((c-a)*(c-b)<0)
    {
    med=c;
    }
    cout<<"median="<<"med"<<endl;
    return 0;
    }

  18. I’ve been surfing a lot this night and I’m glad that I landed on your page for out of all the program your’s is the only one who would run and I’m not exaggerating.
    Thank you so much for sharing this code, really :)

  19. yeah this is very helpful for the students of grade 8&9 for getting knowledge about the basic programing. superb work keep it up.

  20. Hey Dude.. The Mode is Wrong..It is showing wrong answer.. Other than that the Programming is Good.
    Good Progamming.. Just try to correct that..ok

  21. this program was very nice but iwant to simple program of mean,mode, mdian

  22. Mode is wrong. Try checking out the definition of mode(statistics)

  23. Here is my code…. It is a bit long but I know you can get it :). It is long but very good. It gives you bimode, trimode…all the modes (in case there is more than one mode).

    For example.

    2,3,4,4,2 … the mode is = 2,4
    and
    3,3,4,4 does not have a mode because all the modes are the same.

    the String parameter for this method must be like this “2,3,4,2,23,234,21,123,34″
    It organices the numbers and gives you all the info!

    public String calcularEnOrden2(String numeros){
    int modeCount =0;
    ArrayList integers = new ArrayList();
    int repeatedCounter=0;
    String[] numbers0 = numeros.split(“,”);
    int[] numbers = new int[numbers0.length];
    for(int x=0; x<numbers.length;x++){
    numbers[x]= Integer.parseInt(numbers0[x]);
    }
    int[] numbersP = new int[numbers.length];
    for(int i=0; i<numbers.length;i++){
    int timesRepeatedInTheArray =0;
    int biggerThanHowManyNumbersInTheArray = 0;
    for(int j=0; jnumbers[j]){
    biggerThanHowManyNumbersInTheArray++;
    }
    }
    if(repeatedCounter<timesRepeatedInTheArray){
    if(repeatedCounter!=0){
    integers.clear();
    integers.add(numbers[i]+"");
    }
    repeatedCounter = timesRepeatedInTheArray;
    }
    else if(repeatedCounter == timesRepeatedInTheArray){
    boolean iBangYourWife = true;
    modeCount++;
    for(int y=0; y<integers.size();y++){
    if(Integer.parseInt(integers.get(y)) == numbers[i]){
    iBangYourWife = false; // :(
    }
    }
    if(iBangYourWife){
    integers.add(numbers[i]+"");
    }
    }
    //This is how I organize numbers from smaller to bigger. You should analyze it with pencil and paper, it is very useful for the future.
    int counter = timesRepeatedInTheArray;
    for(int m=biggerThanHowManyNumbersInTheArray; m<numbers.length;m++){
    if(counter != 0){
    numbersP[m] = numbers[i];
    counter–;
    }
    }
    }
    int length = numbersP.length;
    double medianA=0;
    double contador = numbersP.length;
    double numberPValuesTotalSum = 0;
    String respuesta= "";
    for(int h=0; h<numbersP.length;h++){
    respuesta = respuesta + " " + numbersP[h];
    numberPValuesTotalSum = numberPValuesTotalSum + numbersP[h];
    }
    if(numbersP.length%2 !=0){
    Double median0 = ((numbersP.length/2) – 0.5);
    medianA = numbersP[median0.intValue()];
    }
    else{
    medianA = ((numbersP[length/2] + numbersP[(length/2)-1])/2.0);
    }
    String modes="";
    if(modeCount == numbers.length-1){
    modes ="No existing modes";
    }
    else{
    for(int l=0;l<integers.size();l++){
    if(l==0){
    modes =integers.get(l);
    }
    else{
    modes = modes + ", " + integers.get(l);
    }
    }
    }
    double totalMean = numberPValuesTotalSum/contador;
    String final2 = "Smaller to Bigger: \n"+
    respuesta+"\n"+
    "Mean: \n"
    +totalMean+"\n"+
    "Median:\n"+
    +medianA+"\n"+
    "Mode: \n"
    +modes+"";
    return final2;
    }

Leave a Reply