diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..9637586 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,65 @@ +#include "histogram.h" +#include +#include +using namespace std; + +//std::vector +bool find_minmax(const vector& numbers, double& minN, double& maxN) +{ + if (numbers.empty()) { + minN = maxN = 0; + return false; + } + minN = numbers[0]; + maxN = numbers[0]; + + for (double x: numbers) + { + if (minN > x) + { + minN = x; + } + if (maxN < x) + { + maxN = x; + } + } + return true; +} + +vector make_histogram(const vector& numbers, size_t bin_count) +{ + double minN, maxN; + find_minmax( numbers, minN, maxN); + + vector bins(bin_count); + double diff = (maxN - minN) / bin_count; + size_t max_count = 0; + for (size_t i = 0; i numbers[i])) + { + bins[j]++; + if (bins[j] > max_count) + { + max_count = bins[j]; + } + found = true; + } + } + if(!found) + { + bins[bin_count - 1]++; + if (bins[bin_count - 1] > max_count) + { + max_count = bins[bin_count - 1]; + } + } + } + return bins; +}