How would I turn this code into a callable function that requires no inputs in M
ID: 3837615 • Letter: H
Question
How would I turn this code into a callable function that requires no inputs in MatLab?
%% Stuck in the Mud
rng('shuffle')
n = 5; % define variables
f = n;
roll = 1;
mud = []; % open array so it can change with each dice roll
score = 0;
while n > 0
fprintf(' ROLL %d ',roll);
roll = roll+1;
r = randi([1 6],1,n);
count2 = sum(r == 2);
count5 = sum(r == 5);
if (count2 > 0) || (count5 >0)
mud = [ mud , repmat(2,1,count2) repmat(5,1,count5)] ;
n = n - count2-count5;
else
score = score+ sum(r);
end
fprintf('Rolling :');disp(r);
fprintf('Stuck in the mud :');disp(mud);
fprintf('Game score: %d ',score);
fprintf('Number of dice stuck: %d ',size(mud,2));
end
Explanation / Answer
You already did it. Their no need to do anything else.
You can simply call your function using its name
Example
Name of your file is a.m
Let take another file b.m
Use call it simply as
a
Note
Script a.m and b.m must in same directory
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.