#include #include #include const size_t WINDOW_WIDTH = 80; const size_t MAX_VALUE = WINDOW_WIDTH - 3 - 1; using namespace std; struct Input{ vector numbers; size_t bin_count = 0; }; Input input_data(){ size_t number_count; cin >> number_count; Input in; in.numbers.resize(number_count); for (size_t i = 0; i < number_count; i++) { cin >> in.numbers[i]; } cin >> in.bin_count; return in; } void find_minmax(const vector &numbers, double &min, double &max){ min = numbers[0]; max = numbers[0]; for (float now: numbers){ if (now < min) {min = now;} if (now > max) {max = now;} } return; } vector make_histogram(vector numbers, size_t bin_count){ vector bins(bin_count); double min = 0, max = 0; find_minmax(numbers, min, max); double bin_size = (max - min) / bin_count; double lo = min, hi = min + bin_size; for (size_t i = 0; i < bin_count; i++){ for (auto now : numbers){ if (i == bin_count - 1) { if ((now >= lo) && (now <= hi)) {bins[i]++;} } else { if ((now >= lo) && (now < hi)) {bins[i]++;} } } lo = hi; hi += bin_size; } return bins; } void show_histogram_text(const vector &bins){ size_t max_count = 0; for (auto now: bins) {if (now > max_count) {max_count = now;}} cout << "mc = " << max_count << endl; for (size_t now : bins){ if (now < 100) {cout << ' ';} if (now < 10) {cout << ' ';} //форматирование строк cout << now << "|"; int height; if (now == max_count) {height = MAX_VALUE * 1.0;} else {height = MAX_VALUE * (static_cast (now) / max_count);} for (int i = 0; i < round(height); i++) {cout << "*";} cout << endl; } } int main(){ double min = 0, max = 0; Input in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); for (auto now: bins) {cout << now << endl;} show_histogram_text(bins); return 0; }