-1

My code to find the mode (most often) and how many times said mode was displayed runs into a never-ending loop. Does anyone know what I can do to fix it?

EDIT I UPDATED THE CODE: It returns 0, which is not the mode.

void calculateMode(int array[], int size)
{     
    int counter = 0;
    int max = 0;
    int mode = 0;
    for (int pass = 0; pass < size - 1; pass++)
        for (int count = pass + 1; count < size; count++) {
             if (array[count] > max) {
                max = array[count];
                mode = 1;
                counter = array[pass];
             }

             cout << "The mode is: " << counter "It's been displayed: " << count << "times" << endl; 

        }
Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • `counter` is not the mode I think. – gongzhitaao Nov 12 '13 at 04:20
  • Your braces are not balanced. – Olaf Dietsche Nov 12 '13 at 04:20
  • 1
    `cout << "The mode is: " << counter << "It's been displayed: "` – greatwolf Nov 12 '13 at 04:21
  • How would I balance the braces? – user2981720 Nov 12 '13 at 04:22
  • your code doesn't compiler as-is btw due to the syntax error I mentioned and the unbalance brace by @Olaf. – greatwolf Nov 12 '13 at 04:24
  • for (int pass = 0; pass < size - 1; pass++){ for (int count = pass + 1; count < size; count++) { if (array [count] == array [pass]){ counter++; cout<< "The mode is: " << counter <<"It's been displayed: "<< count << "times" << endl;}//eno of if }//end of inner for }//end of outer for – Ali Kazmi Nov 12 '13 at 04:24
  • I made that change, the loop terminates, but the mode is off. – user2981720 Nov 12 '13 at 04:30
  • possible duplicate of [Algorithm to compute mode](http://stackoverflow.com/questions/18177647/algorithm-to-compute-mode) – Jerry Coffin Nov 12 '13 at 04:36
  • To find the mode you need to compute how many times each separate character is found, and determine which of these numbers is the greatest. You are not doing that. – n. m. could be an AI Nov 12 '13 at 04:40
  • It's a list of numbers. – user2981720 Nov 12 '13 at 04:44
  • Possible duplicate of http://stackoverflow.com/questions/19920542/c-calculating-the-mode-of-a-sorted-array/19920685#19920685 – doptimusprime Nov 12 '13 at 04:50
  • The function returns `void`; it can't return anything else. You set `mode` to 0, and then maybe to 1, and then never use it. You report `"The mode is "<< counter` on *every iteration* through the inner loop. It's not at all clear what `max` represents, or why the inner loop is there at all. You also report `"It's been displayed: "<< count << "times"`, when `count` is just a loop counter, completely unrelated to the contents of the array. And you want to know why the code doesn't produce the correct answer? – Beta Nov 12 '13 at 05:00

2 Answers2

0

A solution using map. Compile with g++ -std=c++11 a.cpp.

Here is definition of mode

#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main()
{
    vector<int> v = {1, 1, 2, 2, 3, 3};
    map<int, int> count;

    for (size_t i = 0; i < v.size(); ++i)
        count[v[i]]++;

    vector<int> mode;
    int cnt = 0;
    for (map<int, int>::iterator it = count.begin(); it != count.end(); ++it) {
        if (it->second > cnt) {
            mode.clear();
            mode.push_back(it->first);
            cnt = it->second;
        } else if (it->second == cnt) {
            mode.push_back(it->first);
        }
    }

    if (mode.size() * cnt == v.size()) {
        cout << "No mode" << endl;
    } else {
        cout << "mode:";
        for (size_t i = 0; i < mode.size(); ++i)
            cout << ' ' << mode[i];
        cout << endl;
    }

    return 0;
}
gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
  • The TS asks why his code doesn't work. This doesn't answer his question. – n. m. could be an AI Nov 12 '13 at 04:42
  • @n.m Yeah. I was aware of that. I thought the OP doesn't actually understand what's the mode of an array. So I think it will help to give him a simplified example to figure out what's wrong in his code. – gongzhitaao Nov 12 '13 at 04:45
  • @user2981720 I think your code doesn't actually compute mode. Please refer to the definition of [mode](http://www.mathsteacher.com.au/year8/ch17_stat/02_mean/mean.htm#mode) and possible link [here](http://stackoverflow.com/questions/18177647/algorithm-to-compute-mode?lq=1) – gongzhitaao Nov 12 '13 at 04:53
0

This code uses "map" to find out the MODE from the given array. I hope this solution might help you.

int findMode(int * arr, int size)
{
    map<int, int> modeMap;
    sort(arr, arr + size);
    for (int i = 0; i < size; ++i) {
        ++modeMap[arr[i]];
    }

    auto x = std::max_element(modeMap.begin(), modeMap.end(),
        [](const pair<int, int>& a, const pair<int, int>& b) {
        return a.second < b.second; });

    return x->first;
}
oya163
  • 1,371
  • 2
  • 16
  • 20