Hi,Can I get some help writing this program as describe below: Write a program that uses the STL MAP template class to compute a histogram of positive numbers entered by the user.The map's key should be number that is entered and the value should be a counterof the number of times the key has been enterdd so far.Use -1 as a sentinel value to signal the end of user input.Thanks
How far have you got and where are you stuck? Do you understand the requirements, and how to use an STL map? Do you have any code yet?
here's how i started: for example, if the user input 5, 12, 3, 5,5, 3,2 -1 then the program should output the following : the number 3 occurs 2 times, the number 5 occurs 3 times and so on. here's my coding, please help finish it up #include <iostream> #include <map> #include <string> #include <algorithm> #include <cstdlib> #include <iomanip> using namespace std; int main() { map<int, int> histogram; int num=0; while(num>=0) { cout << " enter a positive number" ; cin >>num; histogram[num]++; }
Seems OK so far. Now you need to iterate over the map, extracting pairs of numbers. Do you understand how to do that? I'm guessing you've already been given some information on how to extract information from a map.
Code: /* Hi,Can I get some help writing this program as describe below: Write a program that uses the STL MAP template class to compute a histogram of positive numbers entered by the user.The map's key should be number that is entered and the value should be a counterof the number of times the key has been enterdd so far. Use -1 as a sentinel value to signal the end of user input.Thanks */ #include <cstdio> #include <map> using std::map; void makeHistogram() { printf("\nEnter a number, Press -1 to break\n"); map<int, int> histogramData; int num = 0; scanf("%d", &num); while(num >= 0) { map<int, int> ::iterator it; it = histogramData.find(num); if(it == histogramData.end()) { histogramData.insert(std::pair<int, int>(num, 1)); } else { it->second = it->second + 1; } scanf("%d", &num); } printf("\n\n histogram \n\n"); for(std::map<int, int>::iterator iter = histogramData.begin(); iter != histogramData.end(); iter++) { printf("%d-", iter->first); for(int j = 0; j < iter->second; j++) { printf("*"); } printf("\n"); } } int main(int argc, char* argv[]) { makeHistogram(); return 0; } Arrange the Histogram look N feel as per you taste or requirement!