A die is rolled n times. Each time, a random value has been inserted into a vector. This vector then is used to generate a sorted map (wrongly called hashmap in the code) that is later used to create an ASCII histogram.
The histogram, for example, looks like this:
10
#
#
7 #
# #
# # 5
# # #
# 3 # #
# # # #
# # # 1 #
# # # # #
-----------
1 2 3 4 5 6
And here's the code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <random>
#include <map>
std::vector<int> roll(int times)
{
std::vector<int> rand;
while (times > 0)
{
std::random_device seeder;
std::mt19937 engine(seeder());
std::uniform_int_distribution<int> dist(1, 6);
rand.push_back(dist(engine));
--times;
}
return rand;
}
std::map<int, int> histogram_calculate(int times)
{
std::vector<int> random_numbers = roll(times);
std::map<int, int> cnt_hashmap;
auto max_element = 6;
for (int i = 1; i <= max_element; ++i)
{
cnt_hashmap[i] = 0;
}
for (auto iter = random_numbers.begin(); iter != random_numbers.end(); ++iter)
{
cnt_hashmap[*iter] += 1;
}
return cnt_hashmap;
}
std::string histogram_draw(int times)
{
std::vector<std::string> ret_vec;
std::map<int, int> histogram = histogram_calculate(times);
for (int i = 1; i <= histogram.size(); ++i)
{
std::string to_add = "";
if (histogram[i] > 0)
{
to_add = std::to_string(histogram[i]);
std::string column = "\n";
int j = 0;
while (j <= histogram[i])
{
column += "#";
column += "\n";
++j;
}
to_add += column;
}
to_add += "--";
to_add += std::to_string(i);
ret_vec.push_back(to_add);
}
std::string finalize = "";
for (auto &str : ret_vec)
{
finalize += str;
}
return finalize;
}
int main() {
std::cout << histogram_draw(10) << std::endl;
return 0;
}
There's three functions:
- One to fill the random value vector
- One to sort out the histogram.
- One to display the histogram.
That's about it.