code: point 4.1 took out the make_histogram and find_minmax functionsЭ
Этот коммит содержится в:
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 <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -26,53 +27,6 @@ input_data(){
|
|||||||
return in;
|
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
|
void
|
||||||
show_histogramm_text(vector<int> bins, vector<double> numbers, size_t bin_count){
|
show_histogramm_text(vector<int> bins, vector<double> numbers, size_t bin_count){
|
||||||
|
|
||||||
@@ -151,9 +105,6 @@ main()
|
|||||||
|
|
||||||
auto in = input_data();
|
auto in = input_data();
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
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);
|
show_histogramm_text(bins, in.numbers, in.bin_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user