Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Course: ENCMP 100
- % Assignment: 4A
- % Name:
- % CCID:
- % U of A ID:
- %
- % Acknowledgements:
- % I refered to the MatLab documentation while writing.
- %
- % Description:
- % This program determines the number of bronze, silver, and gold medals won
- % by each country from the provided data set. This program also determines
- % the total number of medals won by each country and can display the
- % results for each country, as well as the countries with the most gold
- % medals, most total medals, or over 20 medals.
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- clc;
- clear;
- load olympics.mat; %olympics.mat loads as four arrays, an array of 3 character country abbreviations in alphabetical order, and the list of medal winners for gold, silver, and bronze medals.
- medals = zeros(size(countries, 1), 4); %setting up an empty grid for our values per country
- for i=1:size(countries, 1)
- medals(i, 1:3) = countMedals(gold, silver, bronze, countries(i, :)); %Count medals will return a 1x3 array with the amount of each medal won (gold, bronze, silver)
- medals(i, 4) = sum(medals(i, 1:3)); %Fourth column is the sum of all medals won, sum of columns 1:3
- end
- printResults(medals, countries); %Prints a grid with the medals and total medals for every country (except XXX at the end)
- for i=1:3
- printBest(medals, countries, i); %Prints the best countries for 3 different criteria, most medals, most gold medals, and over 20 medals won.
- end
- function [count] = countMedals(gold, silver, bronze, country) % returns the gold, silver, and bronze medals won by a country.
- count = zeros(3, 1); %empty grid for results
- medals = gold; medals(:, :, 2) = silver; medals(:, :, 3) = bronze; %one array contains the arrays gold, silver, and bronze.
- for i= 1:3
- for j = 1:size(medals(:, 1, i))
- if medals(j, :, i) == country %Add up all wins for the current array.
- count(i) = count(i) +1;
- end
- end
- end
- end
- function [] = printResults(medals, countries)
- fprintf('Country Gold Silver Bronze Total\n');
- for i=1:size(countries(:, 1))-1
- fprintf('%7s%5.0f%7.0f%7.0f%6.0f\n', countries(i, :), medals(i, 1), medals(i, 2), medals(i, 3), medals(i, 4));
- end
- end %Prints a grid with the medals and total medals for every country (except XXX at the end)
- function [] = printBest(medals, countries, criteria)
- switch criteria %Prints different results for different criteria
- case 1
- fprintf('Countries with the most medals:'); %Most medals
- for i=1:size(medals(:, 4)) -1
- if medals(i, 4) == max(medals(:, 4))
- fprintf('%4s', countries(i, :));
- end
- end
- fprintf('\n');
- case 2
- fprintf('Countries with the most gold medals:'); %Most gold medals
- for i=1:size(medals(:, 1)) -1
- if medals(i, 1) == max(medals(:, 1))
- fprintf('%4s', countries(i, :));
- end
- end
- fprintf('\n');
- case 3
- fprintf('Countries at least 20 medals:'); %More than 20 medals
- for i=1:size(medals(:, 1))-1
- if medals(i, 4) >= 20
- fprintf('%4s', countries(i, :));
- end
- end
- fprintf('\n');
- otherwise
- disp('error');
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement