fork download
  1. //Amari Mosley CSC5 Chapter 9, P. 537 #2
  2.  
  3. /********************************************************************
  4.  *
  5.  * ALLOCATE TEST SCORES
  6.  * _________________________________________________________________
  7.  *
  8.  * This program dynamically allocates an array to hold a user-defined
  9.  * number of test scores. It accepts the scores, and displays the sorted
  10.  * scores and average.
  11.  *
  12.  * _________________________________________________________________
  13.  *
  14.  * INPUT
  15.  * numScores : Number of test scores to enter
  16.  * scores : Test scores entered by the user
  17.  * OUTPUT
  18.  * sortedScores : Test scores sorted in ascending order
  19.  * average : Average of all test scores
  20.  *
  21.  ********************************************************************/
  22. #include <iostream>
  23. #include <iomanip>
  24. using namespace std;
  25.  
  26. // Function Prototypes
  27. void sortScores(double*, int);
  28. double calculateAverage(double*, int);
  29.  
  30. int main()
  31. {
  32. // INPUT
  33. int numScores;
  34.  
  35. // Get the number of test scores
  36. cout << "How many test scores would you like to enter? ";
  37. cin >> numScores;
  38.  
  39. // Input Validation
  40. while (numScores <= 0)
  41. {
  42. cout << "Please enter a number greater than 0: ";
  43. cin >> numScores;
  44. }
  45.  
  46. // Dynamically allocate the array
  47. double* scores = new double[numScores];
  48.  
  49. // Get test scores from the user
  50. for (int count = 0; count < numScores; count++)
  51. {
  52. cout << "Enter test score #" << (count + 1) << ": ";
  53. cin >> *(scores + count);
  54.  
  55. // Input Validation
  56. while (*(scores + count) < 0)
  57. {
  58. cout << "Negative scores are not accepted. Enter again: ";
  59. cin >> *(scores + count);
  60. }
  61. }
  62.  
  63. // Sort the test scores
  64. sortScores(scores, numScores);
  65.  
  66. // Calculate the average
  67. double average = calculateAverage(scores, numScores);
  68.  
  69. // Output Sorted Test Scores
  70. cout << endl << "Sorted Test Scores" << endl;
  71. cout << "________________________" << endl;
  72.  
  73. for (int count = 0; count < numScores; count++)
  74. {
  75. cout << fixed << setprecision(2);
  76. cout << *(scores + count) << endl;
  77. }
  78.  
  79. // Output Average
  80. cout << endl << "Average Test Score: ";
  81. cout << fixed << setprecision(2) << average << endl;
  82.  
  83. // Free dynamically allocated memory
  84. delete[] scores;
  85.  
  86. return 0;
  87. }
  88.  
  89. // Function to sort scores in ascending order
  90. void sortScores(double* scores, int numScores)
  91. {
  92. double temp;
  93.  
  94. for (int count = 0; count < numScores - 1; count++)
  95. {
  96. for (int index = count + 1; index < numScores; index++)
  97. {
  98. if (*(scores + count) > *(scores + index))
  99. {
  100. temp = *(scores + count);
  101. *(scores + count) = *(scores + index);
  102. *(scores + index) = temp;
  103. }
  104. }
  105. }
  106. }
  107.  
  108. // Function to calculate the average test score
  109. double calculateAverage(double* scores, int numScores)
  110. {
  111. double total = 0;
  112.  
  113. for (int count = 0; count < numScores; count++)
  114. {
  115. total += *(scores + count);
  116. }
  117.  
  118. return total / numScores;
  119. }
Success #stdin #stdout 0s 5356KB
stdin
5
85
92
78
100
65
stdout
How many test scores would you like to enter? Enter test score #1: Enter test score #2: Enter test score #3: Enter test score #4: Enter test score #5: 
Sorted Test Scores
________________________
65.00
78.00
85.00
92.00
100.00

Average Test Score: 84.00