From 6161e2abe0da5b6f68aca3a461f7f55965069eca Mon Sep 17 00:00:00 2001 From: YashechkinGA Date: Wed, 14 May 2025 01:44:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BE=D0=BF=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20make=5Fgistogram?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 histogram.cpp diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..9637586 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,65 @@ +#include "histogram.h" +#include +#include +using namespace std; + +//std::vector +bool find_minmax(const vector& numbers, double& minN, double& maxN) +{ + if (numbers.empty()) { + minN = maxN = 0; + return false; + } + minN = numbers[0]; + maxN = numbers[0]; + + for (double x: numbers) + { + if (minN > x) + { + minN = x; + } + if (maxN < x) + { + maxN = x; + } + } + return true; +} + +vector make_histogram(const vector& numbers, size_t bin_count) +{ + double minN, maxN; + find_minmax( numbers, minN, maxN); + + vector bins(bin_count); + double diff = (maxN - minN) / bin_count; + size_t max_count = 0; + for (size_t i = 0; i numbers[i])) + { + bins[j]++; + if (bins[j] > max_count) + { + max_count = bins[j]; + } + found = true; + } + } + if(!found) + { + bins[bin_count - 1]++; + if (bins[bin_count - 1] > max_count) + { + max_count = bins[bin_count - 1]; + } + } + } + return bins; +}