#include #include using namespace std; struct Input { vector numbers; size_t bin_count{}; }; Input input_data() { Input in; int number_count; do { cout << "Enter number count: "; cin >> number_count; } while (number_count < 1); do { cout << "Enter bucket: "; cin >> in.bin_count; } while (in.bin_count < 1); cout << "\n"; in.numbers.resize(number_count); for (int i = 0; i < number_count; i++) { cin >> in.numbers[i]; } return in; } void find_minmax(const vector& numbers, double& min, double& max) { min = numbers[0]; max = numbers[0]; for (float x : numbers) { if (x < min) min = x; else if (x > max) max = x; } } vector make_histogram(const vector& numbers, size_t bin_count) { double min, max; find_minmax(numbers, min, max); float k = (max - min) / bin_count; vector bins(bin_count, 0); for (double number : numbers) { bool flag = false; for (size_t j = 0; (j < bin_count && !flag); j++) { if (number >= (min + k * j) && number < (min + k * (j + 1))) { bins[j]++; flag = true; } } if (!flag) bins[bin_count - 1]++; } return bins; } void show_histogram_text(const vector& bins) { const size_t SCREEN_WIDTH = 80; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; size_t max_count = 0; for (size_t count : bins) { if (count > max_count) { max_count = count; } } for (size_t bin : bins) { if (bin < 100) cout << " "; if (bin < 10) cout << " "; cout << bin << "|"; size_t height = bin; if (max_count > MAX_ASTERISK) { if (max_count != bin) height = MAX_ASTERISK * (static_cast(bin) / max_count); else height = MAX_ASTERISK; } for (size_t i = 0; i < height; i++) cout << "*"; cout << "\n"; } } int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_text(bins); }