Home > other >  Jumping into C : Ch 5 Problem 7 using vertical bar graph
Jumping into C : Ch 5 Problem 7 using vertical bar graph

Time:11-03

I am in need of assistance to create an vertical bar graph with required limitations of learning experience. Such as, using only the fundamental basics listed: if statement, boolean, loop, string, arithmetic and comparison operators. Basically giving an idea of how limited my experience is in the language for clarification. Now, I have completed this problem already but the output does not seem to look like a desirable graph in my opinion so this is why posting. I will provide the problem and valid code I used to complete this problem.

  1. Write a program that provides the option of tallying up the results of a poll with 3 possible values. The first input to the program is the poll question; the next three inputs are the possible answers. The first answer is indicated by 1, the second by 2, the third by 3. The answers are tallied until a 0 is entered. 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.
#include<iostream>

int main()
{
    bool poll = false;
    int Tech = 0, Edu = 0, Agri = 0;

    std::cout << "VOTING POLL" << std::endl;
    std::cout << "----------------------------" << std::endl;
    std::cout << "----------------------------" << std::endl;

    
    while(!poll)
    {
        std::cout << "Of the year 2022, in the United States of America, what should be the top priority of our concerns?" << std::endl;
        std::cout << " 1.Technology\n 2.Education\n 3.Agriculture" << std::endl;
        std::cout << "\n";
        
        int pollAnswer;
        std::cin >> pollAnswer;
        
        if(pollAnswer == 1)
        {
            Tech  ;
        }
        else if(pollAnswer == 2)
        {
            Edu  ;
        }
        else if(pollAnswer == 3)
        {
            Agri  ;
        }
        else if(pollAnswer == 0)
        {      
            poll = true;
        }
        else
        {
            std::cout << "\n" << "Invalid. Please choose an answer from the above listing." << std::endl;
        }
    }
        std::cout << "Technology: ";
        while(Tech > 0)
        {
            std::cout << "* ";
            Tech--;
        }
        std::cout << std::endl;
        
        std::cout << "Education: ";
        while(Edu > 0)
        {
            std::cout << "# ";
            Edu--;
        }
        std::cout << std::endl;
        
        std::cout << "Agriculture: ";
        while(Agri > 0)
        {
            std::cout << "| ";
            Agri--;
        }
}

CodePudding user response:

  • I think the first thing to do would be to make sure all the labels take up the same amount of space, so the bars in the graph all start from the same location on the left.

  • The text suggests scaling the graph to fit the screen. To do that we probably want to take the largest of the three counts, and figure out what to multiply that by so it'll be close to (but not greater than) the screen width (e.g., 80 columns), then multiply all three values by that scale factor.

  • I wouldn't use spaces in the bars, so each bar would look more "solid".

Possible result:

 Technology: ****************************************
  Education: ##########################
Agriculture: ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

CodePudding user response:

using only the fundamental basics listed: if statement, boolean, loop, string, arithmetic and comparison operators.

Those are enough to write a sort of scanline algorithm:

  • Establish a maximum height for the bars and scale all the (three) values accordingly. This isn't mentioned in your requirements, but it seems better to avoid too high (or too low) bars.

  • for each line of output (you may initialize a loop variable row at max_height and decrease it down to 0):

    • print some spaces, so that you can add the labels after this loop.
    • if the value of the first scaled variable (say, Tech) is less than or equal to row, print a ' ' (a space) otherwise print the character chosen to represent that bar ('*', I think).
    • repeat for all the other bars and then add a '\n' (newline) to end this row.
  • Print the labels, like "Technology" and the likes, correctly spaced.

I'll leave all the details of the implementation to the reader.

  •  Tags:  
  • c
  • Related