# include # include using namespace std; int main ( ) // // THREE_HEADS.CPP simulates a situation in which we flip three coins, // and win if we get three heads. // // Each game costs 25 cents, and we keep playing until we win 10 times. // // By using a "success" counter, the WHILE loop can repeat the statements // until we win the desired number of times. // { int coin1, coin2, coin3, tries, wins; float cost; // // Flip 3 coins, get three heads to win. // cost = 0.0; tries = 0; wins = 0; srand ( time ( 0 ) ); while ( wins < 10 ) { // // Increase the loop counter. // tries = tries + 1; cost = cost + 0.25; coin1 = rand ( ); coin2 = rand ( ); coin3 = rand ( ); // // Each coin variable is a random integer. // We will assume a coin comes up "Heads" if the random integer is odd. // // We use the "%" operator to determine if each integer is odd. // We use the "&&" operator to say AND. // if ( coin1 % 2 == 0 && coin2 % 2 == 0 && coin3 % 2 == 0 ) { wins = wins + 1; } } cout << "\n"; cout << "I had to play " << tries << " times.\n"; cout << "Winning " << wins << " times, I spent $" << cost << "\n"; return 0; }