From cd67ea0a8ca1b6dd8a4238233972c197c6f0bc81 Mon Sep 17 00:00:00 2001 From: ShinkarenkoVA Date: Mon, 22 Apr 2024 02:42:00 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=20=D1=84=D0=B0=D0=B9=D0=BB=20=D1=80=D0=B5=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 histogram.cpp diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..18b418e --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +using namespace std; +#include "histogram.h" + +void +find_minmax(const vector& numbers, double& min, double& max){ + min = numbers[0]; + min = numbers[0]; + for(size_t i=0; i < numbers.size(); i++){ + if (numbers[i] < min) + { + min = numbers[i]; + } + else if (numbers[i] > max) + { + max = numbers[i]; + } + } +} + + +vector +make_histogram(const vector& numbers, size_t bin_count){ + + double min, max; + find_minmax(numbers, min, max); + + vector bins(bin_count); + + 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 lower_Bound = min + j * bin_size; + auto upper_Bound = min + (j + 1) * bin_size; + if ((lower_Bound <= numbers[i]) && (numbers[i] < upper_Bound)) + { + bins[j]++; + found = true; + } + } + if (!found) + { + bins[bin_count - 1]++; + } + } + return bins; +} +