Question: This program uses the map template class to compute a histogram of positive numbers entered by the user. The map's key is the number that
This program uses the map template class to compute a histogram of positive numbers entered by the user. The map's key is the number that is entered, and the value will be a counter of the number of times the key has been entered so far. Note that it uses -1 as a sentinel value to signal the end of user input.
Your task is to complete the section of code that uses a constant iterator and traverses through the map outputting "the number ... appears ... times", where the (...) are the values from the map via the use of the iterator. Hint: recall that a map uses two reserved words in conjunction with iterators.
Follow the code below and write your code to output the histogram. You only need to write that piece of logic. The end of the program after your code is assumed.
#include
#include
using namespace std;
int main()
{
map
int num = 0;
cout << "Enter positive numbers. Enter -1 when done." << endl;
while (num != -1)
{
cin >> num;
if (num != -1)
{
// If this is not in the map, then add it with
// a value of 1
if (histogram.find(num) == histogram.end())
{
histogram[num] = 1;
}
else
{
// Already in the map, increment the mapped value
// by one
int count = histogram[num];
count ++;
histogram[num] = count;
}
}
}
// OUTPUT THE HISTOGRAM
// YOUR CODE HERE......
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
