SHARE
TWEET

OOP 1 Exercise 4

a guest Feb 23rd, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3. const int NUMBER_OF_MARATHONS = 12;
  4. const int MAX_NUMBER_PARTICIPANTS = 1000;
  5.  
  6.  
  7. struct Runner {
  8.     char name[30];
  9.     char surname[30];
  10.     double times[NUMBER_OF_MARATHONS];
  11. };
  12.  
  13. Runner getNew()
  14. {
  15.     Runner newRunner;
  16.     cout << "Let's add a new runner!" << endl;
  17.     cout << "Give me a name:" << endl;
  18.     cin >> newRunner.name;
  19.     cout << "Give me a surname:" << endl;
  20.     cin >> newRunner.surname;
  21.     for (int i = 0; i < NUMBER_OF_MARATHONS; i++)
  22.     {
  23.         cout << "Give me his " << i + 1 << " marathon time:" << endl;
  24.         cin >> newRunner.times[i];
  25.     }
  26.     return newRunner;
  27. }
  28.  
  29. void slowestRunner(Runner runnersList[MAX_NUMBER_PARTICIPANTS], int size)
  30. {
  31.     int slowestRunnerIndex = 0;
  32.     double slowestPace = 100000;
  33.     for (int i = 0; i < size; i++)
  34.     {
  35.         double averagePace = 0;
  36.         cout << runnersList[i].name << endl;
  37.         for (int j = 0; j < NUMBER_OF_MARATHONS; j++)
  38.         {
  39.             cout << runnersList[i].times[j] << endl;
  40.             averagePace += runnersList[i].times[j];
  41.         }
  42.         averagePace /= 12;
  43.         if (averagePace < slowestPace)
  44.         {
  45.             slowestPace = averagePace;
  46.             slowestRunnerIndex = i;
  47.         }
  48.     }
  49.     cout << "Slowest runner is" << endl;
  50.     cout << runnersList[slowestRunnerIndex].name << endl;
  51.  
  52. }
  53. int beatNTime(Runner a, Runner b)
  54. {
  55.     int wonTheRace = 0;
  56.     for (int i = 0; i < NUMBER_OF_MARATHONS; i++)
  57.     {
  58.         if (a.times[i] > b.times[i])
  59.         {
  60.             wonTheRace++;
  61.         }
  62.     }
  63.     return wonTheRace;
  64. }
  65.  
  66.  
  67.  
  68. int main()
  69. {
  70.     int n;
  71.     cin >> n;
  72.     Runner runnersList[MAX_NUMBER_PARTICIPANTS];
  73.     for (int i = 0; i < n; i++)
  74.     {
  75.         runnersList[i] = getNew();
  76.     }
  77.     slowestRunner(runnersList,n);
  78.     cout << beatNTime(runnersList[0], runnersList[1]) << endl;
  79.  
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top