From 6186b5107769b24cb492a6dd6b483d05f0341337 Mon Sep 17 00:00:00 2001 From: Popko Egor Date: Thu, 25 Sep 2025 22:34:35 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 31 +++++++++++++++++++++++++++++++ histogram.h | 9 +++++++++ main.cpp | 27 +-------------------------- 3 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 histogram.cpp create mode 100644 histogram.h diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..7b20c7b --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,31 @@ +#include "histogram.h" +#include +#include + +using namespace std; + +void find_minmax(const vector& numbers, double& min, double& max) { + if (numbers.empty()) { + return; + } + min = numbers[0]; + max = numbers[0]; + + for (double x : numbers) { + if (x < min) min = x; + if (x > max) max = x; + } +} + +vector make_histogram(const vector& numbers, size_t bin_count) { + double min, max; + find_minmax(numbers, min, max); + + vector bins(bin_count); + for (double x : numbers) { + size_t bin_index = (x - min) / (max - min) * bin_count; + if (bin_index == bin_count) bin_index--; + bins[bin_index]++; + } + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..8f0e098 --- /dev/null +++ b/histogram.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include +#include + +std::vector make_histogram(const std::vector& numbers, size_t bin_count); + +#endif diff --git a/main.cpp b/main.cpp index df63689..c669af0 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +#include "histogram.h" using namespace std; @@ -26,32 +27,6 @@ Input input_data() return in; } -void find_minmax(const vector& numbers, double& min, double& max) { - if (numbers.empty()) { - return; - } - min = numbers[0]; - max = numbers[0]; - - for (double x : numbers) { - if (x < min) min = x; - if (x > max) max = x; - } -} - -vector make_histogram(const vector& numbers, size_t bin_count) { - double min, max; - find_minmax(numbers, min, max); - - vector bins(bin_count); - for (double x : numbers) { - size_t bin_index = (x - min) / (max - min) * bin_count; - if (bin_index == bin_count) bin_index--; - bins[bin_index]++; - } - return bins; -} - void show_histogram_text(const vector& bins) { const size_t SCREEN_WIDTH = 80; size_t max_count = 0;