Родитель
9cfb62adea
Сommit
3bc5b7faf2
@ -0,0 +1,47 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||||
|
min = numbers[0];
|
||||||
|
for (size_t i = 1; i < numbers.size(); i++) {
|
||||||
|
if (numbers[i] < min) {
|
||||||
|
min = numbers[i];
|
||||||
|
}
|
||||||
|
else if (numbers[i] > max) {
|
||||||
|
max = numbers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t>
|
||||||
|
make_histogram(const vector <double>& numbers, size_t bin_count) {
|
||||||
|
double minc, maxc;
|
||||||
|
find_minmax(numbers, minc, maxc);
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
|
double bin_size = (maxc - minc) / bin_count;
|
||||||
|
size_t bin_max_size = 0;
|
||||||
|
for (size_t i = 0; i < numbers.size(); i++) {
|
||||||
|
bool found = false;
|
||||||
|
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
||||||
|
auto lo = minc + j * bin_size;
|
||||||
|
auto hi = minc + (j + 1) * bin_size;
|
||||||
|
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||||
|
bins[j]++;
|
||||||
|
found = true;
|
||||||
|
if (bins[j] > bin_max_size) {
|
||||||
|
bin_max_size = bins[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
bins[bin_count - 1]++;
|
||||||
|
if (bins[bin_count - 1] > bin_max_size) {
|
||||||
|
bin_max_size = bins[bin_count - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче