# include # include using namespace std; int main ( ) // // GATES_PLUSPLUS.CPP uses the increment and decrement operators as much // as possible. // // Compare the original program, GATES.CPP. // // We are going to win $1,000 from Bill Gates by betting on a coin toss. // // If we don't win the first toss, we try again...and again. // // To simulate a coin toss, we call "rand()", which returns a random // integer. If the integer is odd, we win the toss. // In order to get a different random number sequence each time we play, // we have to call the "srand ( )" function. { int bet; int coin; int tries; int won; srand ( time ( 0 ) ); won = 0; bet = 1000; tries = 1; coin = rand ( ); while ( coin % 2 == 0 ) { won -= bet; // won = won - bet; bet *= 2; // bet = 2 * bet; tries++; // tries = tries + 1; coin = rand ( ); } won += bet; // won = won + bet; cout << "\n"; cout << "Success!\n"; cout << "After " << tries << " coin flips, Bill Gates paid us $" << bet << "\n"; cout << "Our total winnings are " << won << "\n"; return 0; }