fork download
  1. // Andres Guzman CSC5 Chapter 9, P. 538, #4
  2. /******************************************************************************
  3.  *
  4.  * CALCULATE TEST SCORES #2
  5.  * ____________________________________________________________________________
  6.  * Program will take entered student names and test scores, dynamically
  7.  * allocate memory, and sort the scores in ascending order while keeping each
  8.  * student's name matched with their corresponding test score.
  9.  *
  10.  * Computation:
  11.  * Uses selection sort to arrange test scores from lowest to highest while
  12.  * swapping the corresponding student names. Uses the formula "total / b"
  13.  * to calculate the average among the inputed test scores.
  14.  * ____________________________________________________________________________
  15.  *
  16.  * INPUT
  17.  *
  18.  * SIZE = Inputed Array Size
  19.  * names = Student Names
  20.  * test = Variable to hold inputed test scores
  21.  *
  22.  * OUTPUT
  23.  *
  24.  * names = Student Names Sorted by Test Score
  25.  * test = Corresponding Sorted Test Scores
  26.  * averagescore = Average Test Score
  27.  *
  28.  ******************************************************************************/
  29. #include <iostream>
  30. #include <string>
  31. #include <iomanip>
  32. using namespace std;
  33. //
  34. //Function Prototypes
  35. void ascendingorder(string *a, double *b, int c); //Sorts numbers from low to high
  36. double average(double *a, int b); //Calculates Average
  37. //
  38. int main()
  39. {
  40. int SIZE; //Array Size
  41.  
  42. cout << "Enter Amount of Test Scores: ";
  43. cin >> SIZE;
  44. while (SIZE <= 0) //Input Validation
  45. {
  46. cout << "Enter Positive Value" << endl;
  47. cout << "Enter Amount of Test Scores: ";
  48. cin >> SIZE;
  49. }
  50.  
  51. string *names = new string[SIZE]; //Dynamically Allocates Memory
  52. double *test = new double[SIZE]; //Dynamically Allocates Memory
  53. //
  54. //Allows input value for each subscript
  55. cin.ignore();
  56. for(int count = 0; count < SIZE; count++)
  57. {
  58. cout << "\nEnter Student Name: ";
  59. getline(cin, *(names + count));
  60. cout << "\nEnter Test Score: ";
  61. cin >> *(test + count);
  62. while(*(test + count) < 0) //Input Validation
  63. {
  64. cout << "Please Enter Positive Value." << endl;
  65. cout << "Test " << (count + 1) << ": ";
  66. cin >> *(test + count);
  67. }
  68. cin.ignore();
  69. }
  70. cout << "\n\n"; //Spacing
  71. //
  72. //Function Call for sorting
  73. ascendingorder(names, test, SIZE);
  74. //
  75. //For loop to step through array subscript and output test scores accordingly
  76. for(int count = 0; count < SIZE; count++)
  77. {
  78. cout << *(names + count) << ": " << *(test + count) << endl;
  79. }
  80. cout << "\n"; //Spacing
  81. //
  82. //Function call for average score calculation
  83. double averagescore = average(test, SIZE);
  84. //
  85. //Output results
  86. cout << fixed << setprecision(2);
  87. cout << "Average Score: " << averagescore << endl;
  88. delete [] names; //Deletes new memory
  89. delete [] test; //Deletes new memory
  90. return 0;
  91. }
  92.  
  93. /*******************************************************************************
  94.  * Adding definition to function. The function will use selection sort to
  95.  * arrange the inputed test scores in ascending order while keeping the
  96.  * corresponding student names matched with their test scores.
  97.  ******************************************************************************/
  98. void ascendingorder(string *a, double *b, int c)
  99. {
  100. int startscan, minIndex; //Used to scan through the array
  101. double minValue; //Holds lowest value found
  102. string tempname;
  103. //
  104. //Initializing For Loop
  105. for (startscan = 0; startscan < (c - 1); startscan++)
  106. {
  107. minIndex = startscan; //Assigns starting position as lowest
  108. minValue = *(b + startscan); //Assigns starting number as lowest
  109. //
  110. //Searches for lowest value
  111. for (int index = startscan + 1; index < c; index++)
  112. {
  113. if(*(b + index) < minValue)
  114. {
  115. minValue = *(b + index); //Updates lowest number value
  116. minIndex = index; //stores position of lowest value
  117. }
  118. }
  119. //
  120. //Swaps Values
  121. *(b + minIndex) = *(b + startscan);
  122. *(b + startscan) = minValue;
  123.  
  124. tempname = *(a + minIndex);
  125. *(a + minIndex) = *(a + startscan);
  126. *(a + startscan) = tempname;
  127. }
  128. }
  129. /*******************************************************************************
  130.  * Adding definition to function. The function will accumulate the total scores
  131.  * and calculate the average of the input values and return the value.
  132.  ******************************************************************************/
  133. double average(double *a, int b)
  134. {
  135. double total = 0; //Accumulator
  136. double average; //Holder for formula
  137. //
  138. //For loop to step through subscript
  139. for(int count = 0; count < b; count++)
  140. {
  141. total += *(a + count); //Adds Value to accumulator
  142. }
  143. average = total / b; //Calculates Average
  144. return average; //Returns Value
  145. }
  146.  
Success #stdin #stdout 0s 544KB
stdin
2
Andres Guzman
90.9
Andres Alejandro
80.2
stdout
Enter Amount of Test Scores: 
Enter Student Name: 
Enter Test Score: 
Enter Student Name: 
Enter Test Score: 

Andres Alejandro: 80.2
Andres Guzman: 90.9

Average Score: 85.55