You recently won $50 in a raffle. Instead of putting it in the bank like you know you should, you decide to go down to the racetrack and see how many races your money can last. The race track has horse races all summer long with each race consisting of 5 horses. You must wager $10 on each race. The horses with the highest chance of winning will always have lower payouts, while the horses with the lowest chance of winning will always have the highest payouts.
Assume the following conditions are true for all races:
- The horse in gate 1 will always have a 16% of winning and a payout of $35.
- The horse in gate 2 will always have a 20% of winning and a payout of $30.
- The horse in gate 3 will always have a 28% of winning and a payout of $20.
- The horse in gate 4 will always have a 24% of winning and a payout of $25.
- The horse in gate 5 will always have a 12% of winning and a payout of $40.Write a MATLAB program that will ask for the user to pick a horse (1, 2, 3, 4, or 5) and then will use the “rand” or “randi” command to determine the winner of the race. After each race, the program will:
1) Adjust the total money accordingly (deduct bet, add payout)
2) Display the balance to the user
3) Ask the user to pick a new horse
4) Continue with the next race with a new random number determining the winning horse
This will continue until a balance of under $10 is reached. When a balance under $10 is reached, have MATLAB output to the user:
1) How many races their money lasted and
2) The maximum possible they could have won if they guessed correctly every time
cash=20;maxcash=0;attempts=0;disp('So you''ve got $20 and you''re a betting man/woman?')disp(' ')disp('Tell you what - I''ll flip a coin and you guess heads or tails.')disp(' ')disp('If you guess right, I''ll give you $5.')disp('If you guess wrong, I''ll take your $5.')disp(' ')disp('Let''s begin!')disp(' ')disp(' ')%To flip a coin, say 1=heads and 2=tails, ask the user to guess:
guess=input('Please guess heads (1) or tails (2) for random coin flip: ');%I want the code to iterate and let the user keep playing until they run
%out of money or quit:
while cash>=5 %We'll say it's $5 to bet each time
clc cash=cash-5; %Take out $5 for starting bet
chance=randi(2); %Chance is either 1 or 2, 50% even chance for either
%chance=rand %Chance is a random number between 0 and 1
attempts=attempts+1; if chance==1 %Hitting heads
flip=1; elseif chance==2 flip=2; end if flip==guess %User guesses correctly
if flip==1 %Hitting heads cash=cash+10; %They double their bet if they win
elseif flip==2 %Hitting tails
cash=cash+10; %Same as above
end disp('Congrats, you guessed it!') disp(['Your new cash total is $' num2str(cash) '!']) elseif guess<1 || guess>2 disp('You didn''t follow instructions...') disp('Thanks for the $5 donation!') disp(['Your new cash total is $' num2str(cash) '!']) else disp('Sorry, you guessed incorrectly.. =(') disp(['Your new cash total is $' num2str(cash) '!']) end if flip==1 %This if loop to capture max possible amount you could win
maxcash=maxcash+5; elseif flip==2 maxcash=maxcash+5; end if cash>=5 %This IF loop will cancel prompting the user if they run out of money
disp('Please guess again for the next coin flip.') guess=input('Put 1 for heads and 2 for tails: '); endend %I want to display the result to the user:
disp(' ')disp(['You made it for ' num2str(attempts) ' coin flips.'])disp(' ')disp(['The maximum you could have possibly won is:'])disp(['$',num2str(maxcash)])
Best Answer