fork download
  1. //Cameron Pham CS1A Chapter 9, p. 538, #3
  2. //
  3. /******************************************************************************
  4. * DROP LOWEST SCORE
  5. * ______________________________________________________________________________
  6. * This program dynamically allocates an array to store a user-defined number of
  7. * test scores. It accepts an integer value for every test score. The program
  8. * then sorts the scores, calculates their average, identifies the lowest score,
  9. * and displays the sorted scores along with the average.
  10. *
  11. * INPUT
  12. * Number of scores // Total number of test scores
  13. * Scores // Every individual test score
  14. *
  15. * OUTPUT
  16. * Average // Average of test scores
  17. * Lowest // Lowest test score
  18. *******************************************************************************/
  19. #include <iostream>
  20. #include <iomanip>
  21. using namespace std;
  22.  
  23. // Function prototypes
  24. void sortScores(int* scores, int size);
  25. double calculateAverage(int* scores, int size);
  26.  
  27. int main()
  28. {
  29. int numScores;
  30. int* scores;
  31. double average;
  32. // Get the number of test scores
  33. cout << "Enter the number of test scores: ";
  34. cin >> numScores;
  35. while (numScores < 2)
  36. {
  37. cout << "Please enter at least 2 test scores: ";
  38. cin >> numScores;
  39. }
  40. // Dynamically allocate the array
  41. scores = new int[numScores];
  42. // Get and validate each test score
  43. for (int count = 0; count < numScores; count++)
  44. {
  45. cout << "Enter test score " << count + 1 << ": ";
  46. cin >> *(scores + count);
  47.  
  48. while (*(scores + count) < 0)
  49. {
  50. cout << "Test scores cannot be negative. Enter again: ";
  51. cin >> *(scores + count);
  52. }
  53. }
  54. // Sort the test scores
  55. sortScores(scores, numScores);
  56. // Calculate the average without the lowest score
  57. average = calculateAverage(scores, numScores);
  58. // Display the sorted scores
  59. cout << endl << "Sorted test scores:" << endl;
  60. for (int count = 0; count < numScores; count++)
  61. {
  62. cout << *(scores + count) << endl;
  63. }
  64. // Display the average
  65. cout << fixed << setprecision(2);
  66. cout << "Average after dropping lowest score: "
  67. << average << endl;
  68. // Release dynamically allocated memory
  69. delete[] scores;
  70. scores = nullptr;
  71. return 0;
  72. }
  73. // Sort scores in ascending order using bubble sort
  74. void sortScores(int* scores, int size)
  75. {
  76. int temp;
  77. for (int pass = 0; pass < size - 1; pass++)
  78. {
  79. for (int count = 0; count < size - 1 - pass; count++)
  80. {
  81. if (*(scores + count) > *(scores + count + 1))
  82. {
  83. temp = *(scores + count);
  84. *(scores + count) = *(scores + count + 1);
  85. *(scores + count + 1) = temp;
  86. }
  87. }
  88. }
  89. }
  90. // Calculate the average without the lowest score
  91. double calculateAverage(int* scores, int size)
  92. {
  93. int total = 0;
  94. // Start at index 1 to skip the lowest score
  95. for (int count = 1; count < size; count++)
  96. {
  97. total += *(scores + count);
  98. }
  99. return static_cast<double>(total) / (size - 1);
  100. }
Success #stdin #stdout 0s 544KB
stdin
3
76
90
101
stdout
Enter the number of test scores: Enter test score 1: Enter test score 2: Enter test score 3: 
Sorted test scores:
76
90
101
Average after dropping lowest score: 95.50