Click here to Skip to main content
15,885,989 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a program for building histograms. Input data format: the number of statistical measurements, then the results themselves. Output data format: interval, number of results per interval. The data is output in the order of the first occurrence of the measurement result.. 
Input stream

8
 7 5 7 4 1 5 6 1

Output stream
7 2
5 2
4 1
1 2
6 1


What I have tried:

I tried to do this question without loops but doesn't work.
Posted
Updated 20-Sep-22 9:59am
Comments
Richard MacCutchan 20-Sep-22 12:15pm    
You need to show your code and explain which part does not work. We cannot help you without full details of your problem.
Dave Kreskowiak 20-Sep-22 13:35pm    
Why would you do this without loops? It's not part of the problem description.
Rick York 20-Sep-22 21:17pm    
The only reason I can think of is that doing it without loops would force them to use STL algorithms.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
One possibility would be to write the input data into a vector
C++
vector <int> mdat = { 7, 5, 7, 4, 1, 5, 6, 1 };

And manage the histogram as a result in a pair:
C++
vector <pair<int, int>> hist;

Then it is possible to loop through the vector mdat and use it with
a find_if() query to check if the current value is already contained.
If it is not contained you can append the value as a new pair:
C++
hist.push_back(make_pair(val, 1));

If it is already included, you can simply increment the second value.
 
Share this answer
 
Comments
CPallini 21-Sep-22 3:08am    
5.Note you can satisfy the requirements in four lines of code, using a set and a multiset.
merano99 21-Sep-22 3:41am    
Thanks!
merano99 21-Sep-22 3:44am    
Wouldn't a std::set sort what was explicitly not wanted here?
CPallini 21-Sep-22 3:52am    
Why?
merano99 21-Sep-22 4:51am    
The requirement states: "The data is output in the order of the first occurrence of the measurement result.. ". Additional effort in data management and output would probably be necessary for a set.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900