Родитель
17909b49ef
Сommit
efadf5adc7
@ -0,0 +1,45 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <cmath>
|
||||||
|
#include "histogram.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
static void find_minmax(const vector<double>& numbers, double& mini, double& maxi) {
|
||||||
|
|
||||||
|
mini = numbers[0];
|
||||||
|
maxi = numbers[0];
|
||||||
|
for (double x : numbers) {
|
||||||
|
if (x < mini) {
|
||||||
|
mini = x;
|
||||||
|
}
|
||||||
|
else if (x > maxi) {
|
||||||
|
maxi = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vector<size_t>
|
||||||
|
make_histogram(const vector<double>& numbers, size_t bin_count) {
|
||||||
|
double mini, maxi;
|
||||||
|
find_minmax(numbers, mini, maxi);
|
||||||
|
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
|
|
||||||
|
double bin_size = (maxi - mini) / bin_count;
|
||||||
|
for (double num : numbers) {
|
||||||
|
bool found = false;
|
||||||
|
for (size_t j = 0; (j < bin_count - 1) && !found; j++){
|
||||||
|
auto lo = mini + j * bin_size;
|
||||||
|
auto hi = mini + (j + 1) * bin_size;
|
||||||
|
if ((lo <= num) && (num < hi)) {
|
||||||
|
bins[j]++;
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
bins[bin_count - 1]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bins;
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::vector<size_t>
|
||||||
|
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
@ -0,0 +1,38 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <cmath>
|
||||||
|
#include "text.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(const vector<size_t>& bins, size_t bin_count) {
|
||||||
|
size_t max_count = bins[0];
|
||||||
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
|
if (bins[i] > max_count) {
|
||||||
|
max_count = bins[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t> height(bin_count);
|
||||||
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
|
if (max_count > MAX_ASTERISK) {
|
||||||
|
height[i] = round(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
|
||||||
|
} else {
|
||||||
|
height[i] = bins[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
|
if (bins[i] < 10) {
|
||||||
|
cout << " ";
|
||||||
|
} else if (bins[i] < 100) {
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
cout << bins[i] << "| ";
|
||||||
|
|
||||||
|
for (size_t j = 0; j < height[i]; j++) {
|
||||||
|
cout << " * ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(const vector<size_t>& bins, size_t bin_count);
|
Загрузка…
Ссылка в новой задаче