fork download
  1. //Ryan Shahriyarpour CS1A Chapter 9 Question 2
  2. /*****************************************************************************
  3.  *
  4.  * Sort and Average Test Scores
  5.  *
  6.  *********************
  7.  *
  8.  * This program lets the user enter a number of test scores, rejecting any
  9.  * negative scores. It then displays the scores sorted from lowest to
  10.  * highest, along with the average score.
  11.  *
  12.  * INPUT
  13.  * numScores - how many scores will be entered
  14.  * scores - each test score (must not be negative)
  15.  *
  16.  * OUTPUT
  17.  * The sorted list of scores and the average score
  18.  *
  19.  ****************************************************************************/
  20.  
  21. #include <iostream>
  22. #include <iomanip>
  23. using namespace std;
  24.  
  25. // Function prototypes
  26. void sortScores(double *array, int size);
  27. double calcAverage(const double *array, int size);
  28.  
  29. int main()
  30. {
  31. // DATA DICTIONARY
  32. // numScores - number of test scores the user wants to enter
  33. // scores - pointer to the dynamically allocated array of scores
  34. // average - the average of all the scores
  35. // count - loop counter
  36. // startScan - position being filled on each pass of the sort
  37. // index - loop counter used to scan for the smallest value
  38. // minIndex - index of the smallest value found
  39. // minValue - the smallest value found
  40. // total - running total of the scores
  41. int numScores;
  42. double *scores;
  43. double average;
  44. int count;
  45.  
  46. // Get the number of scores
  47. cout << "How many test scores do you want to enter? ";
  48. cin >> numScores;
  49. while (numScores < 1)
  50. {
  51. cout << "Please enter at least 1: ";
  52. cin >> numScores;
  53. }
  54.  
  55. // Allocate the array
  56. scores = new double[numScores];
  57.  
  58. // Get each score and reject negatives
  59. for (count = 0; count < numScores; count++)
  60. {
  61. cout << "Enter test score " << (count + 1) << ": ";
  62. cin >> *(scores + count);
  63. while (*(scores + count) < 0)
  64. {
  65. cout << "Scores cannot be negative. Try again: ";
  66. cin >> *(scores + count);
  67. }
  68. }
  69.  
  70. // Sort the scores and calculate the average
  71. sortScores(scores, numScores);
  72. average = calcAverage(scores, numScores);
  73.  
  74. // Display the results
  75. cout << fixed << setprecision(2);
  76. cout << "\nSorted Scores (low to high)" << endl;
  77. cout << "---------------------------" << endl;
  78. for (count = 0; count < numScores; count++)
  79. cout << *(scores + count) << endl;
  80. cout << "\nAverage Score: " << average << endl;
  81.  
  82. // Free the memory
  83. delete [] scores;
  84. scores = nullptr;
  85.  
  86. return 0;
  87. }
  88.  
  89. // Sorts the array into ascending order using a selection sort
  90. void sortScores(double *array, int size)
  91. {
  92. int startScan, index, minIndex;
  93. double minValue;
  94.  
  95. for (startScan = 0; startScan < size - 1; startScan++)
  96. {
  97. minIndex = startScan;
  98. minValue = *(array + startScan);
  99. for (index = startScan + 1; index < size; index++)
  100. {
  101. if (*(array + index) < minValue)
  102. {
  103. minValue = *(array + index);
  104. minIndex = index;
  105. }
  106. }
  107. *(array + minIndex) = *(array + startScan);
  108. *(array + startScan) = minValue;
  109. }
  110. }
  111.  
  112. // Returns the average of the values in the array
  113. double calcAverage(const double *array, int size)
  114. {
  115. int count;
  116. double total = 0.0;
  117.  
  118. for (count = 0; count < size; count++)
  119. total += *(array + count);
  120.  
  121. return total / size;
  122. }
Success #stdin #stdout 0s 544KB
stdin
5
88
92
94
84
74
stdout
How many test scores do you want to enter? Enter test score 1: Enter test score 2: Enter test score 3: Enter test score 4: Enter test score 5: 
Sorted Scores (low to high)
---------------------------
74.00
84.00
88.00
92.00
94.00

Average Score: 86.40