Finding the Mode

This is a discussion on Finding the Mode within the C Programming forums, part of the General Programming Boards category; So i have yet ANOTHER lab due next week... This one calls for us to create a program that accepts ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    101

    Finding the Mode

    So i have yet ANOTHER lab due next week... This one calls for us to create a program that accepts inputs of only positive whole numbers, (if it is negative it will end the program and output ALL modes, meaning if there is more than one mode it needs to output the numbers that are all tied for the most used.) I know it has to use arrays in one way or another, but i am having a difficult time trying to figure out how to set this program up. I am really new to arrays so my ability to use conceptualize it in my head is weak. Can anyone help me plot what this program needs to do? obviously ill need a while loop with some conditions in it. But... past that, its over my head.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, perhaps you should figure out how to keep a count of the data points.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    101

    Well..

    I believe i caught myself up a little bit on array's, so here is what i have so far... just need to find a way to create a loop that keeps track of when an integer is duplicated. and then outputting it, but im stuck.
    Code:
    #include <FPT.h>
    int main()
    {
      double n[1000],,i;
      outS("Please input a postive integer...");
      i=0;
      n[i]=inD();
      while(n>=0){
        i=i+1;
        n[i]=inD();
      }
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Have you seen a frequency chart? They look like this:
    Code:
    number  frequency
    ------  ---------
      1         4
      2         6
      3         3
      4        11
      5         7
    How would you store that in an array?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    I have never heard of a frequency chart before. Well i would know how to store the users input and keep track of the most frequently inputted number. But finding multiple modes if there is the same number? thats where i am confused.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's why you can't just "keep track of the most frequently inputted number". You have to keep track of all the numbers and how often they occur.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Well, by performing the loop i have done there, isn't that keeping track of all the numbers by storing them into the values for i? Or is there another way i need to keep track of them?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you can't find out how many 4's there are by saying frequency[4], you're not done yet.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    is frequency a function? because we have not learned that yet. or is frequence a variable?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I was just using it as a variable name. (Obviously you can call it whatever you want; count is popular.)

    And of course you knew that: anything followed by [brackets] is an array, while functions are followed by (parentheses).

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by Lucid15 View Post
    ... if there is more than one mode it needs to output the numbers that are all tied for the most used.
    You only want the "most used", or do you intend to output a list of duplicate modes, in descending order possibly?

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    I just need the mode of the inputted data set. So if there are 3 7's and 3 5's, i need it to output 7 AND 5.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Just the fact that a number is repeated matters? Then I would suggest that for every new number you are about to add, you simply loop through all existing numbers and see if it already exists. If yes, then print out that fact... and set a flag indicator (one for each number) so that you don't alert the user another time if that number comes up again. Add only unique numbers to the array.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    So basically create two different arrays, one for storing ALL inputted values and one for storing DUPLICATE values? am i getting this right? if so, i think it makes sense. Then of course after they input a negative number it checks the duplicate array and then finds if any repeat and which one repeats the most, correct?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As long as every input number gets stored somewhere, I guess that will work. You might win simultaneous awards for most inefficient and most confusing, but that's neither here nor there.

    Again, I don't see how storing the list of numbers could possibly be a good idea, since you need the counts of how often the numbers occur.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding mode!
    By afiq8289 in forum C Programming
    Replies: 1
    Last Post: 11-27-2008, 09:51 AM
  2. Finding mode in array of numbers
    By Snowcat in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2006, 08:58 PM
  3. finding mode of an array
    By n00by in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 07:40 PM
  4. Finding the mode of an array
    By Shnakepup in forum C++ Programming
    Replies: 16
    Last Post: 05-16-2003, 10:01 AM
  5. Finding Mode Median and Mean
    By Ginny Morgan in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:09 PM

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