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