|
|
|
@ -9,7 +9,7 @@ const size_t MAX_ASTERISK = SCREEN_WIDTH - 4;
|
|
|
|
|
struct Input {
|
|
|
|
|
vector<double> numbers;
|
|
|
|
|
size_t bin_count{};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Input input_data() {
|
|
|
|
|
|
|
|
|
@ -23,14 +23,13 @@ Input input_data() {
|
|
|
|
|
cin >> in.bin_count;
|
|
|
|
|
|
|
|
|
|
in.numbers.resize(number_count);
|
|
|
|
|
in.bin_count.resize(bin_count);
|
|
|
|
|
|
|
|
|
|
cerr << "Enter numbers: ";
|
|
|
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
|
|
|
cin >> in.numbers[i];
|
|
|
|
|
}
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void find_minmax(const vector<double> &numbers, double &min, double &max){
|
|
|
|
|
min = numbers[0];
|
|
|
|
@ -47,25 +46,25 @@ void find_minmax(const vector<double> &numbers, double &min, double &max){
|
|
|
|
|
|
|
|
|
|
vector<size_t> make_histogram(const vector<double> &numbers, size_t bin_count){
|
|
|
|
|
vector<size_t> bins ( bin_count );
|
|
|
|
|
double min = in.numbers[0];
|
|
|
|
|
double max = in.numbers[0];
|
|
|
|
|
double min = numbers[0];
|
|
|
|
|
double max = numbers[0];
|
|
|
|
|
|
|
|
|
|
find_minmax(numbers, min, max);
|
|
|
|
|
|
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < in.number.size(); i++) {
|
|
|
|
|
for (size_t i = 0; i < numbers.size(); i++) {
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (size_t j = 0; (j < (in.bin_count - 1)) && !found; j++) {
|
|
|
|
|
for (size_t j = 0; (j < (bin_count - 1)) && !found; j++) {
|
|
|
|
|
auto lo = min + j * bin_size;
|
|
|
|
|
auto hi = min + (j + 1) * bin_size;
|
|
|
|
|
if ((lo <= in.numbers[i]) && (in.numbers[i] < hi)) {
|
|
|
|
|
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
|
|
|
|
bins[j]++;
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found) {
|
|
|
|
|
bins[in.bin_count - 1]++;
|
|
|
|
|
bins[bin_count - 1]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return bins;
|
|
|
|
@ -73,14 +72,14 @@ vector<size_t> make_histogram(const vector<double> &numbers, size_t bin_count){
|
|
|
|
|
|
|
|
|
|
void show_histogram_text(const vector<size_t> &bins){
|
|
|
|
|
size_t maxbin = bins[0];
|
|
|
|
|
for (size_t i=1; i < in.bin_count; i++){
|
|
|
|
|
for (size_t i=1; i < bins.size(); i++){
|
|
|
|
|
if (maxbin < bins[i]){
|
|
|
|
|
maxbin = bins[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (maxbin <= MAX_ASTERISK){
|
|
|
|
|
for (size_t i = 0; i < in.bin_count; i++) {
|
|
|
|
|
for (size_t i = 0; i < bins.size(); i++) {
|
|
|
|
|
cout.width(4);
|
|
|
|
|
cout << bins[i] << "|";
|
|
|
|
|
for (size_t j = 0; j < bins[i]; j++) {
|
|
|
|
@ -88,9 +87,8 @@ void show_histogram_text(const vector<size_t> &bins){
|
|
|
|
|
}
|
|
|
|
|
cout << endl;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
for (size_t i = 0; i < in.bin_count; i++) {
|
|
|
|
|
for (size_t i = 0; i < bins.size(); i++) {
|
|
|
|
|
cout.width(4);
|
|
|
|
|
cout << bins[i] << "|";
|
|
|
|
|
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin));
|
|
|
|
@ -99,10 +97,8 @@ void show_histogram_text(const vector<size_t> &bins){
|
|
|
|
|
}
|
|
|
|
|
cout << endl;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
Input in = input_data();
|
|
|
|
|
|
|
|
|
|