main
Ишутина Елизавета 2 лет назад
Родитель 0073bd7b08
Сommit 3dffa78118

@ -0,0 +1,82 @@
#include <iostream>
#include <vector>
#include <cmath>
const size_t WINDOW_WIDTH = 80;
const size_t MAX_VALUE = WINDOW_WIDTH - 3 - 1;
using namespace std;
struct Input{
vector <double> numbers;
size_t bin_count = 0;
};
Input
input_data(){
size_t number_count;
cin >> number_count;
Input in;
in.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
}
cin >> in.bin_count;
return in;
}
void
find_minmax(const vector <double> &numbers, double &min, double &max){
min = numbers[0];
max = numbers[0];
for (float now: numbers){
if (now < min) {min = now;}
if (now > max) {max = now;}
}
return;
}
vector <size_t>
make_histogram(vector <double> numbers, size_t bin_count){
vector <size_t> bins(bin_count);
double min = 0, max = 0;
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
double lo = min, hi = min + bin_size;
for (size_t i = 0; i < bin_count; i++){
for (auto now : numbers){
if (i == bin_count - 1) {
if ((now >= lo) && (now <= hi)) {bins[i]++;}
}
else {
if ((now >= lo) && (now < hi)) {bins[i]++;}
}
}
lo = hi; hi += bin_size;
}
return bins;
}
void show_histogram_text(const vector <size_t> &bins){
size_t max_count = 0;
for (auto now: bins)
{if (now > max_count) {max_count = now;}}
cout << "mc = " << max_count << endl;
for (size_t now : bins){
if (now < 100) {cout << ' ';} if (now < 10) {cout << ' ';} //ôîðìàòèðîâàíèå ñòðîê
cout << now << "|";
int height;
if (now == max_count) {height = MAX_VALUE * 1.0;}
else {height = MAX_VALUE * (static_cast <double> (now) / max_count);}
for (int i = 0; i < round(height); i++) {cout << "*";}
cout << endl;
}
}
int
main(){
double min = 0, max = 0;
Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
for (auto now: bins) {cout << now << endl;}
show_histogram_text(bins);
return 0;
}