From 60818f180422b83293a354e1fe02d5bbe45b1512 Mon Sep 17 00:00:00 2001 From: Artyom Date: Sat, 18 May 2024 15:57:06 +0300 Subject: [PATCH] =?UTF-8?q?Code:=20=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82=20?= =?UTF-8?q?=D0=B3=D0=B8=D1=81=D1=82=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC?= =?UTF-8?q?=D1=8B=20=D0=B8=20=D0=B5=D1=91=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=8D=D0=BA=D1=80=D0=B0=D0=BD=20=D0=B2=D1=8B?= =?UTF-8?q?=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D1=8B=20=D0=B2=20=D0=BE=D1=82?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=83=D1=8E=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index 35b7e93..d30982d 100644 --- a/main.cpp +++ b/main.cpp @@ -46,36 +46,40 @@ find_minmax(const std::vector& numbers, double& min, double& max, bool& } } -int main() { - auto in = input_data(); - auto min = in.numbers[0]; - auto max = in.numbers[0]; +std::vector +make_histogram(const std::vector& numbers, size_t bin_count){ + double min, max; bool res = true; - find_minmax(in.numbers, min, max, res); + find_minmax (numbers, min, max, res); if (res == false){ - cerr << "Number of elements cannot be equal to zero"; + std::cerr << "Number of elements cannot be equal to zero"; exit(1); } - double bin_size = (max - min) / in.bin_count; - vector bins(in.bin_count); - for (auto x : in.numbers) { + double bin_size = (max - min) / bin_count; + std::vector bins(bin_count); + for (auto x : numbers) { bool found = false; - for (auto j = 0; (j < in.bin_count - 1) && !found ; j++) { + for (size_t j = 0; (j < bin_count - 1) && !found ; j++) { if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) { bins[j] += 1; found = true; } } - if (!found) bins[in.bin_count - 1]++; + if (!found) bins[bin_count - 1]++; } + return bins; +} + +void +show_histogram_text(vector bins, size_t bin_count ){ auto max_count = bins[0]; for (auto x : bins) { if (x > max_count) { max_count = x; } } - for (auto i = 0; i < in.bin_count; i++) { - auto j = 100; + for (size_t i = 0; i < bin_count; i++) { + size_t j = 100; while (bins[i] < j) { cout << " "; j /= 10; @@ -99,5 +103,10 @@ int main() { } cout << endl; } +} +int main() { + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins, in.bin_count); return 0; }