Description
The purpose of this challenge is examine the output and write the C++ source code that produces the given output and interaction.
Requirements
- You may use any combination of variable declarations, assignments, conditional statements, expressions, loops, etc
- Include <cstdlib> and <ctime> to enable the use of the built-in random number generator. See below on how to use the random number generator to have the computer produce integers within a given range.
#include <iostream> #include <cstdlib> #include <ctime> int main() { int v1, v2, v3; srand(time(NULL)); // these are examples on how to use the rand() function // v1, v2 and v3 are assigned random numbers below in the // ranges shown in the comments v1 = rand() % 100; // v1 in the range 0 to 99 v2 = rand() % 100 + 1; // v2 in the range 1 to 100 v3 = rand() % 2; // v3 in the range 0 to 1 return 0; }
Sample Interaction / Output
$ [ run your program ] You have $20.00 in your account I'm going to simulate flipping a coin repeatedly. If it comes up heads, you get $1 If it comes up tails twice in a row, you lose $2 If it comes up heads twice in a row, you get $2 If it comes up heads three times in a row, you get $10. When this happens, I will ask you a question that will determine your fate. I'm going to flip a coin until you run out of money or you get up to $50. If you get to $50, you win. If you lose all your money, I win. Here we go! HEADS - You now have $21 TAILS TAILS - You now have $19 HEADS - You now have $20 HEADS - You now have $21 TAILS HEADS - You now have $22 HEADS - You now have $23 HEADS - You now have $33 Wow, 3 HEADS in a row! Do you think the next flip will be heads? If you guess right, you win. Otherwise, it's over. Guess the next flip? heads YOU WIN! Play again (y/n)? y Resetting your account to $20 Here we go! TAILS TAILS - You now have $18 TAILS TAILS - You now have $16 HEADS - You now have $17 TAILS TAILS - You now have $15 TAILS TAILS - You now have $13 TAILS TAILS - You now have $11 TAILS TAILS - You now have $9 TAILS TAILS - You now have $7 TAILS TAILS - You now have $5 TAILS TAILS - You now have $3 TAILS TAILS - You now have $1 TAILS TAILS - You now have $-1
$ [ back to the command prompt ]
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-BB0002
Print Requirements