Thread: vertical bar graph

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    19

    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.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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.
    Last edited by kermit; 09-01-2004 at 04:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error help making no sense
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2007, 07:55 PM
  2. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  3. Bar graph
    By robjules in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2001, 10:55 PM
  4. Horizontal bar Graph?
    By robjules in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2001, 04:55 PM
  5. I need help with my vertical bar graph program.
    By smokedragon in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 05:01 PM