From 161fd910f1d73b41e1d252e33a4117975da0d04b Mon Sep 17 00:00:00 2001 From: SmirnovFA Date: Wed, 5 Jun 2024 03:49:49 +0300 Subject: [PATCH] =?UTF-8?q?code:=20point=204.1=20took=20out=20the=20make?= =?UTF-8?q?=5Fhistogram=20and=20find=5Fminmax=20functions=D0=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ histogram.h | 9 +++++++++ main.cpp | 51 +-------------------------------------------------- 3 files changed, 60 insertions(+), 50 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..606724f --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,50 @@ +#include +#include + +using namespace std; +void +find_minmax(const std::vector &numbers, double &min, double &max){ + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) + { + if (x < min) + { + min = x; + } + else if (x > max) + { + max = x; + } + } + +} + +std::vector +make_histogram(std::vector numbers, size_t bin_count){ + double min; + double max; + vector bins(bin_count); + 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]++; + } + } + return bins; +} \ No newline at end of file diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..a8805e0 --- /dev/null +++ b/histogram.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include +#include +std::vector +make_histogram(std::vector numbers, size_t bin_count); + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index d7a5641..77e91dd 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +#include "histogram.h" using namespace std; @@ -26,53 +27,6 @@ input_data(){ return in; } -void -find_minmax(const std::vector &numbers, double &min, double &max){ - min = numbers[0]; - max = numbers[0]; - for (double x : numbers) - { - if (x < min) - { - min = x; - } - else if (x > max) - { - max = x; - } - } - -} - -std::vector -make_histogram(std::vector numbers, size_t bin_count){ - double min; - double max; - vector bins(bin_count); - 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]++; - } - } - return bins; -} - void show_histogramm_text(vector bins, vector numbers, size_t bin_count){ @@ -151,9 +105,6 @@ main() auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - double min; - double max; - find_minmax(in.numbers, min, max); show_histogramm_text(bins, in.numbers, in.bin_count); }