|
|
@ -3,47 +3,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
struct Input {
|
|
|
|
{
|
|
|
|
vector<double> numbers;
|
|
|
|
size_t number_count;
|
|
|
|
size_t bin_count{};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input input_data(){
|
|
|
|
|
|
|
|
size_t number_count, bin_count;
|
|
|
|
cerr << "Enter number count: ";
|
|
|
|
cerr << "Enter number count: ";
|
|
|
|
cin >> number_count;
|
|
|
|
cin >> number_count;
|
|
|
|
|
|
|
|
Input in;
|
|
|
|
|
|
|
|
in.numbers.resize(number_count);
|
|
|
|
|
|
|
|
|
|
|
|
vector<double> numbers(number_count);
|
|
|
|
for (size_t i = 0; i < number_count; i++)
|
|
|
|
cerr << "Enter numbers: ";
|
|
|
|
|
|
|
|
for(size_t i = 0; i < number_count; i ++)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cin >> numbers[i];
|
|
|
|
cerr << "Enter Num[" << i << "]: ";
|
|
|
|
|
|
|
|
cin >> in.numbers[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t bin_count;
|
|
|
|
|
|
|
|
cerr << "Enter bin count: ";
|
|
|
|
cerr << "Enter bin count: ";
|
|
|
|
cin >> bin_count;
|
|
|
|
cin >> in.bin_count;
|
|
|
|
|
|
|
|
return in;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double min = numbers[0];
|
|
|
|
void find_minmax(const vector<double> &numbers, double &min, double &max)
|
|
|
|
double max = numbers[0];
|
|
|
|
{
|
|
|
|
for(double number : numbers)
|
|
|
|
max = numbers[0];
|
|
|
|
|
|
|
|
min = numbers[1];
|
|
|
|
|
|
|
|
for (double x: numbers)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(number < min)
|
|
|
|
if (x < min)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
min = number;
|
|
|
|
min = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(number > max)
|
|
|
|
else if (x > max)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
max = number;
|
|
|
|
max = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<size_t> bins(bin_count, 0);
|
|
|
|
vector<size_t> make_histogram (vector<double> numbers, size_t bin_count)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
double min, max;
|
|
|
|
|
|
|
|
find_minmax (numbers, min, max);
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
for(size_t i = 0; i < number_count; i++)
|
|
|
|
vector<size_t> bins(bin_count);
|
|
|
|
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto lo = min + j * bin_size;
|
|
|
|
auto lo = min + j * bin_size;
|
|
|
|
auto hi = min + (j + 1) * bin_size;
|
|
|
|
auto hi = min + (j + 1) * bin_size;
|
|
|
|
if((lo <= numbers[i]) && (numbers[i] < hi))
|
|
|
|
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bins[j]++;
|
|
|
|
bins[j]++;
|
|
|
|
found = true;
|
|
|
|
found = true;
|
|
|
@ -51,45 +64,59 @@ int main()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bins[bin_count-1]++;
|
|
|
|
bins[bin_count - 1]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bins;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void show_histogram_text(vector<size_t> bins, size_t bin_count)
|
|
|
|
|
|
|
|
{
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
|
|
|
double max_bin;
|
|
|
|
size_t max_bin = bins[0];
|
|
|
|
size_t height;
|
|
|
|
for(size_t i = 0; i < bin_count; i++)
|
|
|
|
max_bin = bins[0];
|
|
|
|
|
|
|
|
height = bins[0];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < bin_count; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(bins[i] > max_bin)
|
|
|
|
if (bins[i] > max_bin){
|
|
|
|
{
|
|
|
|
|
|
|
|
max_bin = bins[i];
|
|
|
|
max_bin = bins[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (size_t bin: bins)
|
|
|
|
bool flag = false;
|
|
|
|
|
|
|
|
if ( max_bin > 80)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
size_t height = bin;
|
|
|
|
flag = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (max_bin > MAX_ASTERISK)
|
|
|
|
for (size_t i = 0; i < bin_count; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
height = MAX_ASTERISK * (static_cast<double>(bin) / max_bin);
|
|
|
|
if (bins[i] < 100)
|
|
|
|
}
|
|
|
|
cout << " ";
|
|
|
|
|
|
|
|
if (bins[i] < 10)
|
|
|
|
if (bin < 100)
|
|
|
|
cout << " ";
|
|
|
|
|
|
|
|
cout << bins[i] << "|";
|
|
|
|
|
|
|
|
if( flag == true)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cout << ' ';
|
|
|
|
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bin < 10)
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cout << ' ';
|
|
|
|
height = bins[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cout << bin << "|";
|
|
|
|
for (size_t j = 0; j < height; j++)
|
|
|
|
for(size_t i = 0; i < height; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cout << "*";
|
|
|
|
cout << "*";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|