Thread: C program: Median and Mode of array

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    C program: Median and Mode of array

    I have randomized an array of 1000 to fill with numbers 10-50. I need to find the median and mode. Since it was randomized I know i need to sort the array then find the median and mode, yet im unsure how to do that, please help.

    This is what i have so far...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
    int mainArray[1000];
    int size = 1000;
    int currPos = 0;
    int randNumber = 0;
    srand(20);
    
    for (currPos = 0; currPos < 1000; currPos++)
    {
    mainArray[ currPos ] = rand()%41+10;
    }
    
    }
    This is what I have for the sort so far but it isnt sorting them...

    Code:
        int i;
        int n;
        int num1, num2;
        
        for (i = (size - 1); i > 0; i--)
        {
            for (n = 1; n <= i; n++)
            {
                
                if (mainArray[(n-1)] > mainArray[n]) 
                {
                    
                    num1 = n-1;
                    num2 = n;
                    
                }
            }
        }
    Last edited by djay86753; 04-17-2012 at 09:43 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You already have another thread on this. Opening 1000 threads will not get you help any faster, it will just annoy the hell out of people because they have to cross reference their answers.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Are you planning to write your own sort (if so, do so and post back), or are can you use qsort()?

    And your code really sucks. Bad variable names, unused variables, bad spacing. Put in some effort man!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    This is what I have for the sort so far but it isnt sorting them properly...

    Code:
        int i;
        int n;
        int num1, num2;
        
        for (i = (size - 1); i > 0; i--)
        {
            for (n = 1; n <= i; n++)
            {
                
                if (mainArray[(n-1)] > mainArray[n]) 
                {
                    
                    num1 = n-1;
                    num2 = n;
                    
                }
            }
        }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you also djay86?
    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

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Break it down slowly what you are trying to do... Right now you are saying if mainArray[n-1] is greater than mainArray[n] then you want to put n-1 into num1, then n into num2... But you never reference the array your just moving normal variables around.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    no idk who that is but they are obviously copying my stuff, but i dont really care either way

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by djay86753 View Post
    This is what I have for the sort so far but it isnt sorting them properly...

    Code:
        int i;
        int n;
        int num1, num2;
        
        for (i = (size - 1); i > 0; i--)
        {
            for (n = 1; n <= i; n++)
            {
                
                if (mainArray[(n-1)] > mainArray[n]) 
                {
                    
                    num1 = n-1;
                    num2 = n;
                    
                }
            }
        }
    That's just ridiculous. You clearly have absolutely no idea what you're doing. If this is not a joke and you don't want to fail your class, then look up a simple sorting algorithm (e.g., selection sort) and implement it carefully and thoughtfully.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    you have all been so helpful. thanks!

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Oh, we are so sorry we couldn't do your homework djay, would you like a milkshake ? Get off your sorry behind and put some effort into it damnit. Work, Google, compile, test, post questions not ridiculous garbage you magically crafted in your ........ editor.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    someones a little angry. thank you, have a good day

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are welcome, good luck finding someone here that will hand out the code to you.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 04-17-2012, 05:46 PM
  2. mean, mode median calculation..
    By naspek in forum C Programming
    Replies: 2
    Last Post: 09-10-2009, 09:15 AM
  3. Mean, Median, and Mode .....code
    By AdamLAN in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2004, 02:29 PM
  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