Thread: "Jumping into C++" Chapter 5 Practice Problem

  1. #1
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20

    Angry "Jumping into C++" Chapter 5 Practice Problem

    Hi, I'm reading a book on C++ and one of the practice problems after the intro to loops reads:

    "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 3 inputs are the possible answers. The first answer is indicated by 1, the second by 2 and the third by 3. The answers are tallied until 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."

    I can create a program where the user types in a few numbers and bar graph which looks like this is created:

    Rugby(6): ||||||
    Soccer(3): |||
    Hockey(4): ||||

    Although this could maybe pass as a bar graph, it is not scaled to fit the screen no matter how many results are entered and I think it should look more like this:

    6 |
    5 |
    4 | |
    3 | | |
    2 | | |
    1 | | |
    Rugby Soccer Hockey

    I would really appreciate it if someone could tell me how I could properly complete the problem

    If it helps here is the code which I used to create the program which creates a bar graph which is not scaled to fit the screen:

    Code:
    
    
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main ()
    {
        int count1=0;
        int count2=0;
        int count3=0;
        int sport=0;
        int rugby=0;
        int soccer=0;
        int hockey=0;
    
    
        do
        {
            cout <<"1 - Rugby\n2 - Soccer\n3 - Hockey";
            cout <<"\n\n\nEnter your favourite (0 when finished): ";
            cin >>sport;
            cout <<"\n\n";
    
    
            if (sport == 1)
            {
                rugby++;
                count1++;
            }
            else if (sport == 2)
            {
                soccer++;
                count2++;
            }
            else if (sport == 3)
            {
                hockey++;
                count3++;
            }
            else
            {
                cout <<"Invalid sport\n\n\n";
            }
        }
        while (sport != 0);
    
    
          cout <<"\n\n\n\n\n\n\n\n\n\n\n\nRugby:  ";
          while (rugby > 0)
          {
              cout <<"|";
              rugby--;
          }
          cout <<"(" <<count1 <<")";
    
    
          cout <<"\nSoccer: ";
          while (soccer > 0)
          {
              cout <<"|";
              soccer--;
          }
          cout <<"(" <<count2 <<")";
    
    
          cout <<"\nHockey: ";
          while (hockey > 0)
          {
              cout <<"|";
              hockey--;
          }
          cout <<"(" <<count3 <<")";
          cout <<"\n\n\n\n\n\n\n\n";
    }
    PS I promise this is not homework, my school does not provide an option to learn C++

    Thanks a lot

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm not a C++ person, but since this is more of a logic problem, I'll take a crack at it.

    Your first example (horizontal bars), given enough votes, will hit the end of the console and break onto a new line. Your second example (vertical bars) doesn't seem to fit the bill either - too many votes will scroll the console. Both of these configurations break the "properly scaled to fit on your screen" criteria.

    I would think the expected solution was hinted at with the word "scaled." If you take less than twenty or so votes, you can print out the horizontal bar graph like you showed in your first example - one pipe (|) per vote. With a greater number of votes, each pipe (|) would have to represent a certain number (greater than one) of votes.

    In other words, the number of votes represented by each pipe (|) in the bar graph would be a function of the total quantity of overall votes. The more votes you have, the more each pipe (|) is worth, ensuring each bar is scaled to fit the screen.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Why not generate a 2d array/grid of bars and spaces and then print it out at once?
    Seems to be the easiest option to me.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I interpret the exercise as requesting a horizontal bar graph, scaled up or down to fit the line width.

    But for a vertical bar graph, besides using a 2D array, you can do it like this:

    Code:
    #include <iostream>
    #include <iomanip>
    
    int getMax(int *a, int len)
    {
        int i, max = a[0];
        for (i = 1; i < len; i++)
            if (a[i] > max)
                max = a[i];
        return max;
    }
    
    void barGraph(int *a, int len)
    {
        int max = getMax(a, len);
        for ( ; max > 0; max--)
        {
            for (int i = 0; i < len; i++)
            {
                char c = (a[i] >= max) ? '*' : ' ';
                std::cout << "  " << c;
            }
            std::cout << '\n';
        }
        for (int i = 0; i < len; i++)
            std::cout << ' ' << std::setw(2) << i;
        std::cout << '\n';
    }
    
    int main()
    {
        int a[] = {5, 8, 2, 12, 3, 4, 0, 1, 7}; // or use a vector!
        barGraph(a, sizeof(a)/sizeof(*a));
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Recommended reading for those still using C arrays and pointers: SourceForge.net: Safer arrays in Cpp - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  2. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM
  3. Replies: 18
    Last Post: 03-07-2013, 06:55 AM
  4. Jumping into C++ Chapter 5 problem 6 - Critique please
    By Kranky in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2012, 05:44 PM
  5. "uniqe" practice for 8 qeens problem
    By megazord in forum C Programming
    Replies: 21
    Last Post: 11-21-2009, 01:23 PM

Tags for this Thread