fork download
  1. //Zachary Abdollahi CS1A Chapter 9, P. 538, #3
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * CALCULATE AVERAGE WITHOUT LOWEST SCORE
  6.  * _____________________________________________________________________________
  7.  * This program dynamically allocates an array to store a user-defined
  8.  * number of test scores, sorts the scores in ascending order, drops the
  9.  * lowest score, and computes the average of the remaining scores.
  10.  *
  11.  * Computation is based on the formula:
  12.  * Average = (Sum of All Scores - Lowest Score) / (Number of Scores - 1)
  13.  * _____________________________________________________________________________
  14.  * INPUT
  15.  * numScores : Number of test scores to process
  16.  * scores : Dynamically allocated array of test scores
  17.  *
  18.  * OUTPUT
  19.  * scores : Test scores sorted in ascending order
  20.  * average : Average score after dropping the lowest score
  21.  *
  22.  ******************************************************************************/
  23. #include <iostream>
  24. #include <iomanip>
  25. using namespace std;
  26.  
  27. // Function Prototypes
  28. void sortScores(double *, int);
  29. double calcAverageDropLowest(const double *, int);
  30.  
  31. int main()
  32. {
  33. int numScores; //INPUT - Number of test scores to process
  34. double *scores; //INPUT - Pointer to dynamic array of scores
  35. double average; //OUTPUT - Average score without lowest score
  36.  
  37. // Prompt User for Number of Test Scores
  38. cout << "How many test scores will you enter? ";
  39. cin >> numScores;
  40.  
  41. // Validate Input (Must be at least 2 to drop 1 and average remaining)
  42. while (numScores < 2)
  43. {
  44. cout << "Please enter at least 2 test scores: ";
  45. cin >> numScores;
  46. }
  47.  
  48. // Dynamically Allocate Array for Test Scores
  49. scores = new double[numScores];
  50.  
  51. // Read Test Scores Using Pointer Arithmetic
  52. for (int count = 0; count < numScores; count++)
  53. {
  54. cout << "Enter test score #" << (count + 1) << ": ";
  55. cin >> *(scores + count);
  56.  
  57. // Validate Non-Negative Score
  58. while (*(scores + count) < 0)
  59. {
  60. cout << "Negative scores are not allowed. Enter again: ";
  61. cin >> *(scores + count);
  62. }
  63. }
  64.  
  65. // Sort Scores in Ascending Order
  66. sortScores(scores, numScores);
  67.  
  68. // Compute Average Dropping Lowest Score
  69. average = calcAverageDropLowest(scores, numScores);
  70.  
  71. // Output Sorted Scores and Final Average
  72. cout << fixed << showpoint << setprecision(2);
  73. cout << "\nSorted Test Scores (Lowest Dropped):\n";
  74. for (int count = 0; count < numScores; count++)
  75. {
  76. cout << *(scores + count);
  77. if (count == 0)
  78. cout << " (Dropped)";
  79. cout << endl;
  80. }
  81. cout << "\nAverage Score: " << average << endl;
  82.  
  83. // Free Dynamically Allocated Memory
  84. delete [] scores;
  85. scores = nullptr;
  86.  
  87. return 0;
  88. }
  89.  
  90. /*******************************************************************************
  91.  * Function: sortScores
  92.  * Performs an ascending selection sort on an array of doubles using
  93.  * pointer notation.
  94.  ******************************************************************************/
  95. void sortScores(double *arr, int size)
  96. {
  97. int startScan, minIndex;
  98. double minValue;
  99.  
  100. for (startScan = 0; startScan < (size - 1); startScan++)
  101. {
  102. minIndex = startScan;
  103. minValue = *(arr + startScan);
  104.  
  105. for (int index = startScan + 1; index < size; index++)
  106. {
  107. if (*(arr + index) < minValue)
  108. {
  109. minValue = *(arr + index);
  110. minIndex = index;
  111. }
  112. }
  113. *(arr + minIndex) = *(arr + startScan);
  114. *(arr + startScan) = minValue;
  115. }
  116. }
  117.  
  118. /*******************************************************************************
  119.  * Function: calcAverageDropLowest
  120.  * Sums all elements after index 0 (the lowest score in a sorted array)
  121.  * and divides by (size - 1).
  122.  ******************************************************************************/
  123. double calcAverageDropLowest(const double *arr, int size)
  124. {
  125. double total = 0.0;
  126.  
  127. // Start at index 1 to skip the lowest score at *(arr + 0)
  128. for (int count = 1; count < size; count++)
  129. {
  130. total += *(arr + count);
  131. }
  132.  
  133. return total / (size - 1);
  134. }
Success #stdin #stdout 0s 564KB
stdin
4
88.5
72.0
95.0
84.5
stdout
How many test scores will you enter? Enter test score #1: Enter test score #2: Enter test score #3: Enter test score #4: 
Sorted Test Scores (Lowest Dropped):
72.00 (Dropped)
84.50
88.50
95.00

Average Score: 89.33