fork download
  1. // Torrez, Elaine CS1A Chapter 9, P. 537, #2
  2.  
  3. /********************************************************************************************
  4.  *
  5.  * TEST SCORES (DYNAMIC)
  6.  *
  7.  * ------------------------------------------------------------------------------------------
  8.  * This program dynamically allocates an array to store test scores entered by the user.
  9.  * After storing the scores, the program sorts them in ascending order, calculates the
  10.  * average score, and then displays the sorted list along with the average.
  11.  *
  12.  * ------------------------------------------------------------------------------------------
  13.  * INPUT
  14.  * numScores : Number of test scores the user wants to enter
  15.  * scores : Individual test scores entered by the user
  16.  *
  17.  * OUTPUT
  18.  * Sorted list of test scores
  19.  * Average test score
  20.  *
  21.  ********************************************************************************************/
  22.  
  23. #include <iostream>
  24. #include <iomanip>
  25. using namespace std;
  26.  
  27. // FUNCTION PROTOTYPES
  28. void sortArray(double *arr, int size);
  29. double getAverage(double *arr, int size);
  30.  
  31. int main()
  32. {
  33. int numScores; // Number of scores to enter
  34. double *scores = nullptr;
  35.  
  36. // INPUT: Ask user how many scores they want to enter
  37. cout << "How many test scores would you like to enter? ";
  38. cin >> numScores;
  39.  
  40. // INPUT VALIDATION
  41. while (numScores <= 0)
  42. {
  43. cout << "Error: Enter a value greater than 0: ";
  44. cin >> numScores;
  45. }
  46.  
  47. // Dynamically allocate the array
  48. scores = new double[numScores];
  49.  
  50. // INPUT: Enter each test score
  51. for (int i = 0; i < numScores; i++)
  52. {
  53. cout << "Enter score #" << (i + 1) << ": ";
  54. cin >> scores[i];
  55.  
  56. while (scores[i] < 0) // No negative scores allowed
  57. {
  58. cout << "Error: Score must be 0 or higher. Re-enter: ";
  59. cin >> scores[i];
  60. }
  61. }
  62.  
  63. // Sort the array
  64. sortArray(scores, numScores);
  65.  
  66. // OUTPUT: Display sorted list
  67. cout << "\nSorted Test Scores:\n";
  68. for (int i = 0; i < numScores; i++)
  69. {
  70. cout << fixed << setprecision(2);
  71. cout << "Score [" << i << "]: " << scores[i] << endl;
  72. }
  73.  
  74. // OUTPUT: Display average
  75. cout << "\nAverage Score: " << getAverage(scores, numScores) << endl;
  76.  
  77. // Free memory
  78. delete [] scores;
  79. scores = nullptr;
  80.  
  81. return 0;
  82. }
  83.  
  84. //*******************************************************************************
  85. // sortArray
  86. //------------------------------------------------------------------------------
  87. // Sorts an array of doubles in ascending order using simple selection sort.
  88. //*******************************************************************************
  89. void sortArray(double *arr, int size)
  90. {
  91. int minIndex;
  92. double minValue;
  93.  
  94. for (int start = 0; start < size - 1; start++)
  95. {
  96. minIndex = start;
  97. minValue = arr[start];
  98.  
  99. for (int i = start + 1; i < size; i++)
  100. {
  101. if (arr[i] < minValue)
  102. {
  103. minValue = arr[i];
  104. minIndex = i;
  105. }
  106. }
  107.  
  108. arr[minIndex] = arr[start];
  109. arr[start] = minValue;
  110. }
  111. }
  112.  
  113. //*******************************************************************************
  114. // getAverage
  115. //------------------------------------------------------------------------------
  116. // Returns the average value of all test scores stored in the array.
  117. //*******************************************************************************
  118. double getAverage(double *arr, int size)
  119. {
  120. double total = 0;
  121.  
  122. for (int i = 0; i < size; i++)
  123. total += arr[i];
  124.  
  125. return (total / size);
  126. }
  127.  
Success #stdin #stdout 0.01s 5312KB
stdin
5
90
80
70
85
100
stdout
How many test scores would you like to enter? Enter score #1: Enter score #2: Enter score #3: Enter score #4: Enter score #5: 
Sorted Test Scores:
Score [0]: 70.00
Score [1]: 80.00
Score [2]: 85.00
Score [3]: 90.00
Score [4]: 100.00

Average Score: 85.00