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).
// 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";
}
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.
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:
// 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.