From 0026d5fc485459a638430b2dd1a6af809845a874 Mon Sep 17 00:00:00 2001 From: KirsanovES Date: Sun, 4 Jun 2023 21:25:44 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D1=82=D1=8C=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D1=83=D1=8E=20?= =?UTF-8?q?=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 histogram.cpp diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..6e5ea44 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,42 @@ +#include "histogram.h" + +bool find_minmax(const std::vector& numbers, double& min, double& max) { + if (numbers.size() == 0){ + return true; + } + else{ + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) { + if (x < min) { + min = x; + } + else if (x > max) { + max = x; + } +} +return false; +} +} + +std::vector make_histogram(const std::vector& numbers, size_t bin_count){ + double max,min; + std::vector bins(bin_count); + find_minmax(numbers,min,max); + double bin_size = (max - min) / bin_count; + for(double x:numbers) + { + 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 <= x) && (x < hi)) { + bins[j]++; + found = true; + } + } + if (!found) + bins[bin_count-1]++; + } + return bins; +}