diff --git a/main.cpp b/main.cpp index 85477f4..d1fa10b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,51 +1,73 @@ #include #include #include +#include using namespace std; -int main() -{ - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - int i, j; - double num; - size_t number_count, bin_count; - cerr << "Enter number count: " << "\n"; + +struct Input { + vector numbers; + size_t bin_count{}; +}; + +Input +input_data(){ + + size_t number_count; + cerr << "Enter number count: "; cin >> number_count; - vector numbers(number_count); - for (i = 0; i < number_count; i++) - { - cin >> num; - numbers[i] = num; - } - cerr << "Enter bin count: " << "\n"; - cin >> bin_count; - vector bins(bin_count); - for (i = 0; i < bin_count; i++) + + Input in; + + in.numbers.resize(number_count); + cerr << "Enter numbers: " << endl; + for (size_t i=0; i < number_count; i++) { - bins[i] = 0; + cin >> in.numbers[i]; } - double min_number = numbers[0]; - double max_number = numbers[0]; - for (double x : numbers) - { - if (x < min_number) + + in.bin_count; + cerr << "Enter bin count: "; + cin >> in.bin_count; + return in; +} + +void +find_minmax(vector numbers, double& min, double& max){ + min = numbers[0]; + max = numbers[0]; + for(size_t i=0; i < sizeof(numbers) / sizeof(numbers[0]); i++){ + if (numbers[i] < min) { - min_number = x; + min = numbers[i]; } - else if (x > max_number) + else if (numbers[i] > max) { - max_number = x; + max = numbers[i]; } } - double bin_size = (max_number - min_number) / bin_count; - for (size_t i = 0; i < number_count; i++) +} + + +int main() +{ + const size_t MAX_ELEMENT = 76; + + Input in = input_data(); + + double min, max; + find_minmax(in.numbers, min, max); + + vector bins(in.bin_count); + double bin_size = (max - min) / in.bin_count; + + for (size_t i = 0; i < sizeof(in.numbers) / sizeof(in.numbers[0]); i++) { bool found = false; - for (size_t j = 0; (j < bin_count - 1) && !found; j++) + for (size_t j = 0; (j < in.bin_count - 1) && !found; j++) { - auto lo = min_number + j * bin_size; - auto hi = min_number + (j + 1) * bin_size; - if ((lo <= numbers[i]) && (numbers[i] < hi)) + auto lower_Bound = min + j * bin_size; + auto upper_Bound = min + (j + 1) * bin_size; + if ((lower_Bound <= in.numbers[i]) && (in.numbers[i] < upper_Bound)) { bins[j]++; found = true; @@ -53,43 +75,53 @@ int main() } if (!found) { - bins[bin_count - 1]++; + bins[in.bin_count - 1]++; } } - size_t max_bin_capacity = bins[0]; - for (size_t x : bins) - { - if (x > max_bin_capacity) - { - max_bin_capacity = x; + + double max_count = bins[0]; + for (size_t j=0; j < in.bin_count; j++){ + if (bins[j] > max_count) { + max_count = bins[j]; } } - size_t height = 0; - for (i = 0; i < bin_count; i++) - { - if (bins[i] < 10) - { - cout << " " << bins[i] << "|"; - } - if ((bins[i] < 100) && (bins[i] > 9)) - { - cout << " " << bins[i] << "|"; - } - if (bins[i] >= 100) - { - cout << bins[i] << "|"; - } - if (bins[i] > 76){ - height = MAX_ASTERISK * (static_cast(bins[i]) / max_bin_capacity); - } - if (bins[i] < 76) { - height = bins[i]; - } - for (j = 0; j < height; j++) - { - cout << "*"; - } - cout << "\n"; + vector height(in.bin_count); + for (size_t j=0; j < in.bin_count; j++){ + height[j] = MAX_ELEMENT * (bins[j]/max_count); + } + + if (max_count > MAX_ELEMENT){ + for (size_t j=0; j < in.bin_count; j++) + { + if (bins[j] < 100) { + cout << " "; + if (bins[j] < 10){ + cout << " "; + } + } + cout << bins[j] << "|"; + + for (size_t i=0; i < height[j]; i++){ + cout << "*"; + } + cout << endl; + } + } + else { + for (size_t j=0; j < in.bin_count; j++) + { + if (bins[j] < 100) { + cout << " "; + if (bins[j] < 10){ + cout << " "; + } + } + cout << bins[j] << "|"; + + for (size_t i=0; i < bins[j];i++){ + cout << "*"; + } + cout << endl; + } } - return 0; }