Finding the mode of a series of numbers - I'm trying to use std::count()

I'm trying to use std::count() for it, but I can't seem to get it to work. That and I don't know how to let the user stop entering numbers and then print the mode both when I do it all in main and also when I calculate the mode in another function (when I was trying to do it another function, the mode wouldn't be printed at all).

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// chap4_ex16.cpp : Defines the entry point for the console application.
// Osman Zakir
// 10 / 25 / 2016
// Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Exercise 16
// Program to find the mode of a sequence of numbers

#include "std_lib_facilities.h"

int main()
{
	cout << "Keep entering numbers; to stop, enter a \'|\' or \"Ctrl+Z\":\n";
	vector<int> numbers;
	int number;
	int mode;
	while (cin >> number)
	{
		cin.ignore(32767, '\n');
		numbers.push_back(number);
		mode = count(numbers.begin(), numbers.end(), number);
		cout << "The mode is " << mode << "\n";
	}
	cout << "The mode is " << mode << "\n";
}
Why are you using cin.ignore()?
cin.ignore(32767, '\n');
To clear the input buffer so that the program waits for input before exiting. Why? Could that be causing part of the problem?
The line is not needed.
If you really want the program to wait for an input, use std::cin.get().

1
2
3
4
5
6
7
while (cin >> number)
{
	cin.ignore(32767, '\n');
	numbers.push_back(number);
	mode = count(numbers.begin(), numbers.end(), number);
	cout << "The mode is " << mode << "\n";
}


Should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
number = 0;
while (number != -11)
{
	cout << "Enter a number (-11 to quit) : ";
 
	if(!(cin >> number)) 
	{
		cin.clear(); cin.ignore(1000, '\n');
		cout << "Invalid number. Try again.\n\n"; continue;
	}

	numbers.push_back(number);
	mode = count(numbers.begin(), numbers.end(), number);
	cout << "The mode of " << number << " is " << mode << endl << endl;
}
Last edited on
Neither you (the OP) nor Sakurawhatever understand what "mode" is. The mode is the most frequently occurring value. Thus, to print: cout << "The mode of " << number << " is " << mode << endl << endl; makes no sense. A number does not have a mode. A particular number in the list is the mode.

What you need to do is first fill up the vector with numbers entered by the user. This will be done in a loop of course.

Then, in a separate loop (or separate block of code), you can employ a method to find the most frequently occurring value. You can do this by either using a nested for loop and keeping track of the most occurring value, or by sorting the vector, and then counting which number occurs most often by looping through the vector once.

If you have any questions about the above method, post here.
Last edited on
So I can't really use that function to find the actual mode? What should I do to find the mode, then?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
number = 0;
while (number != -11)
{
	cout << "Enter a number (-11 to quit) : ";
 
	if(!(cin >> number)) 
	{
		cin.clear(); cin.ignore(1000, '\n');
		cout << "Invalid number. Try again.\n\n"; continue;
	}

	numbers.push_back(number); cout << endl;
}

int i;
int mode = numbers[0];
for(i = 1; i < numbers.size(); i++)
{
    if(count(numbers.begin(), numbers.end(), numbers[i]) > count(numbers.begin(), numbers.end(), mode)) mode = numbers[i];
}

cout << "The mode number is " << mode << endl;
Last edited on
I would suggest ending the while loop if a non-number is entered.

-11 is not a good ending condition, as that could be a number the user wants included in the list.
@Arslanwhatever
I will use your suggestion, but don't expect a 'thank you' from me.

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
bool bQuit = false;
while (bQuit == false)
{
	cout << "Enter a number (non-number to quit) : ";
 
	if(!(cin >> number)) 
	{
		bQuit = true;
	}
	else
	{
		numbers.push_back(number); 
	}

	cout << endl;
}

int i;
int mode = numbers[0];
for(i = 1; i < numbers.size(); i++)
{
    if(count(numbers.begin(), numbers.end(), numbers[i]) > count(numbers.begin(), numbers.end(), mode)) mode = numbers[i];
}

cout << "The mode number is " << mode << endl;
Last edited on
Just a condition of while (cin >> numbers) works just fine. No need to do anything else, since even with just this much, the loop will stop once anything that isn't a number is entered. Aside from that, yeah, thanks for the help. I got it to work:

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
// chap4_ex16.cpp : Defines the entry point for the console application.
// Osman Zakir
// 10 / 25 / 2016
// Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Exercise 16
// Program to find the mode of a sequence of numbers

#include "std_lib_facilities.h"

int find_mode(const vector<int>& numbers);

int main()
{
	cout << "Keep entering numbers; to stop, enter anything that isn't a number\n";
	vector<int> numbers;
	int number;
	while (cin >> number)
	{
		numbers.push_back(number);
	}
	sort(numbers);
	int mode = find_mode(numbers);
	cout << "The mode is " << mode << "\n";
}

int find_mode(const vector<int>& numbers)
{
	int mode = numbers[0];
	for (int i = 0; static_cast<size_t>(i) < numbers.size(); ++i)
	{
		if (count(numbers.begin(), numbers.end(), numbers[i]) > count(numbers.begin(), numbers.end(), mode))
		{
			mode = numbers[i];
		}
	}
	return mode;
}
Just a condition of while (cin >> numbers) works just fine. No need to do anything else, since even with just this much, the loop will stop once anything that isn't a number is entered.


You are a person of understanding :)

Just one thing: I noticed you call a sort function (line 21). If the vector is sorted, you don't need to use std::count anymore. That would be inefficient as its just another loop. Instead you can take advantage of the fact that all numbers of equal value are next to each other, and simply loop through the vector once to find the longest length of same numbers.

This method results in a better time complexity.

Last edited on
for (int i = 0; static_cast<size_t>(i) < numbers.size(); ++i)

Should be :
for (int i = 1; static_cast<size_t>(i) < numbers.size(); ++i)
So something like this:

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
#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
    std::vector<int> numbers;
    int number = 0;
    while (std::cin >> number) numbers.push_back(number);

    std::sort(numbers.begin(), numbers.end() );

    int mode = 0, mode_count = 0, cnt = 1;
    
    for(int i = 0; i < numbers.size()-1; i++)
    {
        if(numbers[i] == numbers[i+1]) cnt++;
        else
        {
            if(cnt > mode_count)
            {
                mode_count = cnt;
                mode = numbers[i];
            }
            cnt = 1;
        }
    }

    std::cout << "The mode number is " << mode << " occurring " << mode_count << " times\n";
}
Last edited on
Topic archived. No new replies allowed.