From a4cd96b77e3d703f54deb78f6e9e62854c47b073 Mon Sep 17 00:00:00 2001 From: MamakinYR Date: Mon, 8 Apr 2024 16:19:34 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=92=D0=B2=D0=BE=D0=B4=20=D0=B2=D1=8B?= =?UTF-8?q?=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=20=D0=B2=20=D0=BE=D1=82=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BD=D1=83=D1=8E=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab1.cpp | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/lab1.cpp b/lab1.cpp index 0e3b9cc..dd2f329 100644 --- a/lab1.cpp +++ b/lab1.cpp @@ -2,31 +2,38 @@ #include #include using namespace std; +struct Input { + vector numbers; + size_t bin_count{}; +}; + +Input input_data() { + Input in; + size_t number_count; + cin >> number_count; + in.numbers.resize(number_count); + for (size_t i = 0; i < number_count; i++) { + cin >> in.numbers[i]; + } + cin >> in.bin_count; + return in; +} + int main() { const size_t SCREEN_WIDTH = 80; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; int i, j; double num; - size_t number_count, bin_count; - cerr << "Enter number count: " << "\n"; - cin >> number_count; - vector numbers(number_count); - for (i = 0; i < number_count; i++) - { - cin >> num; - numbers[i] = num; - } - cerr << "Enter bin count: " << "\n"; - cin >> bin_count; - vector bins(bin_count); - for (i = 0; i < bin_count; i++) + Input in = input_data(); + vector bins(in.bin_count); + for (i = 0; i < in.bin_count; i++) { bins[i] = 0; } - double min_number = numbers[0]; - double max_number = numbers[0]; - for (double x : numbers) + double min_number = in.numbers[0]; + double max_number = in.numbers[0]; + for (double x : in.numbers) { if (x < min_number) { @@ -37,15 +44,15 @@ int main() max_number = x; } } - double bin_size = (max_number - min_number) / bin_count; - for (size_t i = 0; i < number_count; i++) + double bin_size = (max_number - min_number) / in.bin_count; + for (size_t i = 0; i < in.numbers.size(); i++) { bool found = false; - for (size_t j = 0; (j < bin_count - 1) && !found; j++) + for (size_t j = 0; (j < in.bin_count - 1) && !found; j++) { auto lo = min_number + j * bin_size; auto hi = min_number + (j + 1) * bin_size; - if ((lo <= numbers[i]) && (numbers[i] < hi)) + if ((lo <= in.numbers[i]) && (in.numbers[i] < hi)) { bins[j]++; found = true; @@ -53,7 +60,7 @@ int main() } if (!found) { - bins[bin_count - 1]++; + bins[in.bin_count - 1]++; } } size_t max_bin_capacity = bins[0]; @@ -65,7 +72,7 @@ int main() } } size_t height = 0; - for (i = 0; i < bin_count; i++) + for (i = 0; i < in.bin_count; i++) { if (bins[i] < 10) {