fork download
  1. // Marvyn Jesse De La Torre CS1A Chapter 9 #3 pg. 538
  2. /*****************************************************************
  3.  * REMOVE LOWEST SCORE
  4.  * _________________________________________________________________________
  5.  * This program creates an array of test scores, sorts them from lowest to
  6.  * highest, and calculates the average without the lowest score. Pointer
  7.  * notation is used to access the array elements.
  8.  *
  9.  * INPUT
  10.  * scoreCount : Number of scores (at least 2)
  11.  * scores : Nonnegative test scores
  12.  *
  13.  * OUTPUT
  14.  * Sorted scores
  15.  * Average with the lowest score removed
  16.  *****************************************************************/
  17. #include <iomanip>
  18. #include <iostream>
  19. using namespace std;
  20.  
  21. int getScoreCount()
  22. {
  23. int count;
  24.  
  25. do
  26. {
  27. cout << "How many test scores will be entered (at least 2)? ";
  28. cin >> count;
  29.  
  30. if (count < 2)
  31. cout << "At least two scores are required.\n";
  32. } while (count < 2);
  33.  
  34. return count;
  35. }
  36.  
  37. int getScore(int scoreNumber)
  38. {
  39. int score;
  40.  
  41. do
  42. {
  43. cout << "Enter score " << scoreNumber << ": ";
  44. cin >> score;
  45.  
  46. if (score < 0)
  47. cout << "Test scores cannot be negative.\n";
  48. } while (score < 0);
  49.  
  50. return score;
  51. }
  52.  
  53. void sortScores(int* scores, int size)
  54. {
  55. for (int startScan = 0; startScan < size - 1; ++startScan)
  56. {
  57. int minIndex = startScan;
  58.  
  59. for (int index = startScan + 1; index < size; ++index)
  60. {
  61. if (*(scores + index) < *(scores + minIndex))
  62. minIndex = index;
  63. }
  64.  
  65. int temporary = *(scores + startScan);
  66. *(scores + startScan) = *(scores + minIndex);
  67. *(scores + minIndex) = temporary;
  68. }
  69. }
  70.  
  71. double averageWithoutLowest(const int* scores, int size)
  72. {
  73. int total = 0;
  74.  
  75. for (int index = 1; index < size; ++index)
  76. total += *(scores + index);
  77.  
  78. return static_cast<double>(total) / (size - 1);
  79. }
  80.  
  81. int main()
  82. {
  83. const int scoreCount = getScoreCount();
  84. int* scores = new int[scoreCount];
  85.  
  86. for (int index = 0; index < scoreCount; ++index)
  87. *(scores + index) = getScore(index + 1);
  88.  
  89. sortScores(scores, scoreCount);
  90.  
  91. cout << "\nScores in ascending order:\n";
  92.  
  93. for (int index = 0; index < scoreCount; ++index)
  94. cout << *(scores + index) << " ";
  95.  
  96. cout << fixed << setprecision(2);
  97. cout << "\nLowest score dropped: " << *scores << endl;
  98. cout << "Average of the remaining scores: "
  99. << averageWithoutLowest(scores, scoreCount) << endl;
  100.  
  101. delete[] scores;
  102. scores = nullptr;
  103.  
  104. return 0;
  105. }
Success #stdin #stdout 0s 544KB
stdin
4
88
95
75
100
stdout
How many test scores will be entered (at least 2)? Enter score 1: Enter score 2: Enter score 3: Enter score 4: 
Scores in ascending order:
75 88 95 100 
Lowest score dropped: 75
Average of the remaining scores: 94.33