As a part-time blog writer for a major sports network You are tasked with provid
ID: 3677402 • Letter: A
Question
As a part-time blog writer for a major sports network You are tasked with providing a readerfriendly summary of the most recent Winter Olympics.
Goal: To use two subfunctions, the first will compute the number of gold, silver and bronze medals won per country. The second will print a table with medal standings per country listed alphebetically which should look like:
cont...
File Given:
The olympics.mat file input data gives these variables:
countries = AUS; AUT; BLR; CAN... (27 countries listed alphebetically)
- lists countries that scored medals. The countries are coded using three letter abbreviations, e.g., CAN stands for Canada.
bronze = SWE; NOR; USA; USA... (87 bronze winners listed randomly)
silver = CRO; NOR; USA: NOR... (87 silver winners listed randomly)
gold = ITA; SUI; NOR; SUI; USA... (87 gold winners listed randomly)
-gold, silver and bronze, list the countries that received gold, silver, and bronze medals, respectively. Note that the gold, silver and bronze variables list the data in the same order. This means that the i th row in these variables gives the countries that received the corresponding three types of medals for the same sport. Your objective is to convert the results per sport (from the olympics.mat
General Code Outline:
function [] = assign4_()
%load olympics.mat file
load('olympics.mat');
% compute medals for each country using a sub-function that is called using a loop over all countries
% display medal counts for all countries using a sub-function
%end
Thankyou for helping.
Country Gold Silver Bronze Total AUS 2 1 0 3 AUT 4 6 6 16Explanation / Answer
function [] = Assign4_1255986()
%clear Workspace and Command Window and load olympics2010
clear; clc;
load('olympics2010.mat');
%function calls
[gold_medals, silver_medals, bronze_medals, total_medals] = medal_count(gold, silver, bronze, countries);
disp_medal_count(gold_medals, silver_medals, bronze_medals, total_medals, countries);
disp_medal_stats(gold_medals, total_medals, countries, 1);
disp_medal_stats(gold_medals, total_medals, countries, 2);
disp_medal_stats(gold_medals, total_medals, countries, 3);
end
%-------------------------------------------------------------------------%
%compute medals for each country using a sub-function that is
%called using a loop over all countries
function [gold_medals, silver_medals, bronze_medals, total_medals] = medal_count(gold, silver, bronze, countries)
%calculate the total number of gold, silver and bronze medals for
%each and every country
%initialize and pre-allocate the variables
total_countries = length(countries);
gold_medals = zeros(total_countries, 1);
silver_medals = zeros(total_countries, 1);
bronze_medals = zeros(total_countries, 1);
total_medals = zeros(total_countries, 1);
for i = 1:total_countries
country = countries(i, :);
gold_count = 0;
silver_count = 0;
bronze_count = 0;
for j = 1: length(gold)
if country == gold(j, :)
gold_count = gold_count + 1;
end
if country == silver(j, :)
silver_count = silver_count + 1;
end
if country == bronze(j, :)
bronze_count = bronze_count + 1;
end
gold_medals(i) = gold_count;
silver_medals(i) = silver_count;
bronze_medals(i) = bronze_count;
total_medals(i) = gold_count+silver_count+bronze_count;
end
end
end
%display medal counts for all countries using a sub-function
function [] = disp_medal_count(gold_medals, silver_medals, bronze_medals, total_medals, countries)
%display the gold, silver and bronze medal count for each country
fprintf('Country Gold Silver Bronze Total ');
for i = 1:length(countries)
if strcmp(countries(i, :), 'XXX') == 0
fprintf('%7s %4d %6d %6d %5d ', countries(i, :), gold_medals(i), silver_medals(i), bronze_medals(i), total_medals(i));
end
end
end
%display countries with most medals, most gold medals, and >=20 medals
%using a sub-function, the same sub-function should be used 3 times
function [] = disp_medal_stats(gold_medals, total_medals, countries, option)
%display stats for countries with most medals, most gold medals, and >= 20 medals
fprintf(' ');
switch option
case 1
most_medals = find(total_medals == max(total_medals));
fprintf('Countries with the most medals: %s', countries(most_medals, :));
case 2
most_gold_medals = find(gold_medals == max(gold_medals));
fprintf('Countries with the most gold medals: %s', countries(most_gold_medals, :));
case 3
atleast_20_medals = find(total_medals >= 20);
fprintf('Countries with at least 20 medals: ');
for i = 1:length(atleast_20_medals)
fprintf('%s ', countries(atleast_20_medals(i), :));
end
fprintf(' ');
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.