Code: расчет гистограммы и её вывод на экран вынесены в отдельную функцию

main
Artyom 12 месяцев назад
Родитель 10312c4c58
Сommit 60818f1804

@ -46,36 +46,40 @@ find_minmax(const std::vector<double>& numbers, double& min, double& max, bool&
} }
} }
int main() { std::vector<size_t>
auto in = input_data(); make_histogram(const std::vector<double>& numbers, size_t bin_count){
auto min = in.numbers[0]; double min, max;
auto max = in.numbers[0];
bool res = true; bool res = true;
find_minmax(in.numbers, min, max, res); find_minmax (numbers, min, max, res);
if (res == false){ if (res == false){
cerr << "Number of elements cannot be equal to zero"; std::cerr << "Number of elements cannot be equal to zero";
exit(1); exit(1);
} }
double bin_size = (max - min) / in.bin_count; double bin_size = (max - min) / bin_count;
vector<size_t> bins(in.bin_count); std::vector<size_t> bins(bin_count);
for (auto x : in.numbers) { for (auto x : numbers) {
bool found = false; bool found = false;
for (auto j = 0; (j < in.bin_count - 1) && !found ; j++) { for (size_t j = 0; (j < bin_count - 1) && !found ; j++) {
if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) { if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) {
bins[j] += 1; bins[j] += 1;
found = true; found = true;
} }
} }
if (!found) bins[in.bin_count - 1]++; if (!found) bins[bin_count - 1]++;
} }
return bins;
}
void
show_histogram_text(vector<size_t> bins, size_t bin_count ){
auto max_count = bins[0]; auto max_count = bins[0];
for (auto x : bins) { for (auto x : bins) {
if (x > max_count) { if (x > max_count) {
max_count = x; max_count = x;
} }
} }
for (auto i = 0; i < in.bin_count; i++) { for (size_t i = 0; i < bin_count; i++) {
auto j = 100; size_t j = 100;
while (bins[i] < j) { while (bins[i] < j) {
cout << " "; cout << " ";
j /= 10; j /= 10;
@ -99,5 +103,10 @@ int main() {
} }
cout << endl; cout << endl;
} }
}
int main() {
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, in.bin_count);
return 0; return 0;
} }

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