fork download
  1. //Andrew Alspaugh CS1A Chapter 9. P. 537. #2
  2.  
  3. /****************************************************************************
  4. Process User Input Scores
  5. ____________________________________________________________________________
  6. This program lets the user input any amount of scores; while the scores are
  7. being input, the program creates a new array everytime to ensure the size
  8. of the array is exactly right for the amount of inputs. Once the user inputs
  9. the test scores they use the sentinal value -1 to end putting in inputs.
  10.  
  11. After the test scores are input, the program then uses a function SortScores
  12. to sort the scores in ascending order. Then the program calls GetAverage function
  13. to calculate the average of all of the scores
  14.  
  15. The program dislplays the total number of scores, the scores in ascending order,
  16. and the average of all scores.
  17. _____________________________________________________________________________
  18. //input
  19. int score = 0; //input score
  20. int sizeAccumulator = 1; //size of array
  21. int count = 0;
  22.  
  23. //output
  24. int *testScores; //pointer to beginning of testScores Array
  25. testScores = new int[sizeAccumulator]; //Pointer Definition
  26.  
  27. //SORTSCORES FUNCTION
  28. bool swap;
  29. int temp;
  30.  
  31. //GETAVERAGE FUNCTION
  32. int total = 0;
  33.  
  34. float Average; //Average
  35.  
  36. *****************************************************************************/
  37. #include <iostream>
  38. using namespace std;
  39.  
  40. void SortScores(int *array, int size);
  41. float GetAverage(int *array, int size);
  42. int main()
  43. {
  44. //Data Dictionary
  45. //input
  46. int score = 0; //input score
  47. int sizeAccumulator = 1; //size of array
  48. int count = 0;
  49.  
  50. //output
  51. int *testScores; //pointer to beginning of testScores Array
  52. testScores = new int[sizeAccumulator]; //Pointer Definition
  53.  
  54. //SORTSCORES FUNCTION
  55. bool swap;
  56. int temp;
  57.  
  58. //GETAVERAGE FUNCTION
  59. int total = 0;
  60.  
  61. float Average; //Average
  62.  
  63.  
  64. //INPUT
  65. cout << "Enter Test Scores: Enter -1 to quit: " << endl;
  66. while(score != -1)
  67. {
  68. cin >> score;
  69. if (score != -1)
  70. {
  71. if (count == sizeAccumulator)
  72. {
  73. sizeAccumulator += 1;
  74. int* newTestScores = new int [sizeAccumulator];
  75. for(int i = 0; i < count; i++)
  76. {
  77. newTestScores[i] = testScores[i];
  78.  
  79. }
  80. delete[] testScores;
  81. testScores = newTestScores;
  82. }
  83. testScores[count] = score;
  84. count++;
  85. }
  86. }
  87.  
  88. //Process
  89. SortScores(testScores, count);
  90.  
  91. Average = GetAverage(testScores, count);
  92.  
  93. //Output
  94. cout << "There are " << sizeAccumulator << " scores" << endl;
  95. for(int i = 0; i < sizeAccumulator; i++ )
  96. {
  97. cout << "Score " << (i+1) << " is: " << *(testScores + i) << endl;
  98. }
  99.  
  100. cout << "Average is: " << Average << endl;
  101.  
  102. delete [] testScores;
  103.  
  104. return 0;
  105. }
  106.  
  107. //Sort the scores using a bubble sort
  108. void SortScores (int *array, int size)
  109. {
  110. bool swap;
  111. int temp;
  112.  
  113. do
  114. {
  115. swap = false;
  116. for (int count = 0; count < (size - 1); count++)
  117. {
  118. if (array[count] > array[count + 1])
  119. {
  120. temp = array[count];
  121. array[count] = array [count + 1];
  122. array [count + 1] = temp;
  123. swap = true;
  124. }
  125. }
  126. }
  127. while (swap);
  128. }
  129.  
  130. // Find the Average function
  131. float GetAverage(int *array, int size)
  132. {
  133. int total = 0;
  134.  
  135. for(int i = 0; i < size; i++)
  136. total += array[i];
  137.  
  138. return static_cast<double>(total)/size;
  139. }
Success #stdin #stdout 0.01s 5316KB
stdin
98
89
79
76
67
15
36
42
100
85
76
91
78
-1
stdout
Enter Test Scores: Enter -1 to quit: 
There are 13 scores
Score 1 is: 15
Score 2 is: 36
Score 3 is: 42
Score 4 is: 67
Score 5 is: 76
Score 6 is: 76
Score 7 is: 78
Score 8 is: 79
Score 9 is: 85
Score 10 is: 89
Score 11 is: 91
Score 12 is: 98
Score 13 is: 100
Average is: 71.6923