Finding the mode of an array

This is a discussion on Finding the mode of an array within the C++ Programming forums, part of the General Programming Boards category; I am trying to find the mode, or the number which occurs most often, of my array. This is the ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    17

    Question Finding the mode of an array

    I am trying to find the mode, or the number which occurs most often, of my array. This is the scores array, which is sorted:

    Code:
            820
            987
            1000
            1020
            1100
            1200
            1450
            1550
            1680
            2352
            2400
            2400
            2400
    So far I have gotten the program to count how many times there is a duplicate, which is 3 because 2400 occurs 3 times. But I haven't gotten the program to print out which of the numbers occurs 3 times.
    Here is my incomplete code:

    Code:
    int mode (int scores [], int n)
    {
        int counter = 0;
        for (int pass = 0; pass < n - 1; pass++)
              for (int count = pass + 1; count < n; count++) {
                   if (scores [count] == scores [pass]) 
                   counter++;               
              }
              file_out << counter;
    }
    How can I modify this so that it will tell me that 2400 occurs 3 times?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    Your current code does not appear to find the mode, since you do not compare the current count with the current maximum frequency count.

    A simple solution would be to use a std::map<int, int> to map the numbers to their frequencies. Once you have inserted the numbers, you can use std::max_element() to find the maximum frequency and from there print out both the mode and its frequency.
    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
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i dont think your algorithm for getting the number of occurrences is correct. add a few more numbers, so that the mode is somewhere in the middle and you should notice.

    ive never written one, but i assume it would be a more complicated algorithm than that to find the maximum number of occurrences, and once you have that i imagine it would be straightforward to find out which one it was.

    post back with your corrected code for doing the same task. (unless im wrong, of course!)

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    17
    I haven't learned maps yet. I looked it up and it's way past my level. I think I'll wait on this one, maybe talk to my professor, it's optional anyway.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    Well, you do not need std::map to find the mode. With a sorted array, counting frequencies is pretty easy, and you seem to have the right idea. What you need to do is keep track of what is the current maximum frequency and its associated number. Think along the lines of what you would do if you have to implement an algorithm to find the maximum value in an array, except now you do not just need to keep track of the value, but also its key.
    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

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Algorithm works pretty much like this:
    Code:
    var currentMaxFreq = 0, currentMode = 0;
    var currentFreq = 0, currentNum = -1;
    for each num in sortedArray {
      if currentNum != num {
        if currentFreq > currentMaxFreq {
          currentMaxFreq = currentFreq;
          currentMode = currentNum;
        }
        currentFreq = 0;
        currentNum = num;
      }
      ++currentFreq;
    }
    What's missing is being a bit more careful at the start and handling the last number.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 01:31 PM
  2. Finding array dimensions
    By deviousdexter in forum C# Programming
    Replies: 3
    Last Post: 11-12-2008, 09:34 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Finding a hyphen in an array.
    By omnificient in forum C Programming
    Replies: 7
    Last Post: 06-17-2008, 02:31 AM
  5. Finding largest element in array
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 09:18 PM

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