fork download
  1. //Devin Scheu CS1A Chapter 9, P. 537, #2
  2. //
  3. /**************************************************************
  4. *
  5. * PROCESS AND ANALYZE TEST SCORES
  6. * ____________________________________________________________
  7. * This program determines the sorted list of test scores and
  8. * their average based on a user-defined number of scores.
  9. * ____________________________________________________________
  10. * INPUT
  11. * numScores : The number of test scores to process
  12. * testScores : The test scores entered by the user
  13. *
  14. * OUTPUT
  15. * sortedScores : The sorted list of test scores
  16. * averageScore : The average of the test scores
  17. *
  18. **************************************************************/
  19.  
  20. #include <iostream>
  21. #include <iomanip>
  22.  
  23. using namespace std;
  24.  
  25. // Function to swap two integers using pointers
  26. void swap(int* a, int* b) {
  27. int temp = *a;
  28. *a = *b;
  29. *b = temp;
  30. }
  31.  
  32. // Function to perform bubble sort using pointers
  33. void bubbleSort(int* arr, int size) {
  34. for (int i = 0; i < size - 1; i++) {
  35. for (int j = 0; j < size - i - 1; j++) {
  36. if (*(arr + j) > *(arr + j + 1)) {
  37. swap((arr + j), (arr + j + 1));
  38. }
  39. }
  40. }
  41. }
  42.  
  43. // Function to calculate average using pointers
  44. double calculateAverage(int* arr, int size) {
  45. double sum = 0;
  46. for (int i = 0; i < size; i++) {
  47. sum += *(arr + i);
  48. }
  49. return sum / size;
  50. }
  51.  
  52. int main () {
  53.  
  54. //Variable Declarations
  55. int numScores; //INPUT - The number of test scores to process
  56. int* testScores; //INPUT - The test scores entered by the user
  57. double averageScore; //OUTPUT - The average of the test scores
  58. int* sortedScores; //OUTPUT - The sorted list of test scores
  59.  
  60. //Prompt for Input
  61. cout << "Enter the number of test scores: ";
  62. cin >> numScores;
  63. cout << numScores << endl;
  64.  
  65. //Allocate dynamic array
  66. testScores = new int[numScores];
  67.  
  68. //Input Test Scores with EOF check
  69. int validScores = 0;
  70. for (int i = 0; i < numScores && cin >> *(testScores + i); i++) {
  71. while (*(testScores + i) < 0) {
  72. cout << "\nError: Please enter a non-negative score: ";
  73. cin >> *(testScores + i);
  74. }
  75. cout << *(testScores + i) << endl;
  76. validScores++;
  77. }
  78.  
  79. //Adjust numScores to actual valid inputs if less than expected
  80. if (validScores < numScores) {
  81. numScores = validScores;
  82. }
  83.  
  84. //Create copy for sorting
  85. sortedScores = new int[numScores];
  86. for (int i = 0; i < numScores; i++) {
  87. *(sortedScores + i) = *(testScores + i);
  88. }
  89.  
  90. //Sort Scores
  91. bubbleSort(sortedScores, numScores);
  92.  
  93. //Calculate Average
  94. averageScore = calculateAverage(testScores, numScores);
  95.  
  96. //Separator and Output Section
  97. cout << "-------------------------------------------------------" << endl;
  98. cout << "OUTPUT:" << endl;
  99.  
  100. //Output Result
  101. cout << fixed << setprecision(2);
  102. cout << left << setw(25) << "Sorted Scores:" << right;
  103. for (int i = 0; i < numScores; i++) {
  104. cout << *(sortedScores + i);
  105. if (i < numScores - 1) cout << ", ";
  106. }
  107. cout << endl;
  108.  
  109. cout << left << setw(25) << "Average Score:" << right << setw(15) << averageScore << endl;
  110.  
  111. //Clean up memory
  112. delete[] testScores;
  113. delete[] sortedScores;
  114.  
  115. } //end of main()
Success #stdin #stdout 0.01s 5300KB
stdin
87
34
54
21
17
99
62
stdout
Enter the number of test scores: 87
34
54
21
17
99
62
-------------------------------------------------------
OUTPUT:
Sorted Scores:           17, 21, 34, 54, 62, 99
Average Score:                     47.83