fork download
  1. //Timothy Price CS1A Ch 9 Challenge 2
  2. /******************************************************************************
  3.  * Calculate average score
  4.  *
  5.  * This Program receives a number of test scores, sorts them in ascending order,
  6.  * and calculates the average.
  7.  *
  8.  * Inputs:
  9.  * Number of test scores
  10.  * Test grades
  11.  *
  12.  * Outputs:
  13.  * Average test score
  14.  * Sorted test scores
  15.  *****************************************************************************/
  16.  
  17. #include <iostream>
  18. #include <iomanip>
  19.  
  20. using namespace std;
  21.  
  22. // Function Prototypes
  23. void getScores(double* scores, int count);
  24. void sortScores(double* scores, int count);
  25. double calculateAverage(const double* scores, int count);
  26. void displayResults(const double* scores, int count, double average);
  27.  
  28. int main() {
  29. int numScores = 0;
  30.  
  31. cout << "How many test scores would you like to enter? ";
  32. while (!(cin >> numScores) || numScores <= 0) {
  33. cin.clear();
  34. cin.ignore(10000, '\n');
  35. cout << "Please enter a positive whole number: ";
  36. }
  37.  
  38. // Dynamically allocate array of doubles
  39. double* scores = new double[numScores];
  40.  
  41. getScores(scores, numScores);
  42. sortScores(scores, numScores);
  43. double average = calculateAverage(scores, numScores);
  44. displayResults(scores, numScores, average);
  45.  
  46. // Free dynamically allocated memory
  47. delete[] scores;
  48. scores = nullptr;
  49.  
  50. return 0;
  51. }
  52.  
  53. // Reads non-negative test scores using pointer arithmetic
  54. void getScores(double* scores, int count) {
  55. for (int i = 0; i < count; ++i) {
  56. double value = -1.0;
  57. cout << "Enter score #" << (i + 1) << ": ";
  58. while (!(cin >> value) || value < 0.0) {
  59. cin.clear();
  60. cin.ignore(10000, '\n');
  61. cout << "Invalid input. Enter a non-negative score for #";
  62. cout << (i + 1) << ": ";
  63. }
  64. *(scores + i) = value;
  65. }
  66. }
  67.  
  68. // Sorts scores in ascending order using bubble sort and pointer notation
  69. void sortScores(double* scores, int count) {
  70. for (int i = 0; i < count - 1; ++i) {
  71. for (int j = 0; j < count - i - 1; ++j) {
  72. if (*(scores + j) > *(scores + j + 1)) {
  73. double temp = *(scores + j);
  74. *(scores + j) = *(scores + j + 1);
  75. *(scores + j + 1) = temp;
  76. }
  77. }
  78. }
  79. }
  80.  
  81. // Calculates the average of the scores using pointer notation
  82. double calculateAverage(const double* scores, int count) {
  83. double sum = 0.0;
  84. for (int i = 0; i < count; ++i) {
  85. sum += *(scores + i);
  86. }
  87. return sum / count;
  88. }
  89.  
  90. // Displays sorted scores and average with headings using pointer notation
  91. void displayResults(const double* scores, int count, double average) {
  92. cout << endl;
  93. cout << "--- Sorted Test Scores (Ascending) ---" << endl;
  94. for (int i = 0; i < count; ++i) {
  95. cout << "Score " << (i + 1) << ": " << fixed << setprecision(2);
  96. cout << *(scores + i) << endl;
  97. }
  98.  
  99. cout << endl;
  100. cout << "--- Average Score ---" << endl;
  101. cout << "Average: " << fixed << setprecision(2) << average << endl;
  102. }
Success #stdin #stdout 0s 564KB
stdin
10
100
30
75
75
90
85
50
100
80
75
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: Enter score #6: Enter score #7: Enter score #8: Enter score #9: Enter score #10: 
--- Sorted Test Scores (Ascending) ---
Score 1: 30.00
Score 2: 50.00
Score 3: 75.00
Score 4: 75.00
Score 5: 75.00
Score 6: 80.00
Score 7: 85.00
Score 8: 90.00
Score 9: 100.00
Score 10: 100.00

--- Average Score ---
Average: 76.00