diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..32cae6e --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,68 @@ +#include "histogram.h" +#include + +using namespace std; + + +void +static find_minmax(const vector& numbers, double& min, double& max) { + min = numbers[0]; + max = numbers[0]; + for(size_t i=0; i< numbers.size(); i++){ + if(min > numbers[i]) + min = numbers[i]; + if(max < numbers[i]) + max = numbers[i]; +}} + +vector make_histogram(vector& numbers, size_t bin_count) +{ + +vector bins(bin_count); +vector binss(bin_count); + +double max, min; +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]++; +} +} + +int max_count = bins[0]; +for (size_t i = 0; i < bin_count; i++) +{ +if (bins[i] > max_count) +{ +max_count = bins[i]; +} +} + +if (max_count > 76) +{ + +for (size_t i = 0; i < bin_count; i++) +{ +int count = bins[i]; +size_t height = 76 * (static_cast(count) / max_count); +bins[i] = height; +} +} + +return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..5838a07 --- /dev/null +++ b/histogram.h @@ -0,0 +1,13 @@ +#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 + diff --git a/histogram.h.save-failed b/histogram.h.save-failed new file mode 100644 index 0000000..182036d --- /dev/null +++ b/histogram.h.save-failed @@ -0,0 +1,15 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED +#include + +std::vector + +make_histogram(const std::vector& numbers, size_t bin_count): + + +find_minmax(const std::vector& numbers, double& min, double& max): + + + +#endif // HISTOGRAM_H_INCLUDED + diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..4a65227 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +std::vector make_histogram(std::vector& numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED + diff --git a/lab01.depend b/lab01.depend new file mode 100644 index 0000000..671a52d --- /dev/null +++ b/lab01.depend @@ -0,0 +1,17 @@ +# depslib dependency file v1.0 +1686062766 source:c:\users\kostello\desktop\lubs\lub03\lub01\main.cpp + + + + + +1686137810 source:c:\users\kostello\desktop\lubs\lub03\lub01\histogram.cpp + "histogram.h" + + +1686137015 c:\users\kostello\desktop\lubs\lub03\lub01\histogram.h + + +1686137097 c:\users\kostello\desktop\lubs\lub03\lub01\histogram_internal.h + + diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..07bb4c2 --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,29 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "histogram_internal.h" + +TEST_CASE("distinct positive numbers") { +double min = 0; +double max = 0; +find_minmax({1, 2}, min, max); +CHECK(min == 1); +CHECK(max == 2); +} + +TEST_CASE("odinakovie") { +double min = 0; +double max = 0; +find_minmax({1, 1}, min, max); +CHECK(min == 1); +CHECK(max == 1); +} + +TEST_CASE("distinct negative numbers") { +double min = 0; +double max = 0; +find_minmax({-2, -1}, min, max); +CHECK(min == -2); +CHECK(max == -1); +} +