MATLAB Answers

Printing data results with Fprintf

1 view (last 30 days)
azaandria
azaandria on 23 Mar 2021
Commented: azaandria on 23 Mar 2021
Hello,
I am trying to display the event names that each gymnast won, not the event #. Could someone please explain how to do this? My output is down at the very bottom.THank you!
events={'Pommel Horse ';
'Vault ';
'Floor ';
'Rings ';
'High Bar ';
'Parallel Bars'};
g1=input('Enter scores for Gymnast 1:')
g2=input('Enter scores for Gymnast 2:')
table=[events; g1; g2];
disp(' ');
disp('Number of events won by Gymnast 1:')
g1won=find(g1>g2);
num_of_wins=numel(g1won);
disp(num_of_wins);
disp('Event #s won by Gymnast 1:');
disp(g1won);
disp(' ');
disp('Number of events won by Gymnast 2:');
g2won=find(g2>g1);
numb_of_wins=numel(g2won);
disp(numb_of_wins);
disp('Event #s won by Gymnast 2:');
disp(g2won);
disp('Gymnast 1 Average Score:');
A1=mean(g1);
disp(A1);
disp('Gymnast 2 Average Score:');
A2=mean(g2);
disp(A2);
disp('Overall winner:');
if(A1==A2)
disp('Tie breaker')
elseif(A1>A2)
disp('Gymnast 1')
else
disp('Gymnast 2')
end
%
%
%Output
%Enter scores for Gymnast 1:[9.921 9.923 9.729 9.908 9.534 9.973]
%Enter scores for Gymnast 2:[9.600 9.925 9.831 9.987 9.354 9.879]
%
%Number of events won by Gymnast 1:
% 3
%
%Event #s won by Gymnast 1:
% 1 5 6
%
%
%Number of events won by Gymnast 2:
% 3
%
%Event #s won by Gymnast 2:
% 2 3 4
%
%Gymnast 1 Average Score:
% 9.8313
%
%Gymnast 2 Average Score:
% 9.7627
%
%Overall winner:
%Gymnast 1

Accepted Answer

Geoff Hayes
Geoff Hayes on 23 Mar 2021
azaandria - try replacing
disp('Event #s won by Gymnast 2:');
disp(g2won);
with
disp('Event #s won by Gymnast 2:');
eventsWonByGymnast2 = events(g2won);
fprintf('%s\n',eventsWonByGymnast2{:})
to get the event names rather than the event indices.
  1 Comment
azaandria
azaandria on 23 Mar 2021
Thank you so much. It worked perfectly!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!