fork download
  1. //Cameron Pham CS1A Chapter 9, p. 537, #2
  2. //
  3. /******************************************************************************
  4. * SORT AND ANALYZE TEST SCORES
  5. * ______________________________________________________________________________
  6. * This program dynamically allocates an array to store a user-defined number of
  7. * test scores. It accepts an integer value for every test score. The program
  8. * then sorts the scores, calculates their average, and displays the sorted
  9. * scores along with the average.
  10. *
  11. * INPUT
  12. * Number of scores // Total number of test scores
  13. * Scores // Every individual test score
  14. *
  15. * OUTPUT
  16. * Average // Average of test scores
  17. *******************************************************************************/
  18. #include <iostream>
  19. #include <iomanip>
  20. using namespace std;
  21.  
  22. // Function prototypes
  23. void sortScores(int* scores, int size);
  24. double calculateAverage(int* scores, int size);
  25.  
  26. int main()
  27. {
  28. // Initialize variables
  29. int numScores; // Number of test scores
  30. int* scores; // Pointer to dynamically allocated array
  31. double average; // Average test score
  32. // Ask for the number of test scores
  33. cout << "Enter the number of test scores: ";
  34. cin >> numScores;
  35. // Validate the number of scores
  36. while (numScores < 1)
  37. {
  38. cout << "Please enter at least 1 test score: ";
  39. cin >> numScores;
  40. }
  41. // Dynamically allocate the array
  42. scores = new int[numScores];
  43. // Get each test score
  44. for (int count = 0; count < numScores; count++)
  45. {
  46. cout << "Enter test score " << count + 1 << ": ";
  47. cin >> *(scores + count);
  48. // Validate the test score
  49. while (*(scores + count) < 0)
  50. {
  51. cout << "Test scores cannot be negative. Enter again: ";
  52. cin >> *(scores + count);
  53. }
  54. }
  55. // Sort the test scores
  56. sortScores(scores, numScores);
  57. // Calculate the average test score
  58. average = calculateAverage(scores, numScores);
  59. // Display the sorted test scores
  60. cout << endl << "Sorted test scores:" << endl;
  61. for (int count = 0; count < numScores; count++)
  62. {
  63. cout << *(scores + count) << endl;
  64. }
  65. // Display the average
  66. cout << fixed << setprecision(2);
  67. cout << "Average test score: " << average << endl;
  68. // Release dynamically allocated memory
  69. delete[] scores;
  70. scores = nullptr;
  71. return 0;
  72. }
  73. // Sort test scores in ascending order using bubble sort
  74. void sortScores(int* scores, int size)
  75. {
  76. int temp;
  77. for (int pass = 0; pass < size - 1; pass++)
  78. {
  79. for (int count = 0; count < size - 1 - pass; count++)
  80. {
  81. if (*(scores + count) > *(scores + count + 1))
  82. {
  83. temp = *(scores + count);
  84. *(scores + count) = *(scores + count + 1);
  85. *(scores + count + 1) = temp;
  86. }
  87. }
  88. }
  89. }
  90. // Calculate and return the average test score
  91. double calculateAverage(int* scores, int size)
  92. {
  93. int total = 0;
  94. for (int count = 0; count < size; count++)
  95. {
  96. total += *(scores + count);
  97. }
  98. return static_cast<double>(total) / size;
  99. }
Success #stdin #stdout 0s 644KB
stdin
4
72
91
93
94.5
stdout
Enter the number of test scores: Enter test score 1: Enter test score 2: Enter test score 3: Enter test score 4: 
Sorted test scores:
72
91
93
94
Average test score: 87.50