From a5aeaa0976b1747b0111e0b236b603190dd4b87b Mon Sep 17 00:00:00 2001 From: Hihoffff Date: Thu, 29 May 2025 20:28:01 +0300 Subject: [PATCH] first commit --- Lab1.cbp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ histogram.cpp | 45 ++++++++++++++++++++++++++++++++++++ histogram.h | 10 ++++++++ 3 files changed, 119 insertions(+) create mode 100644 Lab1.cbp create mode 100644 histogram.cpp create mode 100644 histogram.h diff --git a/Lab1.cbp b/Lab1.cbp new file mode 100644 index 0000000..031adb2 --- /dev/null +++ b/Lab1.cbp @@ -0,0 +1,64 @@ + + + + + + diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..b8dd626 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,45 @@ +#include "histogram.h" + +std::vector make_histogram(std::vector numbers, size_t bin_count) +{ + double max; + double min; + std::vector bins; + bins.resize(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]) && (hi > numbers[i])) { + bins[j]++; + found = true; + } + } + if (!found) { + bins[bin_count - 1]++; + } + } + return bins; +} + + +bool find_minmax(std::vector numbers, double& min, double& max) { + if(numbers.size()==0){ + return false; + } + min = numbers[0]; + max = numbers[0]; + + for (double number : numbers) { + if (min > number) { + min = number; + } + if (max < number) { + max = number; + } + } + return true; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..f182421 --- /dev/null +++ b/histogram.h @@ -0,0 +1,10 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + + +bool find_minmax(std::vector numbers, double& min, double& max); +std::vector make_histogram(std::vector numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED