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

Andres Alejandro: 80
Andres Guzman: 90

Average Score: 85