// // Filename: pgm3_3d.cpp // // Description: Rolling Dice Simulation #2 // // 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 rollTwoDice( void ); void displayResults( int, int, int, int, int, int, int, int, int, int, int ); main() { int freq2 = 0; int freq3 = 0; int freq4 = 0; int freq5 = 0; int freq6 = 0; int freq7 = 0; int freq8 = 0; int freq9 = 0; int freq10 = 0; int freq11 = 0; int freq12 = 0; int i = 0; int total = 0; srand( time( NULL ) ); header(); for ( i=1; i<=10000; i++ ) { total = rollTwoDice(); switch (total) { case 2: ++freq2; break; case 3: ++freq3; break; case 4: ++freq4; break; case 5: ++freq5; break; case 6: ++freq6; break; case 7: ++freq7; break; case 8: ++freq8; break; case 9: ++freq9; break; case 10: ++freq10; break; case 11: ++freq11; break; case 12: ++freq12; break; } // End switch } // End for displayResults( freq2, freq3, freq4, freq5, freq6, freq7, freq8, freq9, freq10, freq11, freq12 ); return 0; } // End main void displayResults( int f2, int f3, int f4, int f5, int f6, int f7, int f8, int f9, int f10, int f11, int f12 ) { cout << "Face" << setw(13) << "Frequency\n" << "****" << setw(13) << "*********\n" << " 2" << setw(13) << f2 << ", Prob of a 2 = " << (float) f2/10000 << endl << " 3" << setw(13) << f3 << ", Prob of a 3 = " << (float) f3/10000 << endl << " 4" << setw(13) << f4 << ", Prob of a 4 = " << (float) f4/10000 << endl << " 5" << setw(13) << f5 << ", Prob of a 5 = " << (float) f5/10000 << endl << " 6" << setw(13) << f6 << ", Prob of a 6 = " << (float) f6/10000 << endl << " 7" << setw(13) << f7 << ", Prob of a 7 = " << (float) f7/10000 << endl << " 8" << setw(13) << f8 << ", Prob of a 8 = " << (float) f8/10000 << endl << " 9" << setw(13) << f9 << ", Prob of a 9 = " << (float) f9/10000 << endl << "10" << setw(13) << f10 << ", Prob of a 10 = " << (float)f10/10000 << endl << "11" << setw(13) << f11 << ", Prob of a 11 = " << (float)f11/10000 << endl << "12" << setw(13) << f12 << ", Prob of a 12 = " << (float)f12/10000 << endl; } int rollTwoDice( ) { int face1; int face2; int ttl; face1 = 1 + rand() % 6; face2 = 1 + rand() % 6; ttl = face1 + face2; return ttl; } void header( void ) { cout << "**** DEMO PROGRAM: pgm3_3d.cpp ****\n"; cout << "Simulating a Dice #2!\n\n"; }