fork download
  1. //Amari Mosley CSC5 Chapter 9, P. 538 #4
  2.  
  3. /********************************************************************
  4.  *
  5.  * TEST SCORES #2
  6.  * _________________________________________________________________
  7.  *
  8.  * This program dynamically allocates two arrays to hold student
  9.  * names and test scores. program sorts the
  10.  * scores in ascending order while keeping each student's name
  11.  * matched with their score, then displays the sorted results.
  12.  *
  13.  * _________________________________________________________________
  14.  *
  15.  * INPUT
  16.  * numStudents : Number of students taking the test
  17.  * names : Names of the students
  18.  * scores : Integer test scores
  19.  * OUTPUT
  20.  * sortedScores : Student names and scores in ascending order
  21.  *
  22.  ********************************************************************/
  23. #include <iostream>
  24. #include <string>
  25. using namespace std;
  26.  
  27. // Function Prototype
  28. void sortScores(string*, int*, int);
  29.  
  30. int main()
  31. {
  32. // INPUT
  33. int numStudents;
  34.  
  35. // Get the number of students
  36. cout << "How many students took the test? ";
  37. cin >> numStudents;
  38.  
  39. // Input Validation
  40. while (numStudents <= 0)
  41. {
  42. cout << "Please enter a number greater than 0: ";
  43. cin >> numStudents;
  44. }
  45.  
  46. // Dynamically allocate the arrays
  47. string* names = new string[numStudents];
  48. int* scores = new int[numStudents];
  49.  
  50. // Get student names and test scores
  51. for (int count = 0; count < numStudents; count++)
  52. {
  53. cout << "Enter student #" << (count + 1) << "'s name: ";
  54. cin >> *(names + count);
  55.  
  56. cout << "Enter " << *(names + count) << "'s test score: ";
  57. cin >> *(scores + count);
  58.  
  59. // Input Validation
  60. while (*(scores + count) < 0)
  61. {
  62. cout << "Negative scores are not accepted. Enter again: ";
  63. cin >> *(scores + count);
  64. }
  65. }
  66.  
  67. // Sort the student names and scores
  68. sortScores(names, scores, numStudents);
  69.  
  70. // Output Sorted Student Scores
  71. cout << endl << "Sorted Student Test Scores" << endl;
  72. cout << "______________________________" << endl;
  73. cout << "Student Name\tTest Score" << endl;
  74.  
  75. for (int count = 0; count < numStudents; count++)
  76. {
  77. cout << *(names + count) << "\t\t";
  78. cout << *(scores + count) << endl;
  79. }
  80.  
  81. // Free dynamically allocated memory
  82. delete[] names;
  83. delete[] scores;
  84.  
  85. return 0;
  86. }
  87.  
  88. // Function to sort student names and scores in ascending order
  89. void sortScores(string* names, int* scores, int numStudents)
  90. {
  91. int tempScore;
  92. string tempName;
  93.  
  94. for (int count = 0; count < numStudents - 1; count++)
  95. {
  96. for (int index = count + 1; index < numStudents; index++)
  97. {
  98. if (*(scores + count) > *(scores + index))
  99. {
  100. tempScore = *(scores + count);
  101. *(scores + count) = *(scores + index);
  102. *(scores + index) = tempScore;
  103.  
  104. tempName = *(names + count);
  105. *(names + count) = *(names + index);
  106. *(names + index) = tempName;
  107. }
  108. }
  109. }
  110. }
Success #stdin #stdout 0s 5344KB
stdin
5
Amari
85
John
92
Michael
78
Sarah
100
David
65
stdout
How many students took the test? Enter student #1's name: Enter Amari's test score: Enter student #2's name: Enter John's test score: Enter student #3's name: Enter Michael's test score: Enter student #4's name: Enter Sarah's test score: Enter student #5's name: Enter David's test score: 
Sorted Student Test Scores
______________________________
Student Name	Test Score
David		65
Michael		78
Amari		85
John		92
Sarah		100