# vertical bar graph

• 09-01-2004
n00by
vertical bar graph
hello,
how can i print out a bar graph like this:
Code:

```          *   *    *  *    *   *  *  *  *  *  *   *  *  *  *  *  *   *  *  *  *  *  *   0  1  2  3  4  5```
instead of like this:
Code:

```0****** 1************ 2****************** 3************************* 4***************```
you don't have to give me the code, just help me out. thanks.
• 09-01-2004
Hammer
Think the problem through logically, and write your ideas down on paper. Try to write a step by step flow first, then work on turning that into code.
• 09-01-2004
kermit
Well, assuming that whatever values the histogram is illustrating is stored in an array, one way would be to run through the array finding the element with the highest count. Then once you have that number, you can start printing your histogram. Compare the high count with each of the values in the other array elements, and if their value is greater than (on the first run it won't be) or equal to the high count, print an asterisk - otherwise print a space. That will give you the 'top' line of the histogram. Then you want to decrement the high count that you obtained at the start, and do the test again.

pseudo...

Code:

```find the element with the highest_value while it is not equal to 0     check against each array element         is the array element greater than or equal to highest_value?               if yes, print an asterisk               if no, print a space   decrement highest_value```
Sorry about the poor pseudo-code - some of the guys on here can do a really nice job of it. Hopefully you get the idea though

~/

::edit

By the way, that is just one way to do it - there are more complicated versions, so put on your thinking cap and see what you can come up with.