Write a program simulates that simulates the following roule
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.

