diff --git a/main.cpp b/main.cpp index 2b29275..1a64a25 100644 --- a/main.cpp +++ b/main.cpp @@ -37,32 +37,34 @@ find_minmax(const std::vector& numbers, double& min, double& max){ } } } -int -main() { - size_t i, j; +vector +make_histogram(const std::vector& numbers, size_t bin_count){ double min, max; - Input in = input_data(); - find_minmax (in.numbers, min, max); - double bin_size = (max - min) / in.bin_count; - vector bins(in.bin_count); - for (auto x : in.numbers) { + find_minmax (numbers, min, max); + double bin_size = (max - min) / bin_count; + std::vector bins(bin_count); + for (auto x : numbers) { bool found = false; - for (j = 0; (j < (in.bin_count - 1) && !found); j++) { - if (min + j * bin_size <= x && (x < min + (j + 1) * bin_size)) { + for (size_t j = 0; (j < bin_count - 1) && !found ; j++) { + if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) { bins[j] += 1; found = true; } } - if (!found) bins[in.bin_count - 1]++; + if (!found) bins[bin_count - 1]++; } + return bins; +} +void +show_histogram_text(vector bins, size_t bin_count ){ auto max_count = bins[0]; for (auto x : bins) { if (x > max_count) { max_count = x; } } - for (i = 0; i < in.bin_count; i++) { - j = 100; + for (size_t i = 0; i < bin_count; i++) { + size_t j = 100; while (bins[i] < j) { cout << " "; j /= 10; @@ -86,5 +88,11 @@ main() { } cout << endl; } +} +int +main() { + Input in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins, in.bin_count); return 0; }