fork download
  1. //Sam Partovi CS1A Ch. 9, P. 538, #3
  2. /*******************************************************************************
  3. * SORT TEST SCORES (LOWEST SCORE DROPPED)
  4. * ____________________________________________________________
  5. * This program dynamically allocates an array to store user-defined test
  6. * scores. It validates input to ensure no negative scores, sorts the scores
  7. * in ascending order, drops the lowest score, calculates the average, and
  8. * displays the results.
  9. * ____________________________________________________________
  10. * INPUT
  11. * scores: Dynamically allocated array to store test scores
  12. * numScores: Number of test scores to be entered by the user
  13. * OUTPUT
  14. * sortedScores: Sorted list of scores in ascending order
  15. * average: The average of the entered test scores (excluding the lowest score)
  16. *******************************************************************************/
  17. #include <iostream>
  18. #include <iomanip>
  19. using namespace std;
  20.  
  21. //Function prototypes
  22. void sortScores(double* scores, int numScores);
  23. double calculateAverage(const double* scores, int numScores);
  24.  
  25. int main() {
  26. int numScores; //INPUT - Number of test scores
  27.  
  28. //Prompt for the number of test scores
  29. cout << "Enter the number of test scores: ";
  30. cin >> numScores;
  31.  
  32. //Input validation
  33. while (numScores <= 1) {
  34. cout << "Number of test scores must be greater than 1. Try again: ";
  35. cin >> numScores;
  36. }
  37.  
  38. //Dynamically allocate memory for the test scores
  39. double* scores = new double[numScores];
  40.  
  41. //Input test scores
  42. for (int i = 0; i < numScores; i++) {
  43. cout << "Enter score #" << (i + 1) << ": ";
  44. cin >> *(scores + i);
  45.  
  46. //Validate score input
  47. while (*(scores + i) < 0) {
  48. cout << "Scores cannot be negative. Re-enter score #" << (i + 1) << ": ";
  49. cin >> *(scores + i);
  50. }
  51. }
  52.  
  53. //Sort the scores in ascending order
  54. sortScores(scores, numScores);
  55.  
  56. //Calculate the average, excluding the lowest score
  57. double average = calculateAverage(scores + 1, numScores - 1); // Skip the lowest score
  58.  
  59. //Display the sorted scores
  60. cout << "\nSorted Scores (Lowest Score Dropped):" << endl;
  61. for (int i = 1; i < numScores; i++) {
  62. cout << fixed << setprecision(2) << *(scores + i) << endl;
  63. }
  64.  
  65. //Display the average score
  66. cout << "\nAverage Score (Excluding Lowest): " << fixed << setprecision(2) << average << endl;
  67.  
  68. //Free allocated memory
  69. delete[] scores;
  70.  
  71. return 0;
  72. }
  73.  
  74. //*****************************************************************************
  75. //Function definition for sortScores: *
  76. //This function sorts an array of scores in ascending order using pointer *
  77. //notation. *
  78. //*****************************************************************************
  79. void sortScores(double* scores, int numScores) {
  80. for (int i = 0; i < numScores - 1; i++) {
  81. for (int j = i + 1; j < numScores; j++) {
  82. if (*(scores + i) > *(scores + j)) {
  83. double temp = *(scores + i);
  84. *(scores + i) = *(scores + j);
  85. *(scores + j) = temp;
  86. }
  87. }
  88. }
  89. }
  90.  
  91. //*****************************************************************************
  92. //Function definition for calculateAverage: *
  93. //This function calculates the average of the scores in an array using *
  94. //pointer notation. *
  95. //*****************************************************************************
  96. double calculateAverage(const double* scores, int numScores) {
  97. double total = 0.0;
  98. for (int i = 0; i < numScores; i++) {
  99. total += *(scores + i);
  100. }
  101. return total / numScores;
  102. }
  103.  
Success #stdin #stdout 0s 5284KB
stdin
5
95.42
85.28
81.42
88.67
33.49
stdout
Enter the number of test scores: Enter score #1: Enter score #2: Enter score #3: Enter score #4: Enter score #5: 
Sorted Scores (Lowest Score Dropped):
81.42
85.28
88.67
95.42

Average Score (Excluding Lowest): 87.70