diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..fcc43ed --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,46 @@ +#include +#include "histogram.h" +using namespace std; + +void find_minmax(const vector &numbers, double &min, double &max) +{ + min = numbers[0]; + max = numbers[0]; + for (auto el : numbers) + { + if (el > max) + { + max = el; + } + if (el < min) + { + min = el; + } + } +} + +vector make_histogram(const vector &numbers, const size_t bin_count){ + vector bins(bin_count); + double max, min; + find_minmax(numbers, min, max); + double binSize = (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 * binSize; + auto hi = min + (j + 1) * binSize; + if ((lo <= numbers[i]) && (numbers[i] < 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..1991ac2 --- /dev/null +++ b/histogram.h @@ -0,0 +1,4 @@ +#pragma once +#include + +std::vector make_histogram(const std::vector& numbers, size_t bin_count); \ No newline at end of file diff --git a/main.cpp b/main.cpp index 59d0486..bf8dd20 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,10 @@ #include +#include "histogram.h" +#include "text.h" #include using namespace std; -const size_t SCREEN_WIDTH = 80; -const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - struct Input { vector numbers; @@ -26,74 +25,6 @@ Input input_data() return in; } -void find_minmax(const vector &numbers, double &min, double &max) -{ - min = numbers[0]; - max = numbers[0]; - for (auto el : numbers) - { - if (el > max) - { - max = el; - } - if (el < min) - { - min = el; - } - } -} - -vector make_histogram(const vector &numbers, const size_t &bin_count){ - vector bins(bin_count); - double max, min; - find_minmax(numbers, min, max); - double binSize = (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 * binSize; - auto hi = min + (j + 1) * binSize; - if ((lo <= numbers[i]) && (numbers[i] < hi)) - { - bins[j]++; - found = true; - } - } - if (!found) - { - bins[bin_count - 1]++; - } - } - return bins; - } - -void show_histogram_text(const vector &bins){ - size_t maxCount = bins[0]; - for (auto bin : bins) - { - if (maxCount < bin) - { - maxCount = bin; - } - } - for (auto bin : bins) - { - if (bin < 100) - { - cout << " "; - } - if (bin < 10) - { - cout << " "; - } - size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast(bin) / maxCount); - cout << bin << "|" << string(height, '*'); - cout << "\n"; - } - } - int main() { Input in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..79cee0b --- /dev/null +++ b/text.cpp @@ -0,0 +1,37 @@ +#include +#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 vector &bins) +{ + size_t maxCount = bins[0]; + for (auto bin : bins) + { + if (maxCount < bin) + { + maxCount = bin; + } + } + for (auto bin : bins) + { + if (bin < 100) + { + cout << " "; + } + if (bin < 10) + { + cout << " "; + } + size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast(bin) / maxCount); + cout << bin << "|"; + for (size_t j = 0; j < height; j++) + { + cout << "*"; + } + cout << "\n"; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..441c0f6 --- /dev/null +++ b/text.h @@ -0,0 +1,4 @@ +#pragma once +#include + +void show_histogram_text(const std::vector &bins); \ No newline at end of file