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 highest,
  6.  * and calculates the average without the lowest score. Pointer notation is used to
  7.  * 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.  
  18. #include <iomanip>
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. int getScoreCount()
  23. {
  24. int count;
  25.  
  26. do
  27. {
  28. cout << "How many test scores will be entered (at least 2)? ";
  29. cin >> count;
  30.  
  31. if (count < 2)
  32. cout << "At least two scores are required.\n";
  33. } while (count < 2);
  34.  
  35. return count;
  36. }
  37.  
  38. int getScore(int scoreNumber)
  39. {
  40. int score;
  41.  
  42. do
  43. {
  44. cout << "Enter score " << scoreNumber << ": ";
  45. cin >> score;
  46.  
  47. if (score < 0)
  48. cout << "Test scores cannot be negative.\n";
  49. } while (score < 0);
  50.  
  51. return score;
  52. }
  53.  
  54. void sortScores(int* scores, int size)
  55. {
  56. for (int startScan = 0; startScan < size - 1; ++startScan)
  57. {
  58. int minIndex = startScan;
  59.  
  60. for (int index = startScan + 1; index < size; ++index)
  61. {
  62. if (*(scores + index) < *(scores + minIndex))
  63. minIndex = index;
  64. }
  65.  
  66. int temporary = *(scores + startScan);
  67. *(scores + startScan) = *(scores + minIndex);
  68. *(scores + minIndex) = temporary;
  69. }
  70. }
  71.  
  72. double averageWithoutLowest(const int* scores, int size)
  73. {
  74. int total = 0;
  75.  
  76. for (int index = 1; index < size; ++index)
  77. total += *(scores + index);
  78.  
  79. return static_cast<double>(total) / (size - 1);
  80. }
  81.  
  82. int main()
  83. {
  84. const int scoreCount = getScoreCount();
  85. int* scores = new int[scoreCount];
  86.  
  87. for (int index = 0; index < scoreCount; ++index)
  88. *(scores + index) = getScore(index + 1);
  89.  
  90. sortScores(scores, scoreCount);
  91.  
  92. cout << "\nScores in ascending order:\n";
  93.  
  94. for (int index = 0; index < scoreCount; ++index)
  95. cout << *(scores + index) << " ";
  96.  
  97. cout << fixed << setprecision(2);
  98. cout << "\nLowest score dropped: " << *scores << endl;
  99. cout << "Average of the remaining scores: "
  100. << averageWithoutLowest(scores, scoreCount) << endl;
  101.  
  102. delete[] scores;
  103. scores = nullptr;
  104.  
  105. return 0;
  106. }
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