Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Stl map (http://www.go4expert.com/forums/stl-map-t28057/)

sakpase 19Mar2012 21:51

Stl map
 
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

sakpase 19Mar2012 21:59

Re: Stl map
 
Data structure using C++

xpi0t0s 20Mar2012 03:19

Re: Stl map
 
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?

sakpase 20Mar2012 05:50

Re: Stl map
 
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]++;
}

xpi0t0s 20Mar2012 06:06

Re: Stl map
 
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.

dearvivekkumar 20Mar2012 11:25

Re: Stl 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!


All times are GMT +5.5. The time now is 03:32.