Родитель
bdd78f7c1e
Сommit
736ee1a015
@ -0,0 +1,59 @@
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
static
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (size_t i = 0; i < numbers.size(); i++){
|
||||
if (numbers[i] < min){
|
||||
min = numbers[i];
|
||||
}
|
||||
if (numbers[i] > max){
|
||||
max = numbers[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector <double> make_histogram(const vector<double> numbers, size_t bin_count){
|
||||
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
vector <double> bins;
|
||||
bins.resize(bin_count);
|
||||
|
||||
for (std::size_t i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
bool found = false;
|
||||
for (std::size_t j = 0; (j < bin_count - 1) && !found; j++)
|
||||
{
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
double out;
|
||||
for (std::size_t i = 0; i < bin_count-1; i++)
|
||||
{
|
||||
if (bins[i] > bins[i+1])
|
||||
{
|
||||
out = bins[i];
|
||||
bins[i] = bins[i + 1];
|
||||
bins[i + 1] = out;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
@ -0,0 +1,5 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, const std::size_t bin_count);
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="lab_03" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/lab_03" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/lab_03" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename=".gitignore" />
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram.h" />
|
||||
<Unit filename="main.cpp" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
Загрузка…
Ссылка в новой задаче