% Function name: KOM % % Inputs (1): - (struct) a 1xN structure array % % Output
ID: 3538733 • Letter: #
Question
% Function name: KOM
%
% Inputs (1): - (struct) a 1xN structure array
%
% Outputs (1): - (char) an output string
%
% Function Description:
% Write a function that takes in data from a downhill bicycle race in the form of a structure array.
% The fieldnames of the structure array are: 'name', 'time', 'gender', and 'maxspeed'.
% The function will output a string describing who the King (or Queen) of the mountain (the racer with the shortest time) is.
% Depending on gender denoted by an 'm' or 'f' in the 'gender' fieldname, you will either output
%
% 'The King is <name> with a max speed of <maxspeed> mph!' if the winner is male
% or
% 'The Queen is <name> with a max speed of <maxspeed> mph!' if the winner is female
%
% Notes:
% - There will be no ties
%
% Test Case:
%
% test1 = struct('name', {'John' 'Sally' 'Tom'}, 'time', {20 23 22.5}, 'gender', {'m' 'f' 'm'}, 'maxspeed', {20 21 21.5});
%
% out = KOM(test1)
% out => 'The King is John with a max speed of 20 mph!'
Explanation / Answer
function output = KOM(race)
k=max(size(race));
pos=1;
for j=2:k
if(race(j).time<race(pos).time)
pos=j;
end
end
if(race(pos).gender=='m')
output = sprintf('The King is %s with a max speed of %G mph! ',race(pos).name,race(pos).maxspeed);
else
output = sprintf('The Queen is %s with a max speed of %G mph! ',race(pos).name,race(pos).maxspeed);
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.