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

Average Score: 76