|
|
|
@ -1,67 +1,90 @@
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
|
|
|
|
|
struct Input {
|
|
|
|
|
vector<double> numbers;
|
|
|
|
|
size_t bin_count{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Input
|
|
|
|
|
input_data()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
size_t number_count;
|
|
|
|
|
cerr << "Enter number count:";
|
|
|
|
|
cerr << "count=";
|
|
|
|
|
cin >> number_count;
|
|
|
|
|
vector<double> numbers(number_count);
|
|
|
|
|
cerr << "Enter numbers";
|
|
|
|
|
for (size_t i = 0; i < number_count; i++)
|
|
|
|
|
cin >> numbers[i];
|
|
|
|
|
size_t bin_count;
|
|
|
|
|
cerr << "Enter bin count:";
|
|
|
|
|
cin >> bin_count;
|
|
|
|
|
if (bin_count == 0)
|
|
|
|
|
{
|
|
|
|
|
bin_count = sqrt(number_count);
|
|
|
|
|
if (bin_count > 25)
|
|
|
|
|
{
|
|
|
|
|
bin_count = 1 + log2(number_count);
|
|
|
|
|
cout << "After Sturges rule bin count is:" << bin_count << endl;
|
|
|
|
|
|
|
|
|
|
Input in;
|
|
|
|
|
|
|
|
|
|
in.numbers.resize(number_count);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
|
|
|
cin >> in.numbers[i];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
cout << "After Empirical rule bin count is:" << bin_count << endl;
|
|
|
|
|
|
|
|
|
|
cerr << "bin_count= ";
|
|
|
|
|
cin >> in.bin_count;
|
|
|
|
|
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector <size_t> bins(bin_count, 0);
|
|
|
|
|
double min = numbers[0];
|
|
|
|
|
double max = numbers[0];
|
|
|
|
|
void
|
|
|
|
|
find_minmax(const vector<double>& numbers, double &min, double &max)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
for (double number : numbers)
|
|
|
|
|
min = numbers[0];
|
|
|
|
|
max = numbers[0];
|
|
|
|
|
for (size_t i = 0; i < numbers.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
if (min > number)
|
|
|
|
|
min = number;
|
|
|
|
|
else if (max < number)
|
|
|
|
|
max = number;
|
|
|
|
|
if (numbers[i] > max)
|
|
|
|
|
max = numbers[i];
|
|
|
|
|
if (numbers[i] < min)
|
|
|
|
|
min = numbers[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector <size_t> make_histogram (const vector<double>& numbers, size_t &bin_count)
|
|
|
|
|
{
|
|
|
|
|
vector <size_t> bins(bin_count);
|
|
|
|
|
|
|
|
|
|
double min, max;
|
|
|
|
|
float low, hi;
|
|
|
|
|
|
|
|
|
|
find_minmax(numbers, min, max);
|
|
|
|
|
|
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
|
for (size_t i = 0; i < number_count; i++)
|
|
|
|
|
low = min;
|
|
|
|
|
hi = low + bin_size;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < numbers.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (size_t j = 0; j < (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 ((numbers[i] >= lo) && (numbers[i] < hi))
|
|
|
|
|
low = min + j * bin_size;
|
|
|
|
|
hi = min + (j + 1) * bin_size;
|
|
|
|
|
if ((low <= numbers[i]) && (numbers[i] < hi))
|
|
|
|
|
{
|
|
|
|
|
bins[j]++;
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found)
|
|
|
|
|
if (found==false)
|
|
|
|
|
{
|
|
|
|
|
bins[bin_count - 1]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return bins;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
void show_histogram_text(const vector<size_t>& bins, size_t &bin_count)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
size_t max_bin = bins[0];
|
|
|
|
|
for (size_t bin : bins)
|
|
|
|
@ -79,9 +102,19 @@ int main()
|
|
|
|
|
if (bin < 10)
|
|
|
|
|
cout << " ";
|
|
|
|
|
cout << bin << "|";
|
|
|
|
|
for (size_t i = 0; i < height; i++)
|
|
|
|
|
for (int i = 0; i < height; i++)
|
|
|
|
|
cout << "*";
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|