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

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

Average Score: 76