Creating a bar graph that shows results properly scaled

This is another question from 'Jumping into C++' by Alex Allain. Specifically, part of a practice problem 7 from the end of Chapter 5.

It reads "The program should then show the results of the poll—try making a
bar graph that shows the results properly scaled to fit on your screen no matter how many results were entered."

I didn't want to inconvenience anybody with a long wall of text so I'm omitting the part of the program that introduces the poll question and tallies the votes for each answer option. That part is working fine.

Here is my attempt at graph making :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
for ( int sum1g = sum1; sum1g > 0; sum1g-- ) /* uses value stored in sum1 after while loop is terminated to initialize a new variable sum1g.  
                                                Essentially recounts the votes for option 1 and displays a * for each vote */
    {
        cout << "*\n";
    }

cout << '\n';
cout << "B\n";

for ( int sum2g = sum2; sum2g > 0; sum2g-- )
    {
        cout << "*\n";
    }

cout << '\n';
cout << "P\n";

for ( int sum3g = sum3; sum3g > 0; sum3g-- )
    {
        cout << "*\n";
    }

cout << '\n';
cout << "C\n";


This gives me something sorta kinda resembling what I want.. a vertical column of asterisks denoting the number of votes for each option. However, they're just stacked one after another and for a true 'bar graph' I need them to be side by side.


*
*
*
*

B
*
*

P
*
*
*

C


^ How the output looks now


*   
*
*       *
*   *   *
*   *   *

B   P   C


^ How I'd like it to be

I have a sneaking suspicion nested loops might be the answer.. but I'm not totally sure. I've tried nesting the three for loops I showed above but that just made a big mess x)

Once the graph displays properly, I'm also not sure how to ensure a graph scales to fit the screen no matter how many asterisks (votes) are tallied.

I apologize if anything about my post is confusing.. just tried to keep the length down (and still failed). I can of course post the entire program code if need be. Thanks so much for any help you might be able to provide!
Last edited on
How does this look
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int sum = (sum1 > sum2) ? sum1 : sum2;
sum = (sum > sum3) ? sum : sum3;

for(int i = sum; i > 0; i--)
{
    if(i <= sum1)   // B
        cout << "*";
    else
        cout << " ";

    cout << " ";
    if(i <= sum2)   // P
        cout << "*";
    else
        cout << " ";

    cout << " ";
    if(i <= sum3)   // C
        cout << "*";
    else
        cout << " ";
    cout << endl;
}

cout << endl << "B P C" << endl;
That does indeed fix it. Unfortunately it's using notation that the book hasn't taught me yet so I'm pretty thoroughly confused by it haha. Could you explain a little bit about what's going on there?

Namely the

1
2
int sum = (sum1 > sum2) ? sum1 : sum2;
sum = (sum > sum3) ? sum : sum3;


part.
Last edited on
which part has not been taught yet?
What about this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int sum = sum1;

if(sum <= sum2)
    sum = sum2;

if(sum <= sum3)
    sum = sum3;

for(int i = sum; i > 0; i--)
{
    if(i <= sum1)   // B
        cout << "*";
    else
        cout << " ";

    cout << " ";
    if(i <= sum2)   // P
        cout << "*";
    else
        cout << " ";

    cout << " ";
    if(i <= sum3)   // C
        cout << "*";
    else
        cout << " ";
    cout << endl;
}

cout << endl << "B P C" << endl;
I think I get that version..

Declare a new variable sum and initialize it as the value of sum1 (which comes from my earlier while loop).

Use if statements to set sum to the option with the largest amount of votes if necessary..

Then the for loop checks i (sum) against the vote totals and outputs the appropriate amount of asterisks for each option.

Something along those lines?

I can't believe how difficult it is as a beginner to understand code that you yourself don't write. Whew.
Yes, you got it correct :)
Topic archived. No new replies allowed.