diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..0bf4fff --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,48 @@ +#include "histogram.h" + +using namespace std; + +static void +find_minmax(const std::vector& numbers, double& Min, double& Max) { + Min = numbers[0]; + Max = numbers[0]; + for(size_t x : numbers) + { + if(x < Min) + { + Min = x; + }else + { + if(x > Max) + { + Max = x; + } + } + } +} +std::vector make_histogram(std::vector numbers, size_t bin_count) +{ + std::vector bins(bin_count); + double Min, Max; + 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..aab82b8 --- /dev/null +++ b/histogram.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED +#include +#include + +std::vector +make_histogram(const std::vector numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/main.cpp b/main.cpp index add06c3..1d5c197 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,12 @@ #include #include - -const size_t SCREEN_WIDTH = 80; -const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - +#include "text.h" +#include "histogram.h" using namespace std; struct Input { - vector numbers; + std::vector numbers; size_t bin_count{}; + size_t number_count{}; }; Input input_data() { @@ -22,72 +21,6 @@ input_data() { cin >> in.bin_count; return in; } -void find_minmax(const vector& numbers, double& Min, double& Max) { - Min = numbers[0]; - Max = numbers[0]; - for(size_t x : numbers) - { - if(x < Min) - { - Min = x; - }else - { - if(x > Max) - { - Max = x; - } - } - } -} -vector -make_histogram(vector numbers, size_t bin_count) -{ - std::vector bins(bin_count); - double Min, Max; - 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; -} -void show_histogram_text(vector bins, size_t bin_count) -{ - size_t max_count = bins[0]; - for(size_t x : bins) - { - if(x > max_count) - { - max_count = x; - } - } - for(size_t i = 0;i MAX_ASTERISK) - height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); - for(size_t j = 0;j +#include +#include "text.h" +using namespace std; + +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +void show_histogram_text(const std::vector bins, size_t bin_count) +{ + size_t max_count = bins[0]; + for(size_t x : bins) + { + if(x > max_count) + { + max_count = x; + } + } + for(size_t i = 0;i MAX_ASTERISK) + height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); + for(size_t j = 0;j +#include + +void +show_histogram_text(const std::vector bins, size_t bin_count); + +#endif // TEXT_H_INCLUDED