Question:
create a program that finds the mode (c++)?
yourvp08
2009-02-10 12:16:28 UTC
So I have to write a program that finds the mode from a set of positive integers. I really have no idea where to start.Here is what I have:

#include "std_lib_facilities.h"
int main()
{

int num;

cout<<"please enter a series of positive integers\n"
cin>>num;



keep_window_open()
}


I know the mode is the number used most often, so if I wear to input say 2-4-8-2-9-2-5-2 , the mode would be 2. My bottom question is idk how to tell the computer i want it to keep track of how many times a certain number is inputed.
Six answers:
cja
2009-02-11 04:53:03 UTC
There are many options for implementation, but I think however you do it the algorithm to use is the same:

    - get all input

    - sort the data set

    - iterate through data, creating a list of

      numbers with their frequencies,

      where the frequency is > 1

    - sort the frequency list

    - get the mode from the frequency list - the

      value(s) at the front of the list

      with the greatest frequency.



Here's one way to implement it:



#include

#include

#include

#include

#include



using namespace std;



class Freq {

  public:

    Freq() : _val(0), _freq(0) { }

    Freq(int v, int f) : _val(v), _freq(f) { }

    inline int val() { return _val; }

    inline int freq() { return _freq; }

    bool operator() (const Freq &f1, const Freq &f2) {

        return f1._freq < f2._freq;

    }

  private:

    int _val;

    int _freq;

};



int main(int argc, char *argv[]) {

    vector intVec;

    int n;

    string line;

    stringstream *ss;

    bool inputDone = false;



    //

    // Fill vector of integers, then sort it.

    //

    cout << "Enter integers, one or more per line, " << endl;

    cout << "space separated (blank line to quit) :" << endl;

    while (inputDone == false) {

        getline(cin,line);

        if (line.length() > 0) {

            ss = new stringstream(line);

            while ((*ss) >> n) {

                intVec.push_back(n);

            }

        } else {

            inputDone = true;

        }

    }

    sort(intVec.begin(),intVec.end());



    //

    // Get frequencies, then sort them.

    //

    vector freqVec;

    Freq FreqObj;

    int cnt,x;



    vector::iterator i = intVec.begin();

    while (i != intVec.end()) {

        x = *i++; cnt = 1;

        while ((i != intVec.end()) && (x == *i)) {

            ++cnt; ++i;

        }

        if (cnt > 1) {

            freqVec.push_back(Freq(x,cnt));

        }

    }

    sort(freqVec.begin(),freqVec.end(),FreqObj);



    //

    // Display mode value(s), and number of occurrences

    //

    vector::iterator j = freqVec.begin();

    if (freqVec.size() > 0) {

        x = j->freq();

        cout << "mode = " << (j++)->val();

        while ((j != freqVec.end()) && (j->freq() == x)) {

            cout << ", " << (j++)->val();

        }

        cout << " (" << x << " occurrences)" << endl;

    } else {

        cout << "no mode" << endl;

    }

    return 0;

}



Since sorting isn't the main objective of this exercise, I took the liberty of using the sort function from .





Sample runs:



Enter integers, one or more per line,

space separated (blank line to quit) :

5 4 11 2 11 2



mode = 2, 11 (2 occurrences)



Enter integers, one or more per line,

space separated (blank line to quit) :

1 2 3 4 5



no mode



Enter integers, one or more per line,

space separated (blank line to quit) :

10 9 8 8 7 8 6 5



mode = 8 (3 occurrences)
jerry
2016-05-24 04:20:33 UTC
Go to the library and browse a book on Statistics, find the formulas for Mean, Median & Mode. Convert the formulas to C-code. A programmer always needs to understand at least two fields, at the EXPERT level. 1) his programming language, and 2) the field of his 'user community'. It might be Statistics, Accounting, Engineering. My point is: Major in Programming, and minor in another field. The field of your desired 'user community'. Sounds like a lot more work, but it will get you a better job with better starting salary, when you put it on your resume.
[ J ] a [ Y ]
2009-02-10 12:42:10 UTC
i wouldnt store all the values in one variable. This can result in memory leaks if the total value is over 37767. What I would do is this:



int values;



cout << "please enter how many values you need to check against\n";



cin >> values;



int val[values];



for(int i = 0; i<= values; i++)

{

cout << "enter value " << i << endl;

cin >> val[i];

}



//then you want to search your newly filled array for the number that occurs the most. You can do this a number of ways, depending on how you build your system. For simplicity, its best to restrict the numbers users can enter to below 10 for example (put in bounds checking above for that). Then for j = 0 to 10, search how many times this value occurs in your array.



Something like that. Also if you get an error trying to make an associative array (i cant remember if that was supported), you can use a vector for that.
anonymous
2009-02-10 12:35:47 UTC
My suggestion would be to use the "map" STL container with the key being the integer, and the value being the count of that integer.



For each number entered{

if it doesn't exist in the map: add it with a value of one

if it does exist: increment its value.

}

When all of the integers have been added, iterate through the map, find the highest value, and return its key.
Shahdoost
2009-02-10 12:59:59 UTC
if u mean the number that repeated more that the others u need to sort the list & after that count them



maybe below code helps u(it's not optimized so u can write better)
Kishore
2009-02-10 13:01:48 UTC
include "std_lib_facilities.h"

int main()

{



int num;

int zero, one, two, three, four, five, six, seven, eight, nine;

zero=0;

one=0;

two=0;

three=0;

four=0;

five=0;

six=0;

seven=0;

eight=0;

nine=0;



cout<<"please enter a series of positive integers\n"

cin>>num;



switch(num)

{

case 0:

zero++;

break;



case 1:

one++;

break;



case 2:

two++;

break;



case 3:

three++;

break;



case 4:

four++;

break;



case 5:

five++;

break;



case 6:

six++;

break;



case 7:

seven++;

break;



case 8:

eight++;

break;



case 9:

nine++;

break;

}



keep_window_open()



cout << " 0 entered << zero << "times" + '\n';

cout << " 1 entered << one << "times" + '\n';

cout << " 2 entered << two << "times" + '\n';

cout << " 3 entered << three << "times" + '\n';

cout << " 4 entered << four << "times" + '\n';

cout << " 5 entered << five << "times" + '\n';

cout << " 6 entered << six << "times" + '\n';

cout << " 7 entered << seven << "times" + '\n';

cout << " 8 entered << eight << "times" + '\n';

cout << " 9 entered << nine << "times" + '\n';

}



May be the example given above is little awkward but I don't have better and fast example than this in my mind


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...