diff --git a/main.cpp b/main.cpp index d10853a..6aca730 100644 --- a/main.cpp +++ b/main.cpp @@ -40,29 +40,19 @@ void } } -int main() +vector +make_histogram(const vector& numbers, size_t& bin_count) { - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - - Input in = input_data(); - double min, max; - find_minmax(in.numbers, min, max); - - double max_count = 0; - - size_t bin_count=in.bin_count; - vector bins(bin_count); - size_t number_count=in.numbers.size(); + find_minmax(numbers, min, max); double bin_size = (max - min) / bin_count; - - for (size_t i = 0; i < number_count; i++) + vector bins(bin_count); + for (size_t i = 0; i < numbers.size(); i++) { - for (size_t j = i + 1; j < number_count; j++) + for (size_t j = i + 1; j < numbers.size(); j++) { - if (in.numbers[i] == in.numbers[j]) + if (numbers[i] == numbers[j]) { //numbers.erase(numbers.begin() + j); //number_count--; @@ -72,29 +62,43 @@ int main() } } - for (size_t i = 0; i < number_count; i++) + for (size_t i = 0; i < numbers.size(); i++) { bool found = false; for (size_t j = 0; (j < bin_count - 1) && !found; j++) { auto low_bound = min + j * bin_size; auto high_bound = min + (j + 1) * bin_size; - if ((low_bound <= in.numbers[i]) && (in.numbers[i] < high_bound)) + if ((low_bound <= numbers[i]) && (numbers[i] < high_bound)) { bins[j]++; found = true; - if (bins[j] > max_count) - max_count = bins[j]; } } if (!found) { bins[bin_count - 1]++; - if (bins[bin_count - 1] > max_count) - max_count = bins[bin_count - 1]; } } + + return bins; +} + +void +show_histogram_text(const vector& bins, size_t& bin_count) { + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + size_t max_count = 0; + + for (size_t s = 0; s < bin_count; s++) + { + if (bins[s] > max_count) + { + max_count = bins[s]; + } + } + for (size_t bin : bins) { if (bin < 100) cout << " "; @@ -106,6 +110,11 @@ int main() for (size_t i = 0; i < height; i++) cout << "*"; cout << endl; } +} - return 0; +int main() +{ + Input in = input_data(); + vector bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins, in.bin_count); }