In: Computer Science
Function Name: medievalFootball
Inputs:
1. (struct ) A 1xN structure representing the "field" and the players on it
Outputs:
1. (char ) A short sentence describing what happened.
Topics: ( structures ), (conditionals) , (iteration ), (cell arrays )
Background:
As you are backpacking through Europe during your "life changing study abroad experience", you stop in a small town for a bite to eat and hear about an interesting local tradition similar to a game of football. Only it's not American football, it's Medieval Football. They're exactly the same, except Medieval Football has no limit on team size, no set playing field, and no rules on aggression. Instead of joining the locals, you decide to sit this one out and model it in MATLAB instead.
Function Description:
Given a 1xN structure array representing the "game" and only containing the field "player", determine which goal area (the first and last structures) has a player in it. Then count how many yards (absolute value of the amount of structures moved) that player can move across the field without running into another player who politely forces them to stop. Output what happened in a string with the format:
' madeth it <# of yards they ran> rod(s) ere stove in 's kneecap!'
Example: strc →
sout = medievalFootball(strc) sout → 'Jim madeth it 3 rod(s) ere Bob stove in Jim's kneecap!'
Notes:
Please use matlab to answer question and avoid using the BREAK Function!! This is all the information I am given for the problem!!!!
% medievalFootball.m
function sout = medievalFootball(strc)
% Matlab function that takes as input 1xN structure representing
the
% "field" and the players on it, returns a short sentence
describing
% what happened.
% initialize blockerIndex to 0
blockerIndex = 0;
% determine the runnerIndex based on first and last structure
player
if(~isempty(strc(1).player))
runnerIndex = 1;
else
runnerIndex = length(strc);
end
% based on runnerIndex determine the blockerIndex
if(runnerIndex == 1)
% loop from index 2 to second last index, set blockerIndex to
first
% non-empty player
i = 2;
while(i < length(strc) && blockerIndex == 0)
if(~isempty(strc(i).player)) % blocker found, set the blockerIndex
and exit the loop
blockerIndex = i;
end
i = i + 1;
end
else
% loop from second last index to index 2, set blockerIndex to
first
% non-empty player
i = length(strc)-1;
while(i > 1 && blockerIndex == 0)
if(~isempty(strc(i).player)) % blocker found, set the blockerIndex
and exit the loop
blockerIndex = i;
end
i = i - 1;
end
end
% create the resultant sentence
sout = sprintf('%s madeth it %d rod(s) ere %s in %s%ss
kneecap!',strc(runnerIndex).player, abs(runnerIndex-blockerIndex),
strc(blockerIndex).player, strc(runnerIndex).player,"'") ;
end
%end of medievalFootball.m
Output: