Question
Hello
can anybody tell me why this code is not working?
I copied it from this link : http://www.chegg.com/homework-help/questions-and-answers/playing-game-roll-die-6-times-roll-5-6-four-times-win-4-dollars-two-three-times-win-2-doll-q20159331
a do la È 2 C G Q Search Documentation Log In 1 MATLAB R2017a - academic use HOME PLOTS APPS L 4 SY 4 h Find Files 1 H New Variable R Analyze Code # Open Variable - Run and Time New New Open FB Compare Import Save Simulink Script - Data Workspace Clear Workspace Clear Commands - FILE VARIABLE CODE SIMULINK 1+ /> Users • YoussefTanzakkine ? Documents ? MATLAB ? mynewdir Current Folder O Command Window E Name A >> function prize = DiceGame ( rolls) Layout O Preferences S Set Path illi Parallel - ENVIRONMENT Add-Ons Help 3 Community 9 Request Support 2 Learn MATLAB RESOURCES « 3o winningRolls = 0; for i = 1 : length(rolls) if rolls(i) == 5 || rolls(i) == 6 winningRolls = winningRolls + 1; end Details end if winningRolls >= 4 Workspace Name A | winningRolls Value 0 prize = 4; elseif winningRolls > 1 prize = 2; else prize = 0; end function prize = DiceGame ( rolls) Error: Function definitions are not permitted in this context. fx >>|
Explanation / Answer
% function is not closed using end statement
% include "end" the end of code and then the code should work.
% matlab code
function prize = DiceGame(rolls)
winningRolls = 0;
for i = 1 : length(rolls)
if rolls(i) == 5 || rolls(i) == 6
winningRolls = winningRolls + 1;
end
end
if winningRolls >= 4
prize = 4;
elseif winningRolls > 1
prize = 2;
else
prize = 0;
end
% function is not closed using end in the code provided in screenshot
end