// lab03.cpp adolfo@di-mare.com // ftp://ftp.prenhall.com/pub/deitel/C++_HTP/C++HTP2e/c++htp2e_examples.zip // Fig. 3.8: fig03_08.cpp // Roll a six-sided die 6000 times #include // cout #include // setw() #include // rand() const FACES=6; const TIMES=6000; int main_switch() { int f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0, f6 = 0, face; for ( int roll = 0; roll < TIMES; ++roll ) { face = 1 + rand() % FACES; switch ( face ) { case 1: ++f1; break; case 2: ++f2; break; case 3: ++f3; break; case 4: ++f4; break; case 5: ++f5; break; case 6: ++f6; break; default: cout << "NUNCA\n"; } } cout << "\nFace" << setw( 13 ) << "f" << "\n 1" << setw( 13 ) << f1 << "\n 2" << setw( 13 ) << f2 << "\n 3" << setw( 13 ) << f3 << "\n 4" << setw( 13 ) << f4 << "\n 5" << setw( 13 ) << f5 << "\n 6" << setw( 13 ) << f6 << endl; return 0; } // main_switch() int main_vector() { int freq[FACES]; int face; for (int roll = 0; roll < FACES; ++roll ) { freq[roll] = 0; } for (roll = 0; roll < TIMES; ++roll ) { face = rand() % FACES; ++freq[face]; } cout << "\nFace" << setw( 13 ) << "f"; for (roll = 0; roll < FACES; ++roll ) { cout << "\n " << (1+roll) << setw( 13 ) << freq[roll]; } cout << endl; return 0; } // main_vector() int main_break() { int i= -500; while (0==0) { // forever... if (i > 3) { break; // salida única del ciclo } ++i; } cout << endl << "i == " << i << endl; return i; } // main_break() int main () { return main_switch(); return main_break(); return main_vector(); } // EOF: lab03.cpp