code: добавлена функция расчета гистограммы

main
ShchegolikhYR 2 месяцев назад
Родитель 597bf96c03
Сommit 0e661943b3

@ -37,48 +37,46 @@ void find_minmax(const vector<double>& numbers, double& minimum, double& maximum
} }
} }
int main () { vector<size_t> make_histogram(const vector<double>& numbers, size_t& bin_count) {
vector<float> numbers; vector <size_t> baskets;
vector<size_t> baskets; baskets.resize(bin_count);
// size_t numbers_count;
// size_t baskets_count;
size_t baskets_max_count;
const size_t screen_width = 80;
const size_t max_asterisk = screen_width - 3 - 1;
double basket_max; double basket_max;
double basket_min; double basket_min;
double basket_size; double basket_size;
cout.precision(4);
Input in = input_data();
baskets.resize(in.bin_count);
find_minmax(in.numbers, basket_min, basket_max);
basket_size = (basket_max - basket_min) / in.bin_count; find_minmax(numbers, basket_min, basket_max);
basket_size = (basket_max - basket_min) / bin_count;
for (int i = 0; i < in.bin_count; i++) { for (int i = 0; i < bin_count; i++) {
for (int j = 0; j < in.numbers.size(); j++) { for (int j = 0; j < numbers.size(); j++) {
if ((in.numbers[j] >= (basket_min + i * basket_size) and in.numbers[j] < (basket_min + (i + 1) * basket_size)) or (i == in.bin_count - 1 and in.numbers[j] == basket_max)) { if ((numbers[j] >= (basket_min + i * basket_size) and numbers[j] < (basket_min + (i + 1) * basket_size)) or (i == bin_count - 1 and numbers[j] == basket_max)) {
baskets[i]++; baskets[i]++;
} }
} }
} }
return baskets;
}
int main () {
size_t baskets_max_count;
const size_t screen_width = 80;
const size_t max_asterisk = screen_width - 3 - 1;
cout.precision(4);
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
baskets_max_count = baskets[0]; baskets_max_count = bins[0];
for (size_t i : baskets) { for (size_t i : bins) {
if (i > baskets_max_count) baskets_max_count = i; if (i > baskets_max_count) baskets_max_count = i;
} }
for (int i = 0; i < in.bin_count; i++) { for (int i = 0; i < in.bin_count; i++) {
size_t height = baskets[i]; size_t height = bins[i];
if (baskets_max_count > max_asterisk) { if (baskets_max_count > max_asterisk) {
height = max_asterisk * (static_cast<double>(baskets[i]) / baskets_max_count); height = max_asterisk * (static_cast<double>(bins[i]) / baskets_max_count);
} }
cout << baskets[i] << "|"; cout << bins[i] << "|";
for (int j = 0; j < height; j++) cout << "*"; for (int j = 0; j < height; j++) cout << "*";
cout << endl; cout << endl;
} }
return 0; return 0;
} }

Загрузка…
Отмена
Сохранить