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

Average Score: 76.82