diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..d437f30 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,48 @@ +#include "histogram.h" +#include +#include +using namespace std; +void find_minmax( const vector& numbers, double& minN, double& maxN) { + minN = numbers[0]; + maxN = numbers[0]; + + for (double x: numbers){ + if (minN > x){ + minN = x; + } + if (maxN < x){ + maxN = x; + } + } + +} + +vector make_histogram(const vector& numbers, size_t bin_count){ + double minN, maxN; + find_minmax( numbers, minN, maxN); + + vector bins(bin_count); + double diff = (maxN - minN) / bin_count; + size_t max_count = 0; + for (size_t i = 0; i numbers[i])){ + bins[j]++; + if (bins[j] > max_count){ + max_count = bins[j]; + } + found = true; + } + } + if(!found){ + bins[bin_count - 1]++; + if (bins[bin_count - 1] > max_count){ + max_count = bins[bin_count - 1]; + } + } +} +return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..7ceb169 --- /dev/null +++ b/histogram.h @@ -0,0 +1,6 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED +#include +std::vector make_histogram(const std::vector &numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..08bf2ee --- /dev/null +++ b/text.cpp @@ -0,0 +1,41 @@ +#include "text.h" +#include +#include +using namespace std; +void show_histogram_text(const vector & bins,size_t bin_count, size_t max_count){ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 6 - 1; + + bool scaling = false; + + if (max_count > MAX_ASTERISK){ + scaling = true; + } + + for (size_t i = 0; i < bin_count; i++){ + cout << " "; + if (bins[i] < 100){ + cout << ' '; + } + if (bins[i] < 10){ + cout << ' '; + } + cout << bins[i] << '|'; + + size_t number_of_stars = bins[i]; + + if (scaling){ + if (bins[i] == max_count){ + number_of_stars = MAX_ASTERISK * 1.0; + } + else { + number_of_stars = MAX_ASTERISK * (static_cast(bins[i]) / max_count); + } + } + + for (size_t j = 0; j < number_of_stars; j++){ + cout << '*'; + } + cout << endl; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..a619492 --- /dev/null +++ b/text.h @@ -0,0 +1,6 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED +#include +void show_histogram_text(const std::vector & bins,size_t bin_count, size_t max_count); + +#endif // TEXT_H_INCLUDED