Сommit
8e0c656efb
@ -0,0 +1,82 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
double min,max,lo,hi;
|
||||||
|
size_t number_count,bin_count,i,j,s=0;
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 4;
|
||||||
|
|
||||||
|
cout << "Enter number_count: ";
|
||||||
|
cin >> number_count;
|
||||||
|
cout << "Enter numbers: ";
|
||||||
|
vector<double> numbers;
|
||||||
|
for(size_t i =0 ; i < number_count; ++i){
|
||||||
|
double num;
|
||||||
|
cin >> num;
|
||||||
|
numbers.push_back(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "Enter bin_count: ";
|
||||||
|
cin >> bin_count;
|
||||||
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
for (i = 0; i < number_count; i++){
|
||||||
|
if (numbers[i] < min){
|
||||||
|
min = numbers[i];
|
||||||
|
}
|
||||||
|
else if (numbers[i] > max){
|
||||||
|
max = numbers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
|
double bin_size = (max-min)/bin_count;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < number_count; i++){
|
||||||
|
bool found = false;
|
||||||
|
for (size_t j = 0;(j < bin_count - 1) && !found; j++){
|
||||||
|
lo = min + j * bin_size;
|
||||||
|
hi = min + (j + 1) * bin_size;
|
||||||
|
if ((lo <= numbers[i]) && (numbers[i]<hi)){
|
||||||
|
bins[j]++;
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found){
|
||||||
|
bins[bin_count - 1]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t max_count = 0;
|
||||||
|
|
||||||
|
for (i = 0 ;i < bin_count;i++){
|
||||||
|
if (bins[i] > MAX_ASTERISK){
|
||||||
|
max_count = bins[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0;i < bin_count; i++){
|
||||||
|
s += bins[i];
|
||||||
|
if (bins[i] < 100){
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
if (bins[i]<10){
|
||||||
|
cout <<" ";
|
||||||
|
}
|
||||||
|
cout << s << "|";
|
||||||
|
if (max_count == 0){
|
||||||
|
for(j = 0;j < s; j++){
|
||||||
|
cout <<"*";
|
||||||
|
}}
|
||||||
|
else {
|
||||||
|
size_t height = MAX_ASTERISK*(static_cast<double>(s) / max_count);
|
||||||
|
for( j = 0; j < height; j++){
|
||||||
|
cout <<"*";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}}
|
Загрузка…
Ссылка в новой задаче