From 0e661943b3b489be32aeb4300010c6f17196cf3a Mon Sep 17 00:00:00 2001 From: ShchegolikhYR Date: Wed, 30 Apr 2025 22:20:34 +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=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82=D0=B0=20=D0=B3=D0=B8?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B3=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 | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/main.cpp b/main.cpp index a0f4363..313c6a1 100644 --- a/main.cpp +++ b/main.cpp @@ -37,48 +37,46 @@ void find_minmax(const vector& numbers, double& minimum, double& maximum } } -int main () { - vector numbers; - vector baskets; -// size_t numbers_count; -// size_t baskets_count; - size_t baskets_max_count; - const size_t screen_width = 80; - const size_t max_asterisk = screen_width - 3 - 1; +vector make_histogram(const vector& numbers, size_t& bin_count) { + vector baskets; + baskets.resize(bin_count); + double basket_max; double basket_min; double basket_size; - cout.precision(4); - - Input in = input_data(); - baskets.resize(in.bin_count); - - find_minmax(in.numbers, basket_min, basket_max); - basket_size = (basket_max - basket_min) / in.bin_count; - - for (int i = 0; i < in.bin_count; i++) { - for (int j = 0; j < in.numbers.size(); j++) { - if ((in.numbers[j] >= (basket_min + i * basket_size) and in.numbers[j] < (basket_min + (i + 1) * basket_size)) or (i == in.bin_count - 1 and in.numbers[j] == basket_max)) { + find_minmax(numbers, basket_min, basket_max); + basket_size = (basket_max - basket_min) / bin_count; + for (int i = 0; i < bin_count; i++) { + for (int j = 0; j < numbers.size(); j++) { + if ((numbers[j] >= (basket_min + i * basket_size) and numbers[j] < (basket_min + (i + 1) * basket_size)) or (i == bin_count - 1 and numbers[j] == basket_max)) { baskets[i]++; } } } + return baskets; +} +int main () { + size_t baskets_max_count; + const size_t screen_width = 80; + const size_t max_asterisk = screen_width - 3 - 1; + cout.precision(4); + + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); - baskets_max_count = baskets[0]; - for (size_t i : baskets) { + baskets_max_count = bins[0]; + for (size_t i : bins) { if (i > baskets_max_count) baskets_max_count = i; } - for (int i = 0; i < in.bin_count; i++) { - size_t height = baskets[i]; + size_t height = bins[i]; if (baskets_max_count > max_asterisk) { - height = max_asterisk * (static_cast(baskets[i]) / baskets_max_count); + height = max_asterisk * (static_cast(bins[i]) / baskets_max_count); } - cout << baskets[i] << "|"; + cout << bins[i] << "|"; for (int j = 0; j < height; j++) cout << "*"; cout << endl; } return 0; } -