fork download
  1. //Amari Mosley CSC5 Chapter 9, P. 538 #3
  2.  
  3. /********************************************************************
  4.  *
  5.  * DROP LOWEST SCORE
  6.  * _________________________________________________________________
  7.  *
  8.  * This program dynamically allocates an array to hold a user-defined
  9.  * number of test scores. It accepts the scores, sorts them in
  10.  * ascending order, drops the lowest score, calculates the average
  11.  * of the remaining scores, and displays the sorted scores and
  12.  * average.
  13.  *
  14.  * _________________________________________________________________
  15.  *
  16.  * INPUT
  17.  * numScores : Number of test scores to enter
  18.  * scores : Test scores entered by the user
  19.  * OUTPUT
  20.  * sortedScores : Test scores sorted in ascending order
  21.  * average : Average excluding the lowest score
  22.  *
  23.  ********************************************************************/
  24. #include <iostream>
  25. #include <iomanip>
  26. using namespace std;
  27.  
  28. // Function Prototypes
  29. void sortScores(double*, int);
  30. double calculateAverage(double*, int);
  31.  
  32. int main()
  33. {
  34. // INPUT
  35. int numScores;
  36.  
  37. // Get the number of test scores
  38. cout << "How many test scores would you like to enter? ";
  39. cin >> numScores;
  40.  
  41. // Input Validation
  42. while (numScores <= 1)
  43. {
  44. cout << "Please enter at least 2 test scores: ";
  45. cin >> numScores;
  46. }
  47.  
  48. // Dynamically allocate the array
  49. double* scores = new double[numScores];
  50.  
  51. // Get test scores from the user
  52. for (int count = 0; count < numScores; count++)
  53. {
  54. cout << "Enter test score #" << (count + 1) << ": ";
  55. cin >> *(scores + count);
  56.  
  57. // Input Validation
  58. while (*(scores + count) < 0)
  59. {
  60. cout << "Negative scores are not accepted. Enter again: ";
  61. cin >> *(scores + count);
  62. }
  63. }
  64.  
  65. // Sort the test scores
  66. sortScores(scores, numScores);
  67.  
  68. // Calculate the average excluding the lowest score
  69. double average = calculateAverage(scores, numScores);
  70.  
  71. // Output Sorted Test Scores
  72. cout << endl << "Sorted Test Scores" << endl;
  73. cout << "________________________" << endl;
  74.  
  75. for (int count = 0; count < numScores; count++)
  76. {
  77. cout << fixed << setprecision(2);
  78. cout << *(scores + count);
  79.  
  80. if (count == 0)
  81. {
  82. cout << " (Dropped)";
  83. }
  84.  
  85. cout << endl;
  86. }
  87.  
  88. // Output Average
  89. cout << endl << "Average Test Score: ";
  90. cout << fixed << setprecision(2) << average << endl;
  91.  
  92. // Free dynamically allocated memory
  93. delete[] scores;
  94.  
  95. return 0;
  96. }
  97.  
  98. // Function to sort scores in ascending order
  99. void sortScores(double* scores, int numScores)
  100. {
  101. double temp;
  102.  
  103. for (int count = 0; count < numScores - 1; count++)
  104. {
  105. for (int index = count + 1; index < numScores; index++)
  106. {
  107. if (*(scores + count) > *(scores + index))
  108. {
  109. temp = *(scores + count);
  110. *(scores + count) = *(scores + index);
  111. *(scores + index) = temp;
  112. }
  113. }
  114. }
  115. }
  116.  
  117. // Function to calculate average excluding the lowest score
  118. double calculateAverage(double* scores, int numScores)
  119. {
  120. double total = 0;
  121.  
  122. for (int count = 1; count < numScores; count++)
  123. {
  124. total += *(scores + count);
  125. }
  126.  
  127. return total / (numScores - 1);
  128. }
Success #stdin #stdout 0s 5348KB
stdin
5
85
92
78
100
65
stdout
How many test scores would you like to enter? Enter test score #1: Enter test score #2: Enter test score #3: Enter test score #4: Enter test score #5: 
Sorted Test Scores
________________________
65.00 (Dropped)
78.00
85.00
92.00
100.00

Average Test Score: 88.75