fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. using namespace std;
  5.  
  6. int randomInt() { return rand(); }
  7.  
  8. struct person
  9. {
  10. int athletic;
  11. int smarts;
  12. int spirit;
  13. int contestVal;
  14. };
  15.  
  16. int AthleticContest(vector<person> &subjects)
  17. {
  18. cout << "Athletic Contest!!!" << endl << endl;
  19. for (int hh = 0; hh < subjects.size(); hh++)
  20. {
  21. int result = subjects[hh].athletic;
  22. subjects[hh].contestVal = result;
  23. cout << "Contestant # " << (hh+1) << ") " << subjects[hh].contestVal << endl;
  24. }
  25. int winner;
  26. int tempWin = -1;
  27. for (int hh = 0; hh < subjects.size(); hh++)
  28. {
  29. if (subjects[hh].contestVal > tempWin)
  30. {
  31. tempWin = subjects[hh].contestVal;
  32. winner = hh;
  33. }
  34. else if (subjects[hh].contestVal == tempWin)
  35. {
  36. if (randomInt() > 4)
  37. winner = hh;
  38. }
  39. }
  40. cout << "Winner is Contestant # " << (winner+1) << endl;
  41.  
  42. return winner;
  43. }
  44.  
  45. int main()
  46. {
  47. vector<person> subject(2); // create only 2 items
  48.  
  49. subject[0].athletic = 5;
  50. subject[0].smarts = 3;
  51. subject[0].spirit = 1;
  52. subject[1].athletic = 1;
  53. subject[1].smarts = 3;
  54. subject[0].spirit = 5;
  55. subject[1].athletic = 3;
  56. subject[1].smarts = 5;
  57. subject[0].spirit = 1;
  58.  
  59. AthleticContest(subject);
  60.  
  61. person hbolt = {4, 2, 1, 0};
  62. subject.emplace_back (hbolt);
  63. AthleticContest(subject);
  64.  
  65. }
  66.  
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
Athletic Contest!!!

Contestant # 1) 5
Contestant # 2) 3
Winner is Contestant # 1
Athletic Contest!!!

Contestant # 1) 5
Contestant # 2) 3
Contestant # 3) 4
Winner is Contestant # 1