fork download
  1. /*Timothy Price CS1A Ch6 challenge 10
  2. ********************************************************************************
  3. * Calculate Average
  4. *
  5. * This program receives a number of scores, determines the lowest, removes it
  6. * from the set, and calculates the average of the remaining scores.
  7. *
  8. * Inputs:
  9. * 5 test scores
  10. *
  11. * Outputs:
  12. * Lowest score
  13. * Average of remaining scores
  14. *******************************************************************************/
  15.  
  16. #include <iostream>
  17. #include <iomanip>
  18.  
  19. using namespace std;
  20.  
  21. // Function prototypes
  22. void getScore(int &score);
  23. void calcAverage(int s1, int s2, int s3, int s4, int s5);
  24. int findLowest(int s1, int s2, int s3, int s4, int s5);
  25.  
  26. int main() {
  27. int score1, score2, score3, score4, score5;
  28.  
  29. //Gather inputs
  30. cout << "Please enter five test scores." << endl;;
  31. getScore(score1);
  32. getScore(score2);
  33. getScore(score3);
  34. getScore(score4);
  35. getScore(score5);
  36.  
  37. //Call first fucntion
  38. calcAverage(score1, score2, score3, score4, score5);
  39.  
  40. return 0;
  41. }
  42.  
  43. //Receives test score, stores it, and validates it
  44. void getScore(int &score) {
  45. cout << "Enter a test score: ";
  46. cin >> score;
  47. cout << score << "%" << endl;
  48. if (score < 0 || score > 100) {
  49. cout << "Invalid score. Test scores must be between 0 and 100." << endl;
  50. cout << "Enter a test score: ";
  51. cin >> score;
  52. }
  53. }
  54.  
  55. //Finds lowest test score
  56. int findLowest(int s1, int s2, int s3, int s4, int s5) {
  57. int lowest = s1;
  58.  
  59. if (s2 < lowest) lowest = s2;
  60. if (s3 < lowest) lowest = s3;
  61. if (s4 < lowest) lowest = s4;
  62. if (s5 < lowest) lowest = s5;
  63.  
  64. return lowest;
  65. }
  66.  
  67. // Calculates the average of the remaining scores
  68. void calcAverage(int s1, int s2, int s3, int s4, int s5) {
  69. int lowest = findLowest(s1, s2, s3, s4, s5);
  70. double sum = (s1 + s2 + s3 + s4 + s5) - lowest;
  71. double average = sum / 4.0;
  72.  
  73. cout << fixed << setprecision(2) << endl;
  74. cout << "Lowest score dropped: " << lowest << endl;
  75. cout << "Average of the remaining four scores: " << average << endl;
  76. }
Success #stdin #stdout 0s 5320KB
stdin
100
98
90
70
67

stdout
Please enter five test scores.
Enter a test score: 100%
Enter a test score: 98%
Enter a test score: 90%
Enter a test score: 70%
Enter a test score: 67%

Lowest score dropped: 67
Average of the remaining four scores: 89.50