|
|
@ -2,28 +2,34 @@
|
|
|
|
#include <vector>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace std;
|
|
|
|
int main()
|
|
|
|
struct Input
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
vector<double> numbers;
|
|
|
|
|
|
|
|
size_t bin_count{};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Input
|
|
|
|
|
|
|
|
input_data()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
|
|
|
size_t number_count;
|
|
|
|
size_t number_count;
|
|
|
|
cerr << "Enter number count: ";
|
|
|
|
cerr << "Enter number count: ";
|
|
|
|
cin >> number_count;
|
|
|
|
cin >> number_count;
|
|
|
|
vector<double> numbers(number_count);
|
|
|
|
Input in;
|
|
|
|
|
|
|
|
cerr << "Enter number of bins: ";
|
|
|
|
|
|
|
|
cin >> in.bin_count;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
in.numbers.resize(number_count);
|
|
|
|
|
|
|
|
cerr << "Enter numbers: ";
|
|
|
|
for (size_t i = 0; i < number_count; i++)
|
|
|
|
for (size_t i = 0; i < number_count; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cin >> numbers[i];
|
|
|
|
cin >> in.numbers[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
size_t bin_count;
|
|
|
|
}
|
|
|
|
cerr << "Enter bin count: ";
|
|
|
|
void
|
|
|
|
cin >> bin_count;
|
|
|
|
find_minmax(const vector<double> &numbers, double &min, double &max)
|
|
|
|
vector<size_t> bins (bin_count);
|
|
|
|
{
|
|
|
|
|
|
|
|
min = numbers[0];
|
|
|
|
double min = numbers[0];
|
|
|
|
max = numbers[0];
|
|
|
|
double max = numbers[0];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (double x : numbers)
|
|
|
|
for (double x : numbers)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (x < min)
|
|
|
|
if (x < min)
|
|
|
@ -35,10 +41,16 @@ int main()
|
|
|
|
max = x;
|
|
|
|
max = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<size_t> make_histogram(const vector<double> &numbers, size_t bin_count)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
double min = numbers[0];
|
|
|
|
|
|
|
|
double max = numbers[0];
|
|
|
|
|
|
|
|
find_minmax(numbers, min, max);
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
|
|
|
|
vector <size_t> bins (bin_count);
|
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < number_count; i++)
|
|
|
|
for (size_t i = 0; i < numbers.size(); i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
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++)
|
|
|
@ -56,9 +68,15 @@ int main()
|
|
|
|
bins[bin_count - 1]++;
|
|
|
|
bins[bin_count - 1]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bins;
|
|
|
|
int max_count = 0;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < bin_count; i++)
|
|
|
|
void
|
|
|
|
|
|
|
|
show_histogram_text(const vector <size_t> &bins)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
|
|
|
int max_count = bins[0];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < bins.size(); i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (bins[i] > max_count)
|
|
|
|
if (bins[i] > max_count)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -67,7 +85,7 @@ int main()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (max_count > MAX_ASTERISK)
|
|
|
|
if (max_count > MAX_ASTERISK)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
for (int i = 0; i < bin_count; i++)
|
|
|
|
for (int i = 0; i < bins.size(); i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
|
|
|
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
|
|
|
if (bins[i] < 100)
|
|
|
|
if (bins[i] < 100)
|
|
|
@ -89,7 +107,7 @@ int main()
|
|
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
for (int i = 0; i < bin_count; i++)
|
|
|
|
for (int i = 0; i < bins.size(); i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (bins[i] < 100)
|
|
|
|
if (bins[i] < 100)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -109,5 +127,17 @@ int main()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
|
|
|
main()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
auto in = input_data();
|
|
|
|
|
|
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
|
|
|
|
|
|
show_histogram_text(bins);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|