1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Stl map

Discussion in 'C++' started by sakpase, Mar 19, 2012.

  1. sakpase

    sakpase New Member

    Joined:
    Mar 19, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. sakpase

    sakpase New Member

    Joined:
    Mar 19, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Data structure using C++
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  4. sakpase

    sakpase New Member

    Joined:
    Mar 19, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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]++;
    }
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  6. dearvivekkumar

    dearvivekkumar New Member

    Joined:
    Feb 21, 2012
    Messages:
    29
    Likes Received:
    5
    Trophy Points:
    0
    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!
     
    sakpase likes this.

Share This Page