Родитель
							
								
									5c2c3c19c4
								
							
						
					
					
						Сommit
						6161e2abe0
					
				| @ -0,0 +1,65 @@ | ||||
| #include "histogram.h" | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| using namespace std; | ||||
| 
 | ||||
| //std::vector<size_t>
 | ||||
| bool find_minmax(const vector<double>& numbers, double& minN, double& maxN) | ||||
| { | ||||
|     if (numbers.empty()) { | ||||
|         minN = maxN = 0; | ||||
|         return false; | ||||
|     } | ||||
|     minN = numbers[0]; | ||||
|     maxN = numbers[0]; | ||||
| 
 | ||||
|     for (double x: numbers) | ||||
|     { | ||||
|         if (minN > x) | ||||
|         { | ||||
|             minN = x; | ||||
|         } | ||||
|         if (maxN < x) | ||||
|         { | ||||
|             maxN = x; | ||||
|         } | ||||
|     } | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
| vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) | ||||
| { | ||||
|     double minN, maxN; | ||||
|     find_minmax( numbers, minN, maxN); | ||||
| 
 | ||||
|     vector <size_t> bins(bin_count); | ||||
|     double diff = (maxN - minN) / bin_count; | ||||
|     size_t max_count = 0; | ||||
|     for (size_t i = 0; i <numbers.size() ; i++) | ||||
|     { | ||||
|         bool found = false; | ||||
|         for (size_t j = 0; (j < bin_count - 1) && !found; j++) | ||||
|         { | ||||
|             auto lo = minN + j * diff; | ||||
|             auto hi = minN + (j + 1) * diff; | ||||
|             if ((lo <= numbers[i]) && (hi > 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; | ||||
| } | ||||
					Загрузка…
					
					
				
		Ссылка в новой задаче