#include #include "text.h" static void find_max(const std::vector& numbers, std::size_t& max) { max = numbers[0]; for(double element : numbers) { if(element > max) {max = element;} } } void show_histogram_text(const std::vector& bins){ std::size_t max_bin; find_max(bins, max_bin); double modifier; /* * In case when maximum bin > 76, histogram will be scaled according to * the formula "max_asterisk * (static_cast(bins[i]) / max_bin)" * or "bins[i] * (static_cast(max_asterisk) / max_bin)". * In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number. */ if(max_bin > 76) {modifier = static_cast(74) / (max_bin);} else {modifier = 1;} for(std::size_t i = 0; i < bins.size(); ++i) //Histogram output with alignment, if necessary. { if(bins[i] >= 10) { if(bins[i] >= 100) {std::cout << bins[i] << '|';} //Output a three-digit number. else {std::cout << ' ' << bins[i] << '|';} //Output a two-digit number with alignment. } else {std::cout << " " << bins[i] << '|';} //Output a single-digit number with alignment. size_t height = modifier * bins[i]; //Height stands for the number of output asterisks. for(size_t k = 0; k < height; ++k) std::cout << '*'; std::cout << "\n"; } }