Creating my own histogram using c

I want a function that takes in a array let's say
[ 4, 5, 6, 3, 4]

Then the output will be a histogram graph.

I am thinking of an way which use "|" and "_" to make the graph so the histogram will look something like below.
_
| |
| |
| |
| |

Anyone could suggest me an algorithm?
Check this out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <iostream>
#include <vector>


int main()
{
    char block = static_cast<char>(219);
    char space = ' ';
    std::vector<unsigned> histogram {4, 5, 6, 3, 4};
    unsigned max = *std::max_element(histogram.begin(), histogram.end());
    for(unsigned height = max; height != 0; --height) {
        for(unsigned value: histogram) {
            std::cout << (value >= height? block : space);
        }
        std::cout << '\n';
    }
}
  █
 ██
███ █
█████
█████
█████
*Note that on different systems there might be something else in char range 128-255. If it shows garbage for you, replace block char with '|'
Topic archived. No new replies allowed.