Median and Mode of an Array

This is a discussion on Median and Mode of an Array within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that will find the meadian and mode of the values in an array. ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    44

    Median and Mode of an Array

    I am trying to write a program that will find the meadian and mode of the values in an array.

    This is the code I have so far for Median:

    Code:
    void median (double array[], int numItems)
    {
        int i, j;
        double hold;
    
        for (i = 0; i <(numItems - 1); i++)
        {
            for (j = i+1; j < numItems; j++)
            {
                if (array [j] < array [i])
                {
                    hold = array[i];
                    array[i] = array[j];
                    array[j] = hold;
                }
            }
        }
    
        if (numItems % 2 == 0)
        {
            printf ("\nThe median is %.1lf\n", ((array[numItems/ 2] + array[numItems/2 + 1]) / 2));
        }
        else
        {
            printf ("\nThe median is %.1lf\n", array[(numItems/ 2)]);
        }
    }
    And for the Mode I have:

    Code:
    void mode (double array [], int numItems)
    {
        int largestcount, i, j;
        int count [1500] = {0};
        double mode;
    
        for (i = 0; i < numItems; i++)
        {
            for (j = 0; j < numItems; j++)
            {
                if (i != j)
                {
                    if (array[i] == array[j])
                    {
                        count[i];
                    }
                }
            }
        }
    
        mode = array[0];
        largestcount = count[0];
    
        for (i = 0; i <numItems; i++)
        {
            if (count[i] > largestcount)
                largestcount = count[i];
                mode = array[i];
        }
    
        printf ("\nThe mode is %i\n", mode);
    }
    I'm having a little trouble trying to figure this one out to.

    Any info on this would be great.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Have you learnt about quicksort? As an advanced exercise for median, you could use the nth element algorithm that involves doing quicksort-style partitioning, but only performing the recursion on the partition that contains the median element. This would give you the median in linear time in the average case.

    For an array of doubles, it may make more sense to consider modal class than mode due to floating point inaccuracy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,254
    Quote Originally Posted by laserlight View Post
    For an array of doubles, it may make more sense to consider modal class than mode due to floating point inaccuracy.
    True, but it does give the additional need to specify an appropriate set of intervals to subdivide the range of data. That requires additional information (where the data came from, what the computed statistics will be used for, etc etc)
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Have you learnt about quicksort?
    I have to do my own sorting.

    If anybody knows how I can fix the existing code I have that would be great

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 04-17-2012, 05:46 PM
  2. C program: Median and Mode of array
    By djay86753 in forum C Programming
    Replies: 11
    Last Post: 04-17-2012, 10:16 AM
  3. mean, mode median calculation..
    By naspek in forum C Programming
    Replies: 2
    Last Post: 09-10-2009, 09:15 AM
  4. Finding Mode Median and Mean
    By Ginny Morgan in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:09 PM
  5. median mode
    By bobbydigital in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2003, 08:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21