From 3c5eb2a58cbfe91c5fd6e0bce435c285b54136fd Mon Sep 17 00:00:00 2001 From: NikitkinTY Date: Mon, 24 Apr 2023 01:11:45 +0300 Subject: [PATCH] =?UTF-8?q?git:=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=B5=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=BE=D0=B9=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++ cs-lab34.cbp | 40 ++++++++++++++ main.cpp | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 .gitignore create mode 100644 cs-lab34.cbp create mode 100755 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4caba1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/bin +/obj +/cs-lab34.layout +/cs-lab34.depend diff --git a/cs-lab34.cbp b/cs-lab34.cbp new file mode 100644 index 0000000..49b0653 --- /dev/null +++ b/cs-lab34.cbp @@ -0,0 +1,40 @@ + + + + + + diff --git a/main.cpp b/main.cpp new file mode 100755 index 0000000..dd6aacb --- /dev/null +++ b/main.cpp @@ -0,0 +1,146 @@ +//main.cpp -- the program gets number count, numbers and bin size, then builds a histogram. +//Task 15 -- make a scale under the histogram. + +#include +#include +#include +using namespace std; + +double find_ext(vector array, short key) +{ + double ext = array[0]; //Key "1" keeps the comparison the same. + for(double element : array) //Key "-1" reverses the comparison. + { + if(key * element > key * ext) {ext = element;} + } + return ext; +} + +size_t find_ext(vector array, short key) +{ + size_t ext = array[0]; //Key "1" keeps the comparison the same. + for(double element : array) //Key "-1" reverses the comparison. + { + if(key * element > key * ext) {ext = element;} + } + return ext; +} + + + +int main() { + const size_t screen_width = 80; + const size_t max_asterisk = screen_width - 3 - 1; + + + size_t number_count; //Input variable. + cerr << "Enter number count: "; + cin >> number_count; + vector numbers(number_count); + for (size_t i = 0; i < number_count; ++i) { cin >> numbers[i]; } + size_t bin_count; + cerr << "Enter bin count: "; + cin >> bin_count; + + + double max = find_ext(numbers, 1); //Find min, max and bin size. + double min = find_ext(numbers, -1); //Key 1 stands for maximum, key -1 stands for minimum. + double bin_size = static_cast(max - min) / (bin_count); + + vector bins(bin_count); + + + for(size_t i = 0; i < number_count; ++i) //Checking if a number is in a bin. + { + bool found = false; + for(size_t j = 0; (j < bin_count - 1) && !found; ++j) + { + if( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border + (numbers[i] < (min + (j + 1) * bin_size)) ) //and (min + (j + 1) * bin_size) is equal to the higher border. + { + ++bins[j]; + found = true; + } + } + if(!found) {++bins[bin_count - 1];} //A special case when current number is equal to the maximum. + } + + + + + size_t max_bin = find_ext(bins, 1); //Finds a bin with the maximum size. Key 1 stands for maximum. + double modifier; + + /* + * In case when maximum bin > 76, histogram will be scaled according to + * the formula "max_asterisk * (static_cast(bins[i]) / max_bin)" + * or "bins[i] * (static_cast(max_asterisk) / max_bin)". + * In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number. + */ + + if(max_bin > 76) {modifier = static_cast(max_asterisk) / (max_bin);} + else {modifier = 1;} + + for(long unsigned int i = 0; i < bin_count; ++i) //Histogram output with alignment, if necessary. + { + if(bins[i] >= 10) + { + if(bins[i] >= 100) {cout << bins[i] << '|';} //Output a three-digit number. + else {cout << ' ' << bins[i] << '|';} //Output a two-digit number with alignment. + } + else {cout << " " << bins[i] << '|';} //Output a single-digit number with alignment. + + + size_t height = modifier * bins[i]; //Height stands for the number of output asterisks. + for(long unsigned int k = 0; k < height; ++k) + cout << '*'; + cout << "\n"; + } + + + + //Task 15. + + + size_t interval_task; + cerr << "Enter interval size: "; + cin >> interval_task; + + if((interval_task >= 4) && (interval_task <= 9)) //The scale under the histogram. + { + size_t times; + if(modifier == 1) {times = static_cast(ceil(max_bin / interval_task) + 1);} + else {times = static_cast(ceil(max_asterisk / interval_task) + 1);} + + + + cout << " |"; //1st row output. + for(long unsigned int i = 0; i < times; ++i) + { + for(long unsigned int k = 0; k < interval_task - 1; ++k) + cout << '*'; + cout << '|'; + } + cout << '\n'; + + + + cout << " " << 0; //2nd row output. + for(long unsigned int i = 0; i < (interval_task - 1); ++i) //Distance from the first axis to the second. + cout << ' '; + cout << interval_task; + + // (Number of intervals between the second and last * Interval size) - Last axis. + + if(times > 1) + { + for(long unsigned int i = 0; i < (times - 1) * (interval_task) - 1; ++i) + cout << ' '; + cout << times * interval_task; + } + } + + else {cout << "ERROR";} + + return 0; +}