How to Calculate the Mode?

For my comp sci class i need to fix code i made for a program that calculates the mode of a specified amount of numbers and spits it out at the end.I have my program working to find the mode of numbers that occur more than the others such as and input 3 3 3 1 will return 3 but if I put in say 2 2 4 4 the program will only spit out 4 instead of 2,4. Help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 int main () {
    int amount;
    int temp;
    int num;
    vector <int> vectorToSort;
    cout << "How many integers would you like to enter?" << endl;
    cin >> amount;
cout << "Please enter as many numbers from 1-20 as you would like." << endl;

for (int i = 0; i < amount; i++) {
    cin >> num;
    vectorToSort.push_back (num);
    cout << endl;
}

for (int i =0; i < vectorToSort.size(); i++) {
 for (int j=0; j < vectorToSort.size()-1-i; j++) {
    if (vectorToSort[j] > vectorToSort[j+1]) {
        temp = vectorToSort [j];
        vectorToSort [j] = vectorToSort[j+1];
        vectorToSort [j+1] = temp;
    }
 }
}
int counter = 1;
int max = 0;
int mode = vectorToSort [0];
 for (int pass = 0; pass < vectorToSort.size() - 1; pass++)
        {
           if ( vectorToSort [pass] == vectorToSort [pass+1] )
           {
              counter++;
              if ( counter > max )
              {
                  max = counter;
                  mode = vectorToSort[pass];
              }
           } else
              counter = 1;
        }
    cout << "The mode is: " << mode << endl;
}
closed account (2UD8vCM9)
You have to store the # of occurrences for each number because there can be more than one mode.

Instead of
cout << "The mode is: " << mode << endl;

Try something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
	vector<int> Number; //Vector to store each unique #
	vector<int> NumberCount; //Vector to store each unique #'s occurence count

	for (int i = 0; i < vectorToSort.size(); i++) //For each entered number
	{
		if (Number.size() == 0) //If we have no numbers in our vector
		{
			Number.push_back(vectorToSort[i]); //Add the first number to the vector
			NumberCount.push_back(1); //Initialize count to 1
		}
		else //If we have numbers in our vector
		{
			if (Number[Number.size() - 1] == vectorToSort[i]) //If this number has already been added to the vector
			{
				NumberCount[Number.size() - 1] += 1; //Add 1 to # of occurrences
			}
			else //If this number has not been added to the vector already
			{
				Number.push_back(vectorToSort[i]); //Add number to vector
				NumberCount.push_back(1); //Initialize Count to 1
			}
		}
	}

	int HighestOccurences = 0; //Stores the highest # of occurences for any unique #
	for (int i = 0; i < NumberCount.size(); i++)
	{
		if (NumberCount[i] > HighestOccurences) //If # count is greater than stored highest occurences
		{
			HighestOccurences = NumberCount[i]; //Update highest # of occurences
		}
	}

	//Now go through and remove all numbers that don't meet the highest # of occurences since they wont be part of the mode
	for (int i = NumberCount.size() - 1; i >= 0; i--) //For each item in vector(s) Number & Number Count
	{
		if (NumberCount[i] < HighestOccurences) //If number does not have highest # of occurrences
		{
			Number.erase(Number.begin() + i); //Erase from number vector
			NumberCount.erase(NumberCount.begin() + i); //Erase from number count vector
		}
	}

	if (Number.size() == 1) //If only one mode
	{
		cout << "The mode is: " << Number[0] << endl;
	}
	else //If more than one mode
	{
		cout << "The modes are: " << Number[0];
		for (int i = 1; i < Number.size(); i++)
		{
			cout << ", " << Number[i];
		}
		cout << endl;
	}
Topic archived. No new replies allowed.