From 9144570e436652ef7d14270b14da00b41ffbdb5e Mon Sep 17 00:00:00 2001 From: MelnikovDM Date: Tue, 7 May 2024 01:12:55 +0300 Subject: [PATCH] code: Creating a histogram separately --- histogram.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ histogram.h | 9 +++++++++ labor01.cpp | 41 ++--------------------------------------- 3 files changed, 54 insertions(+), 39 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..9acb4cf --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,43 @@ +#include "histogram.h" +#include +#include + +using namespace std; + +static void +find_minmax(const 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; + } + } +} + +vector +make_histogram(const vector& numbers, size_t& bin_count) { + double min = 0; + double max = 0; + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + vector bins(bin_count); + for (double number : 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 <= number) && (number < 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..2dc7695 --- /dev/null +++ b/histogram.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +std::vector +make_histogram(const std::vector& numbers, size_t& bin_count); + +#endif // HISTOGRAM_H_INCLUDED \ No newline at end of file diff --git a/labor01.cpp b/labor01.cpp index b5b954d..100024c 100644 --- a/labor01.cpp +++ b/labor01.cpp @@ -1,6 +1,8 @@ #include #include #include +#include "histogram.h" + using namespace std; struct Input { @@ -26,44 +28,6 @@ input_data() { return in; } -void -find_minmax(const 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; - } - } -} - -vector -make_histogram(const vector& numbers, size_t& bin_count) { - double min = 0; - double max = 0; - find_minmax(numbers, min, max); - double bin_size = (max - min) / bin_count; - vector bins(bin_count); - for (double number : 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 <= number) && (number < hi)) { - bins[j]++; - found = true; - } - } - if (!found) { - bins[bin_count - 1]++; - } - } - return bins; -} - void show_histogram_text(const vector& bins, size_t& bin_count) { const size_t SCREEN_WIDTH = 80; @@ -97,7 +61,6 @@ int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_text(bins, in.bin_count); - }