// // Filename: pgm3_3c.cpp // // Description: Rolling Dice Simulation // // ENSC 104: Digital Computer Programming // // Instructor: Dr. Walsh // // Section: 2 // // Date Created: 03/16/98 // Last Modified: 03/16/98 // // Name: N/A // #include #include #include #include //*************** Function Prototypes ******************* void header( void ); int rollDice( void ); int rollUnfairDice( void ); void displayResults( int, int, int, int, int, int ); main() { int freq1 = 0; int freq2 = 0; int freq3 = 0; int freq4 = 0; int freq5 = 0; int freq6 = 0; int i = 0; int face = 0; srand( time( NULL ) ); header(); for ( i=1; i<=6000; i++ ) { face = rollDice(); // face = rollUnfairDice(); switch (face) { case 1: ++freq1; break; case 2: ++freq2; break; case 3: ++freq3; break; case 4: ++freq4; break; case 5: ++freq5; break; case 6: ++freq6; break; } // End switch } // End for displayResults( freq1, freq2, freq3, freq4, freq5, freq6 ); return 0; } // End main /* * Function: displayResults * * Description: This function displays the outcomes of rolling a dice. */ void displayResults( int f1, int f2, int f3, int f4, int f5, int f6 ) { cout << "Face" << setw(13) << "Frequency\n" << "****" << setw(13) << "*********\n" << " 1" << setw(13) << f1 << endl << " 2" << setw(13) << f2 << endl << " 3" << setw(13) << f3 << endl << " 4" << setw(13) << f4 << endl << " 5" << setw(13) << f5 << endl << " 6" << setw(13) << f6 << endl; } /* * Function: rollDice * * Description: This simulates the rolling a FAIR dice. */ int rollDice( ) { int roll; roll = 1 + rand() % 6; return roll; } /* * Function: rollDice * * Description: This function simulates the rolling an UNFAIR dice. */ int rollUnfairDice() { int roll; int temp; roll = 1 + rand() % 6; temp = 1 + rand() % 10; if ( roll == 1 && temp <= 5 ) roll = 6; return roll; } /* * Function: header * * Description: This function prints out header information describing * what this program does. */ void header( void ) { cout << "**** DEMO PROGRAM: pgm3_3c.cpp ****\n"; cout << "Simulating a FAIR Dice!\n"; cout << "AND Simulating an UNFAIR Dice!\n\n"; }