//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; }