#include "histogram.h" void find_minmax(const std::vector<double>& numbers, double& min, double& max) { min = numbers[0]; for(double element : numbers) { if(element < min) {min = element;} } max = numbers[0]; for(double element : numbers) { if(element > max) {max = element;} } } std::vector<std::size_t> make_histogram(const std::vector<double>& numbers, std::size_t bin_count){ double max; double min; find_minmax(numbers, min, max); double bin_size = static_cast<double>(max - min) / (bin_count); std::vector <std::size_t> bins(bin_count); for(std::size_t i = 0; i < numbers.size(); ++i) //Checking if a number is in a bin. { bool found = false; for(std::size_t j = 0; (j < bin_count - 1) && !found; ++j) { if( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border (numbers[i] < (min + (j + 1) * bin_size)) ) //and (min + (j + 1) * bin_size) is equal to the higher border. { ++bins[j]; found = true; } } if(!found) {++bins[bin_count - 1];} //A special case when current number is equal to the maximum. } return bins; }