Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
60 строки
1.3 KiB
C++
60 строки
1.3 KiB
C++
#include "histogram.h"
|
|
#include <iostream>
|
|
#include <vector>
|
|
using namespace std;
|
|
|
|
static void find_minmax(const vector<double>& numbers, double& minN, double& maxN)
|
|
{
|
|
minN = numbers[0];
|
|
maxN = numbers[0];
|
|
|
|
for (double x: numbers)
|
|
{
|
|
if (minN > x)
|
|
{
|
|
minN = x;
|
|
}
|
|
if (maxN < x)
|
|
{
|
|
maxN = x;
|
|
}
|
|
}
|
|
}
|
|
|
|
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
|
|
{
|
|
double minN, maxN;
|
|
find_minmax( numbers, minN, maxN);
|
|
|
|
vector <size_t> bins(bin_count);
|
|
double diff = (maxN - minN) / bin_count;
|
|
size_t max_count = 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 = minN + j * diff;
|
|
auto hi = minN + (j + 1) * diff;
|
|
if ((lo <= numbers[i]) && (hi > numbers[i]))
|
|
{
|
|
bins[j]++;
|
|
if (bins[j] > max_count)
|
|
{
|
|
max_count = bins[j];
|
|
}
|
|
found = true;
|
|
}
|
|
}
|
|
if(!found)
|
|
{
|
|
bins[bin_count - 1]++;
|
|
if (bins[bin_count - 1] > max_count)
|
|
{
|
|
max_count = bins[bin_count - 1];
|
|
}
|
|
}
|
|
}
|
|
return bins;
|
|
}
|