How to make a vertical bar graph with the c+# character?

Asked

Viewed 1,343 times

1

Performing a C++ job here at the college:

How to carry out a vertical graph in bars, that is, from the bottom to the top, of some vector.

Suppose there is a vector with some positions, for example v[3], and each position is a user that contains a value. The graph should contain that amount.

Assuming that V[0] has 5, V[1] has 6 and V[2] has 4. The total is 15, then the graph should be proportional, that is, 5 is 33.33% and successively.

  • 2

    You could give more details about your question. And is using which Toolkit? Qt? wxWidgets? WIN32?

  • Codeblocks 13.12, WIN32, could you tell me what Qt means?

  • 1

    Information about Qt: http://qt-project.org/

  • It is not Qt no, it is a simple program, the teacher will enter the data inside the vectors and the graph will appear soon afterwards, vertically (the most important part), with the percentage of each vector position.

  • I didn’t understand the #part. Give more details.

  • 2

    @Lucasnunes Probably the program is console, no Qt, etc, and # is used to make the graphic, ASCII art style.

Show 1 more comment

1 answer

4

If I understand correctly, you want to draw N bars each with height Hk. As the output is horizontal you have to go drawing a piece of each bar at a time before skipping the line. Then first find the maximum element (the highest bar) and then do this number of iterations. In each draw the slider if its value is greater than or equal to the current iteration.

Here’s a simple code that does this:

#include <iostream>
#include <vector>
#include <algorithm>

void printBottomUpBarChart(const std::vector<int>& levels) {
    int highest = *std::max_element(levels.begin(), levels.end());
    for (int level = highest; level > 0; --level) {
        for (unsigned i = 0; i < levels.size(); ++i) {
            if (levels[i] >= level)
                std::cout << " #";
            else
                std::cout << "  ";
        }
        std::cout << std::endl;
    }
}

An example of use:

int main()
{
    std::vector<int> lvs;
    lvs.push_back(3);
    lvs.push_back(8);
    lvs.push_back(1);
    lvs.push_back(2);
    lvs.push_back(0);
    lvs.push_back(5);
    printBottomUpBarChart(lvs);
    return 0;
}

And the output:

   #        
   #        
   #        
   #       #
   #       #
 # #       #
 # #   #   #
 # # # #   #

If you have other values and want to draw the graph by being proportional to those values but not using them for the number of #, scale the vector before moving to the function. I suggest rounding up always (std::ceil(1.0 * x * targetHeight / actualHeight)).

  • 2

    Instead of # I think you can use it, too (219) and (220), hence can increase the resolution of the graph.

  • I also thought about it, but the teacher asked in #, I did not understand the reason.

  • @Guilherme Bernal, thank you very much, helped me a lot.

  • @user8184 should have accepted as answer then.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.