Advertisement
JKattackk

Assignment4A

Mar 6th, 2020
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.36 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Course: ENCMP 100
  3. % Assignment: 4A
  4. % Name:
  5. % CCID:
  6. % U of A ID:
  7. %
  8. % Acknowledgements:
  9. % I refered to the MatLab documentation while writing.
  10. %
  11. % Description:
  12. % This program determines the number of bronze, silver, and gold medals won
  13. % by each country from the provided data set.  This program also determines
  14. % the total number of medals won by each country and can display the
  15. % results for each country, as well as the countries with the most gold
  16. % medals, most total medals, or over 20 medals.
  17. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  18. clc;
  19. clear;
  20. 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.    
  21. medals = zeros(size(countries, 1), 4);  %setting up an empty grid for our values per country
  22.  
  23. for i=1:size(countries, 1)
  24.     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)
  25.     medals(i, 4) = sum(medals(i, 1:3));                                      %Fourth column is the sum of all medals won, sum of columns 1:3
  26. end
  27.  
  28. printResults(medals, countries);  %Prints a grid with the medals and total medals for every country (except XXX at the end)
  29.  
  30. for i=1:3
  31.     printBest(medals, countries, i);  %Prints the best countries for 3 different criteria, most medals, most gold medals, and over 20 medals won.
  32. end
  33.  
  34.  
  35. function [count] = countMedals(gold, silver, bronze, country)  % returns the gold, silver, and bronze medals won by a country.
  36. count = zeros(3, 1);  %empty grid for results
  37. medals = gold; medals(:, :, 2) = silver; medals(:, :, 3) = bronze; %one array contains the arrays gold, silver, and bronze.
  38. for i= 1:3
  39.     for j = 1:size(medals(:, 1, i))
  40.         if medals(j, :, i) == country  %Add up all wins for the current array.
  41.             count(i) = count(i) +1;
  42.         end
  43.     end
  44. end        
  45. end
  46.  
  47. function [] = printResults(medals, countries)
  48. fprintf('Country Gold Silver Bronze Total\n');
  49. for i=1:size(countries(:, 1))-1
  50.     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));
  51. end
  52. end  %Prints a grid with the medals and total medals for every country (except XXX at the end)
  53.  
  54. function [] = printBest(medals, countries, criteria)
  55. switch criteria  %Prints different results for different criteria
  56.     case 1
  57.         fprintf('Countries with the most medals:'); %Most medals
  58.         for i=1:size(medals(:, 4)) -1
  59.             if medals(i, 4) == max(medals(:, 4))  
  60.                 fprintf('%4s', countries(i, :));
  61.             end
  62.         end
  63.         fprintf('\n');
  64.        
  65.     case 2
  66.         fprintf('Countries with the most gold medals:');  %Most gold medals
  67.         for i=1:size(medals(:, 1)) -1
  68.             if medals(i, 1) == max(medals(:, 1))
  69.                 fprintf('%4s', countries(i, :));
  70.             end
  71.         end
  72.         fprintf('\n');
  73.        
  74.     case 3
  75.         fprintf('Countries at least 20 medals:');  %More than 20 medals
  76.         for i=1:size(medals(:, 1))-1
  77.             if medals(i, 4) >= 20
  78.                 fprintf('%4s', countries(i, :));
  79.             end
  80.         end
  81.         fprintf('\n');
  82.     otherwise
  83.         disp('error');
  84. end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement