Question

Use matlab please Write a function mydicegame that takes an input vector d of 5 numbers...

Use matlab please

Write a function mydicegame that takes an input vector d of 5 numbers (representing a roll of 5 dice) and generates the following score s for each roll as an output:

-50 points if all the dice are the same value

-30 points if they are all consecutive values (e.g. 1 2 3 4 5 or 2 3 4 5 6)

-5 points if two of the numbers are the same

-10 points if 3 of the numbers are the same

-20 points if 4 of the numbers are the same

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Matlab function

============================================================================================

function mydicegame(d)

%initial arr to 0
for i=1:6
arr(i)=0;
end

for i=1:size(d,2)
arr(d(i))=arr(d(i))+1;
end


%check for consecutive values
f=1;
for i=1:size(d,2)-1
if(d(i+1)-d(i)~=1)
f=0;
break;
end
end

if f==1
score=30
else
%nested if elseif
if(find(arr==5))
score=50
elseif(find(arr==4))
score=20
elseif(find(arr==3))
score=10
elseif(find(arr==2))
score=5
end
  
end %end if else block

end

============================================================================================

Output

MATLAB R2018a 0 - Search Documentation X Login HOME AFFS PUBISH V EW SGG PLOTS Find File: Compare Print 5 New EDITOR insert C

Add a comment
Know the answer?
Add Answer to:
Use matlab please Write a function mydicegame that takes an input vector d of 5 numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT