#include #include using namespace std; int main() { size_t bin_count, numbers_count; const size_t SCREEN_WIDTH = 80; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; cerr << "Enter number count:"; cin >> numbers_count; vector numbers ( numbers_count ); for (size_t i=0; i < numbers_count; i++){ cin >> numbers[i]; } cin >> bin_count; vector bins ( bin_count ); double minx = numbers[0]; double maxx = numbers[0]; for (double x : numbers){ if (x < minx){ minx = x; } else if (x > maxx){ maxx = x; } } double bin_size = (maxx - minx) / bin_count; for (size_t i=0; i < numbers_count; i++ ){ bool found = false; for (size_t j = 0; ( j < bin_count - 1 ) && !found; j++ ){ auto lo = minx + j * bin_size; auto hi = minx + ( j + 1 ) * bin_size; if (lo <= numbers[i] && ( numbers[i] < hi )){ bins[j]++; found = true; } } if ( !found ){ bins[bin_count-1]++; } } size_t maxbin = bins[0]; for (size_t i=1; i < bin_count; i++){ if (maxbin < bins[i]){ maxbin = bins[i]; } } if (maxbin <= MAX_ASTERISK){ for (size_t i=0; i < bin_count; i++){ if ((bins[i] < 1000)&&(bins[i] > 99)){ cout << bins[i] << "|"; for ( size_t j=0; j < bins[i]; j++ ){ cout << "*"; } cout << endl; } else if ((bins[i] < 100)&&(bins[i]>9)) { cout << " " << bins[i] << "|"; for ( size_t j=0; j < bins[i]; j++ ){ cout << "*"; } cout << endl; } else if ( bins[i] < 10 ){ cout << " " << bins[i]<< "|"; for ( size_t j=0; j < bins[i]; j++ ){ cout << "*"; } cout << endl; } } } else { for (size_t i=0; i < bin_count; i++){ size_t heightG= MAX_ASTERISK * (static_cast(bins[i]) / maxbin); if ((bins[i] < 1000)&&(bins[i] > 99)){ cout << bins[i] << "|"; for ( size_t j=0; j < heightG; j++ ){ cout << "*"; } cout << endl; } else if ((bins[i] < 100)&&(bins[i]>9)) { cout << " " << bins[i] << "|"; for (size_t j=0; j < heightG; j++){ cout << "*"; } cout << endl; } else if (bins[i] < 10){ cout << " " << bins[i]<< "|"; for (size_t j=0; j < heightG; j++){ cout << "*"; } cout << endl; } } } }