Creating a histogram with C++ (Homework)

13,386

Try something along the lines of this:

  1. Determine the largest number in the histogram
  2. Using a loop like this to construct the histogram:

    for(int i = largest; i >= 1; i--)

    Inside the body of the loop, do steps 3 to 5 inclusive

  3. If i <= value_of_column_a then print a *, otherwise print a space

  4. Repeat step 3 for each column (or write a loop...)

  5. Print a newline character

  6. Print the horizontal line using -

  7. Print the column labels

Share:
13,386
Lindsiria
Author by

Lindsiria

Updated on June 04, 2022

Comments

  • Lindsiria
    Lindsiria almost 2 years

    In my c++ class, we got assigned pairs. Normally I can come up with an effective algorithm quite easily, this time I cannot figure out how to do this to save my life.

    What I am looking for is someone to explain an algorithm (or just give me tips on what would work) in order to get this done. I'm still at the planning stage and want to get this code done on my own in order to learn. I just need a little help to get there.

    We have to create histograms based on a 4 or 5 integer input. It is supposed to look something like this:

    Calling histo(5, 4, 6, 2) should produce output that appears like:
    
            *
        *   *
        * * *
        * * *
        * * * *
        * * * *
        -------
        A B C D
    

    The formatting to this is just killing me. What makes it worse is that we cannot use any type of arrays or "advanced" sorting systems using other libraries.

    At first I thought I could arrange the values from highest to lowest order. But then I realized I did not know how to do this without using the sort function and I was not sure how to go on from there.

    Kudos for anyone who could help me get started on this assignment. :)