#include "histogram.h" #include "histogram_internal.h" #include using std::vector; void find_minmax(const vector& numbers, double& min, double& max) { if (numbers.empty()) { min = 0; max = 0; return; } max = numbers[0]; min = numbers[0]; for (double x : numbers) { if (x < min) min = x; else if (x > max) max = x; } } std::vector make_histogram(const std::vector& numbers, size_t bin_count) { std::vector bins(bin_count, 0); double min, max; find_minmax(numbers, min, max); double bin_size = (max - min) / bin_count; for (double number : numbers) { bool found = false; for (size_t j = 0; (j < bin_count - 1) && !found; j++) { double lo = min + j * bin_size; double hi = min + (j + 1) * bin_size; if ((lo <= number) && (number < hi)) { bins[j]++; found = true; } } if (!found) { bins[bin_count - 1]++; } } return bins; }