fork download
  1. //Zachary Abdollahi CS1A Chapter 9, P. 537, #2
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * SORT AND AVERAGE TEST SCORES
  6.  * _____________________________________________________________________________
  7.  * This program dynamically allocates an array to hold a user-defined
  8.  * number of test scores, sorts the scores in ascending order using
  9.  * pointer notation, and calculates the average score.
  10.  *
  11.  * Computation is based on the formula:
  12.  * Average = Sum of All Scores / Number of Scores
  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 of all test scores
  21.  *
  22.  ******************************************************************************/
  23. #include <iostream>
  24. #include <iomanip>
  25. using namespace std;
  26.  
  27. // Function Prototypes
  28. void sortScores(double *, int);
  29. double calcAverage(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 of all test scores
  36.  
  37. // Prompt User for Number of Test Scores
  38. cout << "How many test scores will you enter? ";
  39. cin >> numScores;
  40.  
  41. // Validate Positive Number of Scores
  42. while (numScores <= 0)
  43. {
  44. cout << "Please enter a number greater than 0: ";
  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 Score
  69. average = calcAverage(scores, numScores);
  70.  
  71. // Output Sorted Scores and Average
  72. cout << fixed << showpoint << setprecision(2);
  73. cout << "\nSorted Test Scores:\n";
  74. for (int count = 0; count < numScores; count++)
  75. {
  76. cout << *(scores + count) << endl;
  77. }
  78. cout << "\nAverage Score: " << average << endl;
  79.  
  80. // Free Dynamically Allocated Memory
  81. delete [] scores;
  82. scores = nullptr;
  83.  
  84. return 0;
  85. }
  86.  
  87. /*******************************************************************************
  88.  * Function: sortScores
  89.  * Performs an ascending selection sort on an array of doubles using
  90.  * pointer notation.
  91.  ******************************************************************************/
  92. void sortScores(double *arr, int size)
  93. {
  94. int startScan, minIndex;
  95. double minValue;
  96.  
  97. for (startScan = 0; startScan < (size - 1); startScan++)
  98. {
  99. minIndex = startScan;
  100. minValue = *(arr + startScan);
  101.  
  102. for (int index = startScan + 1; index < size; index++)
  103. {
  104. if (*(arr + index) < minValue)
  105. {
  106. minValue = *(arr + index);
  107. minIndex = index;
  108. }
  109. }
  110. *(arr + minIndex) = *(arr + startScan);
  111. *(arr + startScan) = minValue;
  112. }
  113. }
  114.  
  115. /*******************************************************************************
  116.  * Function: calcAverage
  117.  * Computes the arithmetic mean of an array of doubles using pointer
  118.  * notation.
  119.  ******************************************************************************/
  120. double calcAverage(const double *arr, int size)
  121. {
  122. double total = 0.0;
  123.  
  124. for (int count = 0; count < size; count++)
  125. {
  126. total += *(arr + count);
  127. }
  128.  
  129. return total / size;
  130. }
Success #stdin #stdout 0s 544KB
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:
72.00
84.50
88.50
95.00

Average Score: 85.00