|  |  |  | @ -3,72 +3,63 @@ | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | #include <iostream> | 
			
		
	
		
			
				
					|  |  |  |  | #include <vector> | 
			
		
	
		
			
				
					|  |  |  |  | #include <math.h> | 
			
		
	
		
			
				
					|  |  |  |  | using namespace std; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | double find_ext(vector <double> 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 <size_t> 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; | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | using namespace std; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | struct Input { | 
			
		
	
		
			
				
					|  |  |  |  |     vector<double> numbers; | 
			
		
	
		
			
				
					|  |  |  |  |     size_t bin_count{}; | 
			
		
	
		
			
				
					|  |  |  |  |     //size_t interval_task{};
 | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 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: "; | 
			
		
	
		
			
				
					|  |  |  |  | Input | 
			
		
	
		
			
				
					|  |  |  |  | input_data() { | 
			
		
	
		
			
				
					|  |  |  |  |     size_t number_count; | 
			
		
	
		
			
				
					|  |  |  |  |     cin >> number_count; | 
			
		
	
		
			
				
					|  |  |  |  |     vector<double> 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; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     Input in; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     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<double>(max - min) / (bin_count); | 
			
		
	
		
			
				
					|  |  |  |  |     in.numbers.resize(number_count); | 
			
		
	
		
			
				
					|  |  |  |  |     for (size_t i = 0; i < number_count; i++) { | 
			
		
	
		
			
				
					|  |  |  |  |         cin >> in.numbers[i]; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     vector <size_t> bins(bin_count); | 
			
		
	
		
			
				
					|  |  |  |  |     cin >> in.bin_count; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     //cin >> in.interval_task;
 | 
			
		
	
		
			
				
					|  |  |  |  |     return in; | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     for(size_t i = 0; i < number_count; ++i)                                //Checking if a number is in a bin.
 | 
			
		
	
		
			
				
					|  |  |  |  | void | 
			
		
	
		
			
				
					|  |  |  |  | find_minmax(const std::vector<double>& numbers, double& min, double& max) { | 
			
		
	
		
			
				
					|  |  |  |  |     min = numbers[0]; | 
			
		
	
		
			
				
					|  |  |  |  |     for(double element : numbers) | 
			
		
	
		
			
				
					|  |  |  |  |     { | 
			
		
	
		
			
				
					|  |  |  |  |         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.
 | 
			
		
	
		
			
				
					|  |  |  |  |         if(element < min) {min = element;} | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     max = numbers[0]; | 
			
		
	
		
			
				
					|  |  |  |  |     for(double element : numbers) | 
			
		
	
		
			
				
					|  |  |  |  |     { | 
			
		
	
		
			
				
					|  |  |  |  |         if(element > max) {max = element;} | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | void | 
			
		
	
		
			
				
					|  |  |  |  | find_max(const std::vector<std::size_t>& numbers, std::size_t& max) { | 
			
		
	
		
			
				
					|  |  |  |  |     max = numbers[0]; | 
			
		
	
		
			
				
					|  |  |  |  |     for(double element : numbers) | 
			
		
	
		
			
				
					|  |  |  |  |     { | 
			
		
	
		
			
				
					|  |  |  |  |         if(element > max) {max = element;} | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     size_t max_bin = find_ext(bins, 1);                                     //Finds a bin with the maximum size. Key 1 stands for maximum.
 | 
			
		
	
		
			
				
					|  |  |  |  | void | 
			
		
	
		
			
				
					|  |  |  |  | show_histogram_text(const std::vector<std::size_t>& bins){ | 
			
		
	
		
			
				
					|  |  |  |  |     std::size_t max_bin; | 
			
		
	
		
			
				
					|  |  |  |  |     find_max(bins, max_bin); | 
			
		
	
		
			
				
					|  |  |  |  |     double modifier; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /*
 | 
			
		
	
	
		
			
				
					|  |  |  | @ -78,69 +69,61 @@ int main() { | 
			
		
	
		
			
				
					|  |  |  |  |      * 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<double>(max_asterisk) / (max_bin);} | 
			
		
	
		
			
				
					|  |  |  |  |     if(max_bin > 76) {modifier = static_cast<double>(74) / (max_bin);} | 
			
		
	
		
			
				
					|  |  |  |  |     else {modifier = 1;} | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     for(long unsigned int i = 0; i < bin_count; ++i)                                      //Histogram output with alignment, if necessary.
 | 
			
		
	
		
			
				
					|  |  |  |  |     for(std::size_t i = 0; i < bins.size(); ++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.
 | 
			
		
	
		
			
				
					|  |  |  |  |             if(bins[i] >= 100) {std::cout << bins[i] << '|';}                    //Output a three-digit number.
 | 
			
		
	
		
			
				
					|  |  |  |  |             else {std::cout << ' ' << bins[i] << '|';}                           //Output a two-digit number with alignment.
 | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         else {cout << "  " << bins[i] << '|';}                              //Output a single-digit number with alignment.
 | 
			
		
	
		
			
				
					|  |  |  |  |         else {std::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"; | 
			
		
	
		
			
				
					|  |  |  |  |         for(size_t k = 0; k < height; ++k) | 
			
		
	
		
			
				
					|  |  |  |  |             std::cout << '*'; | 
			
		
	
		
			
				
					|  |  |  |  |         std::cout << "\n"; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | std::vector<std::size_t> | 
			
		
	
		
			
				
					|  |  |  |  | make_histogram(const std::vector<double>& numbers, std::size_t bin_count){ | 
			
		
	
		
			
				
					|  |  |  |  |     double max; | 
			
		
	
		
			
				
					|  |  |  |  |     double min; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     find_minmax(numbers, min, max); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     //Task 15.
 | 
			
		
	
		
			
				
					|  |  |  |  |     double bin_size = static_cast<double>(max - min) / (bin_count); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     std::vector <std::size_t> bins(bin_count); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     size_t interval_task; | 
			
		
	
		
			
				
					|  |  |  |  |     cerr << "Enter interval size: "; | 
			
		
	
		
			
				
					|  |  |  |  |     cin >> interval_task; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if((interval_task >= 4) && (interval_task <= 9))                        //The scale under the histogram.
 | 
			
		
	
		
			
				
					|  |  |  |  |     for(std::size_t i = 0; i < numbers.size(); ++i)                                //Checking if a number is in a bin.
 | 
			
		
	
		
			
				
					|  |  |  |  |     { | 
			
		
	
		
			
				
					|  |  |  |  |         size_t times; | 
			
		
	
		
			
				
					|  |  |  |  |         if(modifier == 1) {times = static_cast<size_t>(ceil(max_bin / interval_task) + 1);} | 
			
		
	
		
			
				
					|  |  |  |  |         else {times = static_cast<size_t>(ceil(max_asterisk / interval_task) + 1);} | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         cout << "   |";                                                     //1st row output.
 | 
			
		
	
		
			
				
					|  |  |  |  |         for(long unsigned int i = 0; i < times; ++i) | 
			
		
	
		
			
				
					|  |  |  |  |         bool found = false; | 
			
		
	
		
			
				
					|  |  |  |  |         for(std::size_t j = 0; (j < bin_count - 1) && !found; ++j) | 
			
		
	
		
			
				
					|  |  |  |  |         { | 
			
		
	
		
			
				
					|  |  |  |  |             for(long unsigned int k = 0; k < interval_task - 1; ++k) | 
			
		
	
		
			
				
					|  |  |  |  |                 cout << '*'; | 
			
		
	
		
			
				
					|  |  |  |  |             cout << '|'; | 
			
		
	
		
			
				
					|  |  |  |  |             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; | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         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; | 
			
		
	
		
			
				
					|  |  |  |  |         if(!found) {++bins[bin_count - 1];}                                 //A special case when current number is equal to the maximum.
 | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     return bins; | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         //  (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";} | 
			
		
	
		
			
				
					|  |  |  |  | int main() { | 
			
		
	
		
			
				
					|  |  |  |  |     auto in = input_data(); | 
			
		
	
		
			
				
					|  |  |  |  |     auto bins = make_histogram(in.numbers, in.bin_count); | 
			
		
	
		
			
				
					|  |  |  |  |     show_histogram_text(bins); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     return 0; | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
	
		
			
				
					|  |  |  | 
 |