Thread: mean, mode median calculation..

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38

    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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mean, Median, and Mode .....code
    By AdamLAN in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2004, 02:29 PM
  2. Finding Mode Median and Mean
    By Ginny Morgan in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:09 PM
  3. median mode
    By bobbydigital in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2003, 08:53 PM
  4. Computing Mean, Median and Mode Using Arrays
    By Rodneo in forum C++ Programming
    Replies: 0
    Last Post: 05-29-2002, 11:40 PM
  5. Implementing "ls -al"
    By pdstatha in forum C Programming
    Replies: 7
    Last Post: 03-06-2002, 05:36 PM