diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..13fd6e7 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,36 @@ + +#include "histogram.h" +const void find_minmax(const std::vector& 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 make_histogram(const std::vector& numbers, size_t bin_count){ + double max,min; + std::vector bins(bin_count); + find_minmax(numbers,min,max); + double bin_size = (max - min) / bin_count; + for(double x:numbers) + { + 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 <= x) && (x < hi)) { + bins[j]++; + found = true; + } + } + if (!found) + bins[bin_count-1]++; + } + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..1ae09c4 --- /dev/null +++ b/histogram.h @@ -0,0 +1,8 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +std::vector make_histogram(const std::vector& numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/lab3.cbp b/lab3.cbp index f103d77..6781c04 100644 --- a/lab3.cbp +++ b/lab3.cbp @@ -32,6 +32,12 @@ + + + + diff --git a/main.cpp b/main.cpp index 2ee71a0..dfce5bc 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,7 @@ #include #include +#include "histogram.h" + using namespace std; struct Input { @@ -14,44 +16,11 @@ input_data(){ Input in; in.numbers.resize(number_count); - for (size_t i = 0; i < number_count; i++) { - cin >> in.numbers[i]; - } - cin >> in.bin_count; -return in; -} - void find_minmax(const vector& numbers, double& min, double& max) { - min = numbers[0]; - max = numbers[0]; - for (double x : numbers) { - if (x < min) { - min = x; + for (size_t i = 0; i < number_count; i++) { + cin >> in.numbers[i]; } - else if (x > max) { - max = x; - } -} -} -vector make_histogram(const vector& numbers,size_t bin_count){ - double max,min; - find_minmax(numbers,min,max); - double bin_size = (max - min) / bin_count; - vector bins(bin_count); - for(double x:numbers) - { - 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 <= x) && (x < hi)) { - bins[j]++; - found = true; - } - } - if (!found) - bins[bin_count-1]++; - } - return bins; + cin >> in.bin_count; + return in; } void show_histogram(vector bins,size_t bin_count){ for(size_t i=0;i bins,size_t bin_count){ } int main() { auto in = input_data(); - auto bins=make_histogram(in.numbers,in.bin_count); + auto bins = make_histogram(in.numbers,in.bin_count); show_histogram(bins,in.bin_count); }