|
|
|
@ -1,24 +1,37 @@
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 4;
|
|
|
|
|
struct Input {
|
|
|
|
|
vector<double> numbers;
|
|
|
|
|
size_t bin_count{};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Input input_data() {
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
size_t number_count;
|
|
|
|
|
cerr << "Enter number count: ";
|
|
|
|
|
cin >> number_count;
|
|
|
|
|
|
|
|
|
|
vector<double> numbers(number_count);
|
|
|
|
|
Input in;
|
|
|
|
|
|
|
|
|
|
cout << "Enter bin count: ";
|
|
|
|
|
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 >> numbers[i];
|
|
|
|
|
cin >> in.numbers[i];
|
|
|
|
|
}
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double min = numbers[0];
|
|
|
|
|
double max = numbers[0];
|
|
|
|
|
void find_minmax(const vector<double> &numbers, double &min, double &max){
|
|
|
|
|
min = numbers[0];
|
|
|
|
|
max = numbers[0];
|
|
|
|
|
for (double x : numbers) {
|
|
|
|
|
if (x < min) {
|
|
|
|
|
min = x;
|
|
|
|
@ -27,48 +40,43 @@ int main() {
|
|
|
|
|
max = x;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t bin_count;
|
|
|
|
|
cerr << "Enter bin count: ";
|
|
|
|
|
cin >> bin_count;
|
|
|
|
|
int main(){
|
|
|
|
|
Input in = input_data();
|
|
|
|
|
|
|
|
|
|
if (bin_count == 0) {
|
|
|
|
|
bin_count = int(sqrt(number_count));
|
|
|
|
|
if (bin_count > 25) {
|
|
|
|
|
bin_count = int(1 + log2(number_count));
|
|
|
|
|
cerr << "Calculated " << bin_count << " bins with Sturges' formula" << endl;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cerr << "Calculated " << bin_count << " bins with Empiric formula" << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
vector<size_t> bins ( in.bin_count );
|
|
|
|
|
double min = in.numbers[0];
|
|
|
|
|
double max = in.numbers[0];
|
|
|
|
|
|
|
|
|
|
vector<size_t> bins(bin_count, 0);
|
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
|
find_minmax(in.numbers, min, max);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
|
|
|
double bin_size = (max - min) / in.bin_count;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < in.number.size(); i++) {
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (size_t j = 0; (j < (bin_count - 1)) && !found; j++) {
|
|
|
|
|
for (size_t j = 0; (j < (in.bin_count - 1)) && !found; j++) {
|
|
|
|
|
auto lo = min + j * bin_size;
|
|
|
|
|
auto hi = min + (j + 1) * bin_size;
|
|
|
|
|
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
|
|
|
|
if ((lo <= in.numbers[i]) && (in.numbers[i] < hi)) {
|
|
|
|
|
bins[j]++;
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found) {
|
|
|
|
|
bins[bin_count - 1]++;
|
|
|
|
|
bins[in.bin_count - 1]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t maxbin = bins[0];
|
|
|
|
|
for (size_t i=1; i < bins.size(); i++){
|
|
|
|
|
for (size_t i=1; i < in.bin_count; i++){
|
|
|
|
|
if (maxbin < bins[i]){
|
|
|
|
|
maxbin = bins[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (maxbin <= MAX_ASTERISK){
|
|
|
|
|
for (size_t i = 0; i < bins.size(); i++) {
|
|
|
|
|
for (size_t i = 0; i < in.bin_count; i++) {
|
|
|
|
|
cout.width(4);
|
|
|
|
|
cout << bins[i] << "|";
|
|
|
|
|
for (size_t j = 0; j < bins[i]; j++) {
|
|
|
|
@ -78,7 +86,7 @@ int main() {
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
for (size_t i = 0; i < bins.size(); i++) {
|
|
|
|
|
for (size_t i = 0; i < in.bin_count; i++) {
|
|
|
|
|
cout.width(4);
|
|
|
|
|
cout << bins[i] << "|";
|
|
|
|
|
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin));
|
|
|
|
|