From 8f349e76140173695cc9344985ec56a6aa2a1731 Mon Sep 17 00:00:00 2001 From: DevyatovaMY <DevyatovaMY@mpei.ru> Date: Mon, 15 Apr 2024 15:36:43 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D1=80=D0=B0=D1=81=D1=87=D1=91=D1=82=D0=B0=20=D0=B8=20=D0=B2?= =?UTF-8?q?=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0=20=D1=82=D0=B5=D0=BA=D1=81=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D0=B9=20=D0=B3=D0=B8=D1=81=D1=82=D0=BE=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D0=BC=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 56 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/main.cpp b/main.cpp index 13d818d..985134d 100644 --- a/main.cpp +++ b/main.cpp @@ -3,13 +3,15 @@ using namespace std; -struct Input { +struct Input +{ vector<double> numbers; size_t bin_count{}; }; Input -input_data() { +input_data() +{ size_t number_count; cerr << "Enter number count: "; cin >> number_count; @@ -25,10 +27,12 @@ input_data() { } void -find_minmax(const vector<double>& numbers, double& min, double& max) { +find_minmax(const vector<double>& numbers, double& min, double& max) +{ min = numbers[0]; max = numbers[0]; - for (double x : numbers) { + for (double x : numbers) + { if (x > max ) { max = x; @@ -40,20 +44,18 @@ find_minmax(const vector<double>& numbers, double& min, double& max) { } } -int main() +vector<size_t> +make_histogram(const vector<double>& numbers, size_t& bin_count) { - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - Input in = input_data(); - vector<size_t> bins(in.bin_count); double min = 0; double max = 0; - find_minmax(in.numbers, min, max); - auto bin_size = (max - min)/in.bin_count; - for (double number : in.numbers) + find_minmax(numbers, min, max); + auto bin_size = (max - min)/bin_count; + vector<size_t> bins(bin_count); + for (double number : numbers) { bool found = false; - for (size_t j = 0; (j < in.bin_count - 1) && !found; j++) + for (size_t j = 0; (j < bin_count - 1) && !found; j++) { auto lo = min + j * bin_size; auto hi = min + (j + 1) * bin_size; @@ -65,15 +67,23 @@ int main() } if (!found) { - bins[in.bin_count - 1]++; + bins[bin_count - 1]++; } } + return bins; +} + +void +show_histogram_text(const vector<size_t>& bins, size_t& bin_count) { + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; size_t max_count = 0; - for (size_t s = 0; s < in.bin_count; s++) { + for (size_t s = 0; s < bin_count; s++) + { if (bins[s] > max_count) - { - max_count = bins[s]; - } + { + max_count = bins[s]; + } } for (size_t bin : bins) { @@ -86,7 +96,8 @@ int main() cout << " "; } cout << bin << "|"; - if (max_count > MAX_ASTERISK) { + if (max_count > MAX_ASTERISK) + { size_t count = bin; size_t height = MAX_ASTERISK * (static_cast<double>(count) / max_count); for (size_t t = 0; t < height; t++) @@ -101,3 +112,10 @@ int main() cout << endl; } } + +int main() +{ + Input in = input_data(); + vector<size_t> bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins, in.bin_count); +}