### histogram help

i am writing a histogram and i am almost done i just need help with the final format of the astericks:

The longest bar always should contain 50 asterisks, and the other bars should be scaled proportionally. The sample input

450 401 3 78 444 200 -1

should look like:

 ``` 0- 99 2|********************************* 100-199 0| 200-299 1|***************** 300-399 0| 400-499 3|************************************************** 500+ 0|```

here is my code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include using namespace std; int main () { int a=0,b=0,c=0,d=0,e=0,f=0,i=0; while (cin>>i) { if (i>0 && i<100) a++; else if (i>=100 && i<200) b++; else if (i>=200 && i<300) c++; else if (i>=300 && i<400) d++; else if (i>=400 && i<500) e++; else if (i>=500) f++; else if (i == -1 ) break; } cout << setw (7) << "0-99" << setw (4) << a << "|"; for (int i=0;i

how should i do that??

Last edited on
You need to find the maximum value of a through f.
Then adjust the scale of the histogram by a factor of 50/max. i.e. change the number of asterisks printed by that factor.
i dont really know how to find the max of those numbers. do i need and array of some sort?

An array would be good if you had many values to deal with.
For just six, you can simply code it line by line.

First, declare an integer called max, and set it equal to a.
Then compare it with each of the other values, like this,
`if (b>max) max = b;` and do the same for c through f.
Last edited on
ok thanks i got that down but the scale of the histogram part still is knda confusing
well, this is what you have now:
 ``12`` `````` for (int i=0; i

Here the number of asterisks printed is `a`.
You just need to use `a * 50 / max`.
oh ok i was trying to repalce a with 50/max for some reason

thanks alot
Topic archived. No new replies allowed.