Thread: Median and Mode of 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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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