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

Test 3: 60.9
Test 1: 80.3
Test 2: 90.8

Lowest Score Dropped: 60.9
Average Score: 85.55