diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..6e5ea44 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,42 @@ +#include "histogram.h" + +bool find_minmax(const std::vector& numbers, double& min, double& max) { + if (numbers.size() == 0){ + return true; + } + else{ + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) { + if (x < min) { + min = x; + } + else if (x > max) { + max = x; + } +} +return false; +} +} + +std::vector make_histogram(const std::vector& numbers, size_t bin_count){ + double max,min; + std::vector bins(bin_count); + find_minmax(numbers,min,max); + double bin_size = (max - min) / bin_count; + for(double x: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 <= x) && (x < hi)) { + bins[j]++; + found = true; + } + } + if (!found) + bins[bin_count-1]++; + } + return bins; +}