Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
43 строки
997 B
C++
43 строки
997 B
C++
#include "histogram.h"
|
|
#include <vector>
|
|
using namespace std;
|
|
void FindMinMax(const vector<double>& Numbers, double& minn, double& maxx) {
|
|
maxx = Numbers[0];
|
|
minn = Numbers[0];
|
|
for (auto x : Numbers) {
|
|
if (x > maxx) {
|
|
maxx = x;
|
|
}
|
|
if (x < minn) {
|
|
minn = x;
|
|
}
|
|
}
|
|
}
|
|
|
|
vector<size_t> MakeHistogram(vector<double> Numbers, size_t BinCount, double& MaxCount) {
|
|
double maxx, minn;
|
|
FindMinMax(Numbers, minn, maxx);
|
|
double BinSize = (maxx - minn) / BinCount;
|
|
vector <size_t> Bins(BinCount);
|
|
for (size_t i = 0; i < Numbers.size(); i++) {
|
|
bool found = false;
|
|
for (size_t j = 0; (j < BinCount - 1) && !found; j++) {
|
|
auto lo = minn + j * BinSize;
|
|
auto hi = minn + (j + 1) * BinSize;
|
|
if ((lo <= Numbers[i]) && (Numbers[i] < hi)) {
|
|
Bins[j]++;
|
|
found = true;
|
|
if (Bins[j] > MaxCount) {
|
|
MaxCount = Bins[j];
|
|
}
|
|
}
|
|
}
|
|
if (!found) {
|
|
Bins[BinCount - 1]++;
|
|
if (Bins[BinCount - 1] > MaxCount) {
|
|
MaxCount = Bins[BinCount - 1];
|
|
}
|
|
}
|
|
}
|
|
return Bins;
|
|
} |