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)
).
You could give more details about your question. And is using which Toolkit? Qt? wxWidgets? WIN32?
– Lucas Lima
Codeblocks 13.12, WIN32, could you tell me what Qt means?
– user8184
Information about Qt: http://qt-project.org/
– Luiz Vieira
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.
– user8184
I didn’t understand the #part. Give more details.
– Lucas Lima
@Lucasnunes Probably the program is console, no Qt, etc, and # is used to make the graphic, ASCII art style.
– pepper_chico