Write a program simulates that simulates the following roule

Write a program simulates that simulates the following roulette betting strategy, starting with a given initial balance greaterthanorequalto $1: Let n = $1 Place n on \"Odd\" If the round is won, then n will be added to the balance: and the next bet is n = $1 If the round is lost, then n will be subtracted from the balance; and the next bet is n = min(2 * n, balance) Repeat steps 2-4 until either you have doubled the initial amount (i.e. balance is twice the initial balance), or balance is $0 Your program should use a random number generator to simulate the result of spinning the wheel in each round, which is a number between 0 and 36 (including both 0 and 36). A round is won if this number is odd. A round is lost if the number is even (including zero). Your program should output the bet, the winning number and the new balance after each round. Your program should accept two integer command line arguments: The first argument is the initial balance The second argument is used as the seed for the random number generator. Examples of program executions are: prompt$ ./simulate 3 4567 Bet: $1. Winning number: 29. New balance: $4 Bet: $1. Winning number: 0. New balance: $3 Bet: $2. Winning number: 25. New balance: $5 Bet: $1. Winning number: 31. New balance: $6 prompt$ ./simulate 3 2234 Bet: $1. Winning number: 16. New balance: $2 Bet: $2. Winning number: 1. New balance: $4 Bet: $1. Winning number: 9. New balance: $5 Bet: $1. Winning number: 0. New balance: $4 Bet: $2. Winning number: 10. New balance: $2 Bet: $2. Winning number: 26. New balance: $0 prompt$ ./simulate 100 16 Bet: $1. Winning number: 0. New balance: $99 Bet: $2. Winning number: 2. New balance: $97 Bet: $4. Winning number: 18. New balance: $93 Bet: $8. Winning number: 2. New balance: $85 Bet: $16. Winning number: 14. New balance: $69 Bet: $32. Winning number: 6. New balance: $37 Bet: $37. Winning number: 8. New balance: $0 If there are not exactly two arguments or one of them is not a positive integer, then a usage error message should be generated prompt$ ./simulate 100 Usage: ./simulate initial balance random seed prompt$ ./simulate 100 16 1 Usage: ./simulate initial balance random seed prompt$ ./simulate 100 x Usage: ./simulate initial balance random seed

Solution

Please follow the code and comments for description :

CODE :

#include <stdio.h> // required header files
#include <time.h>
#include <stdlib.h>

int min(int x, int y) // method to return the minimum of the two values passed
{
return y ^ ((x ^ y) & -(x < y));
}

int main() // driver method
{
int initialBal, n = 1, newBal, run; // required initialisations
time_t Seed;
scanf (\"%d\", &initialBal); // getting the values form the user
scanf(\"%d\", &Seed);
srand((unsigned) time(&Seed)); // srand function for the given seed
if((initialBal) && (initialBal > 0) && (Seed) && (Seed > 0)) { // checking for the input values entered by the user
while(newBal != 2 * initialBal){ // iterating over the loop till the balance gets doubles
run = rand() % 36; // generating a random number for each iteration
if(run % 2 == 0 || run == 0){ // checking if the values is an even or odd
newBal = initialBal - n; // if lost reducing the balance
printf(\"\ Bet : $%d. Winning Number : %d. New Balance : $%d.\ \", n, run, newBal); // printing the data analysis
n = min(2 * n, initialBal); // refreshing the bet value for hte next turn
} else {
newBal = n + initialBal; // if won incrementing the balance
printf(\"\ Bet : $%d. Winning Number : %d. New Balance : $%d.\ \", n, run, newBal); // printing the data analysis
}
if(newBal == 0) { // checking if the balance is 0
exit(0); // if so exit the code
}
}
} else { // if the user values do not satisfy the condition returning a message to the console
printf(\"Initial Balance RandomSeed.\");
}
   return 0;
}

OUTPUT :

5
4567

Bet : $1. Winning Number : 35. New Balance : $6.

Bet : $1. Winning Number : 35. New Balance : $6.

Bet : $1. Winning Number : 3. New Balance : $6.

Bet : $1. Winning Number : 34. New Balance : $4.

Bet : $2. Winning Number : 29. New Balance : $7.

Bet : $2. Winning Number : 7. New Balance : $7.

Bet : $2. Winning Number : 33. New Balance : $7.

Bet : $2. Winning Number : 4. New Balance : $3.

Bet : $4. Winning Number : 25. New Balance : $9.

Bet : $4. Winning Number : 9. New Balance : $9.

Bet : $4. Winning Number : 28. New Balance : $1.

Bet : $5. Winning Number : 3. New Balance : $10.


Hoep this is helpful.

 Write a program simulates that simulates the following roulette betting strategy, starting with a given initial balance greaterthanorequalto $1: Let n = $1 Pla
 Write a program simulates that simulates the following roulette betting strategy, starting with a given initial balance greaterthanorequalto $1: Let n = $1 Pla

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site