From 40ee38f4c95d8b040de5d93f68bb5c2aedb23dee Mon Sep 17 00:00:00 2001 From: "Dima (AntonovDA)" Date: Thu, 11 Apr 2024 23:24:11 +0300 Subject: [PATCH] =?UTF-8?q?code:=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20make=5Fhi?= =?UTF-8?q?stogram?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- l03.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/l03.cpp b/l03.cpp index c9871d9..8794733 100644 --- a/l03.cpp +++ b/l03.cpp @@ -37,7 +37,6 @@ Input input_data() return in; } -using namespace std; void find_minmax(const vector &numbers, double &min, double &max) { @@ -57,6 +56,46 @@ void find_minmax(const vector &numbers, double &min, double &max) } } +int make_histogram(const vector &numbers, size_t &bin_count) +{ + vector bins(bin_count); + int max_count = bins[0]; + double min = numbers[0]; + double max = numbers[0]; + + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + + for (size_t i = 0; i < numbers.size(); i++) + { + bool found = false; + for (size_t j = 0; (j < bin_count - 1) && !found; j++) + { + auto lo = min + j * bin_size; + auto hi = min + (j + 1) * bin_size; + + if ((lo <= numbers[i]) && (numbers[i] < hi)) + { + bins[j]++; + found = true; + } + } + + if (!found) + { + bins[bin_count - 1]++; + } + } + for (int i = 0; i < bin_count; i++) + { + if (bins[i] > max_count) + { + max_count = bins[i]; + } + } + return bins; +} + int main() { Input in = input_data();