fork download
  1. //Devin Scheu CS1A Chapter 9, P. 538, #3
  2. //
  3. /**************************************************************
  4. *
  5. * PROCESS AND ANALYZE TEST SCORES WITH LOWEST DROPPED
  6. * ____________________________________________________________
  7. * This program determines the sorted list of test scores and
  8. * their average, excluding the lowest score.
  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 excluding the lowest
  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 excluding lowest score using pointers
  44. double calculateAverageExcludingLowest(int* arr, int size) {
  45. if (size <= 0) return 0.0;
  46. double sum = 0;
  47. int* minPtr = arr;
  48. for (int i = 0; i < size; i++) {
  49. if (*(arr + i) < *minPtr) {
  50. minPtr = arr + i;
  51. }
  52. sum += *(arr + i);
  53. }
  54. return (sum - *minPtr) / (size - 1);
  55. }
  56.  
  57. int main () {
  58.  
  59. //Variable Declarations
  60. int numScores; //INPUT - The number of test scores to process
  61. int* testScores; //INPUT - The test scores entered by the user
  62. double averageScore; //OUTPUT - The average of the test scores excluding the lowest
  63. int* sortedScores; //OUTPUT - The sorted list of test scores
  64.  
  65. //Prompt for Input
  66. cout << "Enter the number of test scores: ";
  67. cin >> numScores;
  68. cout << numScores << endl;
  69.  
  70. //Allocate dynamic array
  71. testScores = new int[numScores];
  72.  
  73. //Input Test Scores with EOF check
  74. int validScores = 0;
  75. for (int i = 0; i < numScores && cin >> *(testScores + i); i++) {
  76. while (*(testScores + i) < 0) {
  77. cout << "\nError: Please enter a non-negative score: ";
  78. cin >> *(testScores + i);
  79. }
  80. cout << *(testScores + i) << endl;
  81. validScores++;
  82. }
  83.  
  84. //Adjust numScores to actual valid inputs if less than expected
  85. if (validScores < numScores) {
  86. numScores = validScores;
  87. }
  88.  
  89. //Create copy for sorting
  90. sortedScores = new int[numScores];
  91. for (int i = 0; i < numScores; i++) {
  92. *(sortedScores + i) = *(testScores + i);
  93. }
  94.  
  95. //Sort Scores
  96. bubbleSort(sortedScores, numScores);
  97.  
  98. //Calculate Average Excluding Lowest
  99. averageScore = calculateAverageExcludingLowest(testScores, numScores);
  100.  
  101. //Separator and Output Section
  102. cout << "-------------------------------------------------------" << endl;
  103. cout << "OUTPUT:" << endl;
  104.  
  105. //Output Result
  106. cout << fixed << setprecision(2);
  107. cout << left << setw(25) << "Sorted Scores:" << right;
  108. for (int i = 0; i < numScores; i++) {
  109. cout << *(sortedScores + i);
  110. if (i < numScores - 1) cout << ", ";
  111. }
  112. cout << endl;
  113.  
  114. cout << left << setw(25) << "Average Score (Lowest Dropped):" << right << setw(15) << averageScore << endl;
  115.  
  116. //Clean up memory
  117. delete[] testScores;
  118. delete[] sortedScores;
  119.  
  120. } //end of main()
Success #stdin #stdout 0.01s 5320KB
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 (Lowest Dropped):          54.00