2

How can I edit this function to find multiple modes? Right now if there are multiple, it will display the smallest.

Example

Input 5 5 2 2 Output 5 2

What it actually does

Input 5 5 1 1 Output 1

void calculateMode(int array[], int big)
{

    int counter = 1;
    int max = 0;
    int mode = array[0];
    for (int pass = 0; pass < big - 1; pass++)
    {
       if ( array[pass] == array[pass+1] )
       {
          counter++;
          if ( counter > max )
          {
              max = counter;
              mode = array[pass];
          }
       } else
          counter = 1; // reset counter.
    }
cout << "The mode is: " << mode << endl;
}

Anything helps!

jrod
  • 21
  • 2
  • 5
  • 3
    If use of the standard library is allowed by the assignment, consider using `std::map freq;` to create a mapping of an integer to the number of times you've seen it: `freq[array[pass]]++` Then look in freq for the highest count. If not, then your best bet is to learn how to use the debugging software that almost certainly came with your development environment. Once you start stepping through your code line by line you will quickly see your mistake. – user4581301 Mar 24 '17 at 16:47
  • 1
    Possible duplicate of [Algorithm to compute mode](http://stackoverflow.com/questions/18177647/algorithm-to-compute-mode) ... the answer by Dietmar Kühl is very close to what you're trying to do here. – andand Mar 24 '17 at 16:50

2 Answers2

0

I like also the stdlib option as one of the comments refers. However, I tried to solve this problem without using it as you (as an exercise). I had as a requirement to have a constant array as a function parameter, so I could not order it (nor remove the constant nor copy it in a new non-const one). In addition, if there are multiple modes or no elements, I had to return zero.

At the end a came up with something like the following. Hopefully, it might help.

#include <iostream>
#include <stdexcept>

template <typename T> T mode(const T *values, size_t length) {

  // check if it has zero length
  if (!length)
    return 0;

  if (!values)
    throw std::invalid_argument{"Invalid input array"};

  int count{}, maxOccurrences{};
  int multipleModes{};
  T mode{};

  // check every element unless the mode's occurrences are greater than the
  // remaining list
  for (int k{}; k < length && maxOccurrences <= (length - k); ++k) {

    // reset the count for every individual element
    count = 0;

    // count the number of occurrences
    for (int i{}; i < length; ++i) {
      if (values[k] == values[i])
        count++;
    }

    if (count > maxOccurrences && mode != values[k]) {
      mode = values[k];
      maxOccurrences = count;
      multipleModes = 0;
      /*std::cout << "Count:" << count << " - MaxOccur:" << maxOccurrences
                << " - Mode:" << mode << std::endl;*/
    }

    if (count == maxOccurrences && mode != values[k]) {
      // if the array has multiple modes
      multipleModes = 1;
    }
  }

  if (multipleModes == 1)
    return 0;
  else
    return mode;
}

Thanks for you attention!

angelos.p
  • 500
  • 1
  • 5
  • 12
0

you can try adding this

else if (counter==max){
   mode += array[pass]
}

can't test it on my own system. see if it's of any help.