From ffc39805f3741f505af13141e2b9cbcb55e440f0 Mon Sep 17 00:00:00 2001 From: "Sasha (KobzevAV)" Date: Fri, 21 Apr 2023 21:03:14 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 104 +++++++++++++++++++++++++++---------------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/main.cpp b/main.cpp index ed3fed8..b0a1039 100644 --- a/main.cpp +++ b/main.cpp @@ -2,50 +2,58 @@ #include using namespace std; -int main() -{ - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 6 - 1; - size_t bin_count, counts; - cerr << "Enter counts:"; - cin >> counts; +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 6 - 1; - vector numbers(counts); +struct Input { + vector numbers; + size_t bin_count{}; +}; - for (size_t i = 0; i < counts; i++){ - cerr << "Enter array "<< i <<" number:"; - cin >> numbers[i]; +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]; } - cerr << "Enter bin_count:"; - cin >> bin_count; + cin >> in.bin_count; - double minN = numbers[0], maxN = numbers[0]; + return in; +} +void +find_minmax(const vector& numbers, double& min, double& max){ + min = numbers[0]; + max = numbers[0]; for (double x: numbers){ - if (minN > x){ - minN = x; + if (min > x){ + min = x; } - if (maxN < x){ - maxN = x; + if (max < x){ + max = x; } } +} - double diff = (maxN - minN) / bin_count; - +vector make_histogram(const vector& numbers, size_t bin_count){ + double min, max; vector bins(bin_count); - + find_minmax(numbers, min, max); + double diff = (max - min) / bin_count; size_t max_count = 0; - for (size_t i = 0; i < counts; i++){ + for (double x: numbers){ bool found = false; for (size_t j = 0;(j < bin_count - 1) && !found; j++){ - auto lo = minN + j * diff; - auto hi = minN + (j + 1) * diff; - if ((lo <= numbers[i]) && (hi > numbers[i])){ + auto lo = min + j * diff; + auto hi = min + (j + 1) * diff; + if ((lo <= x) && (hi > x)){ bins[j]++; - if (bins[j] > max_count){ - max_count = bins[j]; - } found = true; } } @@ -56,41 +64,33 @@ int main() } } } + return bins; +} - bool scaling = false; - - if (max_count > MAX_ASTERISK){ - scaling = true; - } - - for (size_t i = 0; i < bin_count; i++){ +void show_histogram_text(const vector& bins){ + for (double x: bins){ cout << " "; - if (bins[i] < 100){ + if (x < 100){ cout << " "; } - if (bins[i] < 10){ + if (x < 10){ cout << " "; } - cout << bins[i] << "|"; + cout << x << "|"; - size_t number_of_stars = bins[i]; - - if (scaling){ - if (bins[i] == max_count){ - number_of_stars = MAX_ASTERISK * 1.0; - } - else { - number_of_stars = MAX_ASTERISK * (static_cast(bins[i]) / max_count); - } - } + size_t number_of_stars = x; for (size_t j = 0; j < number_of_stars; j++){ cout << "*"; } cout << endl; - - if (i < bin_count - 1){ - cout << minN + (i + 1) * diff << endl; - } } + +} + + +int main(){ + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins); }