#include #include #include #include using namespace std; struct Input { vector numbers; size_t bin_count{}; }; 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 (double 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); vector bins(bin_count); double bin_size = (max - min) / bin_count; size_t number_count; for (size_t i = 0; i < numbers.size(); i++){ bool found = false; for (size_t j = 0; (j < bin_count - 1) && !found; j++){ auto lo = min + j * bin_size; auto hi = min + (j + 1) * bin_size; if ((lo <= numbers[i]) && (numbers[i] < hi)){ bins[j]++; found = true; } } if (!found){ bins[bin_count - 1]++; } } return bins; } void show_histogram_text(size_t& bin_count, vector& bins){ vector counts(bin_count); for (size_t i = 0; i < bin_count; i++){ for (size_t j = 0; j < bins[i]; j++){ counts[i]++; } } size_t max_count = counts[0]; for (size_t i=0; imax_count){ max_count = counts[i]; } } const size_t screen_width = 80; const size_t max_asterisk = screen_width - 3 - 1; vector heights(bin_count); size_t height; for (size_t i=0; i 76){ height = (max_asterisk*(static_cast(counts[i])/max_count)); } else { height = counts[i]; } heights[i] = height; } for (size_t i = 0; i < bin_count; i++){ if(bins[i]<100){ cout << " "; } if (bins[i]<10){ cout << " "; } cout << bins[i] << "|"; for (size_t j = 0; j < heights[i]; j++){ cout << "*"; } cout << "\n"; } } int main(){ auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_text(in.bin_count, bins); getch(); return 0; }