fork download
  1. //Zachary Abdollahi CS1A Chapter 9, P. 538, #4
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * SORT STUDENT TEST SCORES
  6.  * _____________________________________________________________________________
  7.  * This program dynamically allocates parallel arrays to store student
  8.  * names and their corresponding integer test scores, sorts both arrays in
  9.  * ascending order by score using pointers, and computes the class average.
  10.  *
  11.  * Computation is based on the formula:
  12.  * Average = Sum of All Scores / Number of Students
  13.  * _____________________________________________________________________________
  14.  * INPUT
  15.  * numStudents : Number of students to process
  16.  * names : Dynamically allocated array of student names
  17.  * scores : Dynamically allocated array of integer test scores
  18.  *
  19.  * OUTPUT
  20.  * names : Student names ordered by ascending test score
  21.  * scores : Student test scores sorted in ascending order
  22.  * average : Average of all student test scores
  23.  *
  24.  ******************************************************************************/
  25. #include <iostream>
  26. #include <iomanip>
  27. #include <string>
  28. using namespace std;
  29.  
  30. // Function Prototypes
  31. void sortStudentData(string *, int *, int);
  32. double calcAverage(const int *, int);
  33.  
  34. int main()
  35. {
  36. int numStudents; //INPUT - Number of students to process
  37. string *names; //INPUT - Pointer to dynamic array of names
  38. int *scores; //INPUT - Pointer to dynamic array of scores
  39. double average; //OUTPUT - Average of all test scores
  40.  
  41. // Prompt User for Number of Students
  42. cout << "How many students took the test? ";
  43. cin >> numStudents;
  44.  
  45. // Validate Positive Number of Students
  46. while (numStudents <= 0)
  47. {
  48. cout << "Please enter a number greater than 0: ";
  49. cin >> numStudents;
  50. }
  51.  
  52. // Dynamically Allocate Parallel Arrays
  53. names = new string[numStudents];
  54. scores = new int[numStudents];
  55.  
  56. // Read Student Name-Score Pairs Using Pointers
  57. for (int count = 0; count < numStudents; count++)
  58. {
  59. cout << "Enter student #" << (count + 1) << "'s name: ";
  60. cin.ignore();
  61. getline(cin, *(names + count));
  62.  
  63. cout << "Enter " << *(names + count) << "'s score: ";
  64. cin >> *(scores + count);
  65.  
  66. // Validate Non-Negative Score
  67. while (*(scores + count) < 0)
  68. {
  69. cout << "Negative scores are not allowed. Enter again: ";
  70. cin >> *(scores + count);
  71. }
  72. }
  73.  
  74. // Sort Parallel Arrays in Ascending Order by Score
  75. sortStudentData(names, scores, numStudents);
  76.  
  77. // Compute Class Average
  78. average = calcAverage(scores, numStudents);
  79.  
  80. // Output Sorted Student Roster and Average
  81. cout << "\nSorted Student Scores:\n";
  82. cout << "-----------------------------\n";
  83. for (int count = 0; count < numStudents; count++)
  84. {
  85. cout << left << setw(20) << *(names + count)
  86. << right << setw(5) << *(scores + count) << endl;
  87. }
  88. cout << fixed << showpoint << setprecision(2);
  89. cout << "\nClass Average: " << average << endl;
  90.  
  91. // Free Dynamically Allocated Memory
  92. delete [] names;
  93. delete [] scores;
  94. names = nullptr;
  95. scores = nullptr;
  96.  
  97. return 0;
  98. }
  99.  
  100. /*******************************************************************************
  101.  * Function: sortStudentData
  102.  * Performs an ascending selection sort on the scores array while keeping
  103.  * the parallel names array synchronized using pointer notation.
  104.  ******************************************************************************/
  105. void sortStudentData(string *namesArr, int *scoresArr, int size)
  106. {
  107. int startScan, minIndex, minScore;
  108. string minName;
  109.  
  110. for (startScan = 0; startScan < (size - 1); startScan++)
  111. {
  112. minIndex = startScan;
  113. minScore = *(scoresArr + startScan);
  114. minName = *(namesArr + startScan);
  115.  
  116. for (int index = startScan + 1; index < size; index++)
  117. {
  118. if (*(scoresArr + index) < minScore)
  119. {
  120. minScore = *(scoresArr + index);
  121. minName = *(namesArr + index);
  122. minIndex = index;
  123. }
  124. }
  125. *(scoresArr + minIndex) = *(scoresArr + startScan);
  126. *(scoresArr + startScan) = minScore;
  127.  
  128. *(namesArr + minIndex) = *(namesArr + startScan);
  129. *(namesArr + startScan) = minName;
  130. }
  131. }
  132.  
  133. /*******************************************************************************
  134.  * Function: calcAverage
  135.  * Computes the floating-point average of an integer array using pointer
  136.  * notation.
  137.  ******************************************************************************/
  138. double calcAverage(const int *arr, int size)
  139. {
  140. int total = 0;
  141.  
  142. for (int count = 0; count < size; count++)
  143. {
  144. total += *(arr + count);
  145. }
  146.  
  147. return static_cast<double>(total) / size;
  148. }
Success #stdin #stdout 0s 544KB
stdin
3
Maria Lopez
92
David Chen
78
Alex Rivera
85
stdout
How many students took the test? Enter student #1's name: Enter Maria Lopez's score: Enter student #2's name: Enter David Chen's score: Enter student #3's name: Enter Alex Rivera's score: 
Sorted Student Scores:
-----------------------------
David Chen             78
Alex Rivera            85
Maria Lopez            92

Class Average: 85.00