/****************************************************** ** Name: ** Filename: rolldie.cpp ** Project #: Deitel & Deitel 4.17 ** Project Description: Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. Note: Since each die can show an integer value from 1 to 6, then the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. Your program should roll the two dice 36,000 times. Use a single-subscripted array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable(i.e., there are six ways to roll a 7, so approximately one sixth of all the rolls should be 7). ** Output: Results of rolling two dice 36,000 times and the number of each possible outcome. ** Input: None ** Algorithm: Roll two die 36000 times add the number of times each combination of the two dice appears. Store them in the frequency array. Print out the elements of the frequency array skipping element 0 and element 1 since values can only be in elements 2 through 12. ******************************************************/ // Include files #include // used for cin, cout #include #include #include #include using namespace std; // Global Type Declarations // Function Prototypes void instruct (void); void pause (); //Global Variables - should not be used without good reason. int main () { // Declaration section const int arraySize = 13; int die1, die2, frequency[ arraySize ] = { 0 }; // Executable section instruct (); srand( time( 0 ) ); //Roll Dice 36000 times store result in frequency array for ( int roll = 0; roll < 36000; ++roll ) { die1 = 1 + rand() % 6; die2 = 1 + rand() % 6; ++frequency[ die1 + die2 ]; } //Column Headers cout << setw( 10 ) << "Dice Combination" << setw( 17 ) << "Frequency\n\n"; //Start at Element 2 To Skip Elements 0 and Element 1 for ( int combo = 2; combo < arraySize; ++combo ) cout << setw( 9 ) << combo << setw( 18 ) << frequency[ combo ] << endl; pause (); return 0; } void instruct (void) { // Declaration section cout << "This program will simulate the rolling of two dice 36000 times. " << "It will then\nprint out the results of how many times each " << "combination of the two dice\nappeared. You should see in the " << "results that the 7 combination will appear\nthe most with the 2 " << "combination and the 12 combination appearing the least.\n" << "_______________________________________________________________" << "_____________" << "\n" << endl; // Executable section } void pause () { // Declaration section // Executable section cout << "\nPress any key to continue..."; getch(); cout << "\r"; cout << " "; cout << "\r"; } /* Program Output This program will simulate the rolling of two dice 36000 times. It will then print out the results of how many times each combination of the two dice appeared. You should see in the results that the 7 combination will appear the most with the 2 combination and the 12 combination appearing the least. ____________________________________________________________________________ Dice Combination Frequency 2 1024 3 1995 4 3033 5 3939 6 4875 7 6034 8 5082 9 3955 10 3002 11 2037 12 1024 Press any key to continue... */