// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 79. #include #include #include using namespace std; int roll(int dice[], const int num_dice); int print_histogram(const int num_bins, const int bin_array[]); int main() { const int max_dice = 12; int dice[max_dice]; const int possible_outcomes = max_dice*6; int freq_counters[possible_outcomes+1]; // Extra 1 is because outcomes will go from 1 to possible_outcomes, // not from 0 to possible_outcomes-1 int num_dice; int num_rolls; int roll_cnt; int i; cout << "Enter number of dice (1 - " << max_dice << "): "; cin >> num_dice; if ((num_dice < 1)||(num_dice > max_dice)) { cout << "Sorry. Can only handle between 1 and " << max_dice << " dice\n"; return 0; } cout << "Enter number of rolls: "; cin >> num_rolls; if (num_rolls < 1) { cout << "Cannot do less than 1 roll!\n"; return 0; } srand((int) time(NULL)); // Seed rand number generator for (i = 0; i < possible_outcomes+1; i++) // Initialize range of cnts freq_counters[i] = 0; for (roll_cnt = 0; roll_cnt < num_rolls; roll_cnt++) { roll(dice, num_dice); int sum = 0; for (i = 0; i < num_dice; i++) sum += dice[i]; freq_counters[sum]++; } print_histogram(num_dice*6,freq_counters); return 1; } int roll(int dice[], const int num_dice) { for (int i = 0; i < num_dice; i++) { dice[i] = rand()/(RAND_MAX/6) + 1; } return 0; } int print_histogram(const int num_bins, const int bin_array[]) { const int max_height = 25; // Desired height of histogram const int line_length = 79; // Length of a text line char outline[line_length+1]; int i; // First, clear outline for (i = 0; i < line_length; i++) outline[i] = ' '; outline[line_length] = '\0'; // Null termination // Find highest peak in histogram int height = -1; for (i = 0; i < num_bins; i++) { if (bin_array[i] > height) height = bin_array[i]; } // Now make the histogram fit in max_height: // On each iteration decrement the height threshold by // height_dec - set so histogram will occupy max_height lines int height_dec = height/max_height; if (height_dec < 1) height_dec = 1; while(height > 0) { for (i = 0; i <= num_bins; i++) { if (bin_array[i] >= height) outline[i] = '*'; } cout << outline << '\n'; height -= height_dec; } return 0; } // Example run of the program: // (Distribution may appear skewed when printed with variable-width font) // // Enter number of dice (1 - 12): 8 // Enter number of rolls: 9999 // * // *** // ***** // ***** // ******* // ******* // ******* // ********* // ********* // ********* // *********** // *********** // *********** // ************* // ************* // ************* // *************** // *************** // *************** // ***************** // ***************** // ******************* // ********************* // ********************* // ************************* // ************************************ //