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

Using MatLab: You are playing a game where you roll a die 10 times. If you roll

ID: 3600498 • Letter: U

Question

Using MatLab:

You are playing a game where you roll a die 10 times. If you roll 5 or 6 seven or more times, you win 2 dollars; four or more times, you win 1 dollar; and if you roll a 5 or 6 three or less times, you win no money. Write a function called diceGame that takes in a vector representing the die values and returns the amount of money won.

For example:

diceGame ([5 1 4 6 5 5 6 6 5 2]) should return 2

diceGame ([2 4 1 3 6 6 6 4 5 3]) should return 1

Note: This function should work for any length vector.

Explanation / Answer

function [dollar] = diceGame(a)
count = 0
for i = 1:length(a)
if(a(i) == 5 || a(i) == 6)
count = count + 1
end
end
if(count >= 7)
dollar = 2
elseif (count >= 4 && count < 7)
dollar = 1
else
dollar = 0
end
end


diceGame ([5 1 4 6 5 5 6 6 5 2])
diceGame ([2 4 1 3 6 6 6 4 5 3])

Output:

octave: X11 DISPLAY environment variable not set
octave: disabling GUI features
count = 0
count = 1
count = 2
count = 3
count = 4
count = 5
count = 6
count = 7
dollar = 2
ans = 2
count = 0
count = 1
count = 2
count = 3
count = 4
dollar = 1
ans = 1