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

Test 3: 60
Test 1: 80
Test 2: 90

Lowest Score Dropped: 60
Average Score: 85