Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses the map template class to compute a histogram of posit

ID: 3634921 • Letter: W

Question


Write a program that uses the map template class to compute a histogram of positive numbers entered by the user. The map’s key should be the number that is entered, and the value should be a counter of the number of times the key has been entered so far. Use -1 as a sentinel value to signal the end of userinput. For example, if the user inputs:
7
14
3
7
7
3
29
-1
Then the program should output the following (not necessarily in this order):
The number 3 occurs 2 times.
The number 7 occurs 3 times.
The number 14 occurs 1 times.
The number 29 occurs 1 times.

Explanation / Answer

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
map<int, int> histogram;
int a=0;
while(a>=0)
{
cout << " enter value of a" ;
cin >>a;
histogram[a]++;
}
map<int,int>::iterator it;
for ( it=histogram.begin() ; it != histogram.end(); it++ )
{
cout << (*it).first << " occurs " << setw(3) << (*it).second << (((*it).second == 1) ? " time." : " times.") <<endl;
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote