Сравнить коммиты
1 Коммитов
6cd71782fd
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
161fd910f1 |
50
histogram.cpp
Обычный файл
50
histogram.cpp
Обычный файл
@@ -0,0 +1,50 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
void
|
||||
find_minmax(const std::vector<double> &numbers, double &min, double &max){
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::vector<int>
|
||||
make_histogram(std::vector<double> numbers, size_t bin_count){
|
||||
double min;
|
||||
double max;
|
||||
vector<int> bins(bin_count);
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
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 = 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]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
9
histogram.h
Обычный файл
9
histogram.h
Обычный файл
@@ -0,0 +1,9 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
std::vector<int>
|
||||
make_histogram(std::vector<double> numbers, size_t bin_count);
|
||||
|
||||
#endif
|
||||
51
main.cpp
51
main.cpp
@@ -1,5 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -26,53 +27,6 @@ input_data(){
|
||||
return in;
|
||||
}
|
||||
|
||||
void
|
||||
find_minmax(const std::vector<double> &numbers, double &min, double &max){
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::vector<int>
|
||||
make_histogram(std::vector<double> numbers, size_t bin_count){
|
||||
double min;
|
||||
double max;
|
||||
vector<int> bins(bin_count);
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
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 = 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]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
void
|
||||
show_histogramm_text(vector<int> bins, vector<double> numbers, size_t bin_count){
|
||||
|
||||
@@ -151,9 +105,6 @@ main()
|
||||
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
double min;
|
||||
double max;
|
||||
find_minmax(in.numbers, min, max);
|
||||
show_histogramm_text(bins, in.numbers, in.bin_count);
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user