Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
57 строки
1.6 KiB
C++
57 строки
1.6 KiB
C++
#include "histogram.h"
|
|
#include "histogram_internal.h"
|
|
|
|
void
|
|
find_minmax (const std::vector<double>& numbers, double& min, double& max) {
|
|
if (numbers.size() == 0) { //empty array situation check
|
|
return;
|
|
} else {
|
|
min = numbers[0];
|
|
for (double element : numbers) {
|
|
if (element < min) {
|
|
min = element;
|
|
}
|
|
}
|
|
|
|
max = numbers[0];
|
|
for (double element : numbers) {
|
|
if (element > max) {
|
|
max = element;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
|