Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider an electronic “slot machine” that generates three uniformly distributed

ID: 3684313 • Letter: C

Question

Consider an electronic “slot machine” that generates three uniformly distributed random integers from 1 to 10, inclusive. A “Jackpot” occurs whenever all three of the integers that appear are the same. For example, if four attempts are made with this slot machine, it could generate the following four sets of random integers 2 2 2 139 5 5 5 485 In the above example, Jackpots occurred twice, on attempts number 1 and 3. Write a MATLAB program that uses an input N, as the number of attempts, and determines how many times Jackpots occur if N attempts are made. If no Jackpots occur, output a message that states, “Sorry, no Jackpots in N attempts. Please try again.” (You are to replace the N in this statement with the actual input number.) If at least one Jackpot occurs, output a message that tells the user how many total Jackpots occurred and provides a list of which attempts they occurred on and what integer gave the Jackpot. For example, your output might look like this if Jackpots occurred on attempts 1 and 3 as in the above example: Jackpots occurred 2 time(s) on the following attempts: 1 all integers are 2 3 all integers are 5 (The bold typeface is used for emphasis in this problem statement.)

Explanation / Answer

clc
clear

N = input('Enter number of attempts to be made: '); %asking user for no. of attempts
J = [];
I = [];

for i = 1:N; %loop will run to make attempts
    R = randi(10,1,3); %generating 3 random integers betwen 1 to 10
    if R(1)==R(2)&&R(2)==R(3) %checking for jackpot
        J = cat(1,J,i); %saving jackpot attempts in variable J
        I = cat(1,I,R(1)); %saving jackpot integer in variable I
    end
end

Lj = length(J); %calculating no. of Jackpots occured
if Lj==0 %condition for no jackpot
    s = sprintf(' Sorry, no Jackpots in %d attempts. Please try again',N);
    disp(s)
else
    s1 = sprintf(' Jackpots occured %d time(s) on the following attempts:',Lj);
    disp(s1)
    for j = 1:length(J)
        s2 = sprintf('%d all integers are %d.',J(j),I(j));
        disp(s2)
    end
end