fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 9 P. 537 #2
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute Test Average
  6.  * ____________________________________________________________________________
  7.  * This program will allocate an array not accepting any negative scores and
  8.  * then sorts the scores in ascending order. The program will then compute the
  9.  * average.
  10.  * ____________________________________________________________________________
  11.  * Input
  12.  * numScores :Number of tests
  13.  * scores :Scores on each of the tests
  14.  * Output
  15.  * sortScores :Function that sorts the test scores in ascending order
  16.  * averageScore :Function that computes the average of the scores entered
  17.  *****************************************************************************/
  18. #include <iostream>
  19. #include <iomanip>
  20. using namespace std;
  21.  
  22. //Function Prototype
  23. void sortScores(double* scores, int size);
  24. double averageScore(double* scores, int size);
  25.  
  26. int main() {
  27. //Data Dictionary
  28. int numScores;
  29. double* scores;
  30. double avg;
  31.  
  32. //Prompt User
  33. cout << "How many test scores will you enter? " << endl;
  34. cin >> numScores;
  35.  
  36. //Input Validation
  37. while(numScores <= 0) {
  38. cout << "ERROR: Enter a positive number of test scores: " << endl;
  39. cin >> numScores;
  40. }
  41. scores = new double[numScores];
  42.  
  43. //Input Scores with Validation
  44. for(int i = 0; i < numScores; i++) {
  45. cout << "Enter score #" << (i + 1) << ": " << endl;
  46. cin >> *(scores + i);
  47.  
  48. //Validate!
  49. while(*(scores + i) < 0) {
  50. cout << "ERROR: Score cannot be negative. Re-enter score #" << (i + 1);
  51. cout << ": " << endl;
  52. cin >> *(scores + i);
  53. }
  54. }
  55.  
  56. //Sort Scores
  57. sortScores(scores, numScores);
  58.  
  59. //Calculate Average
  60. avg = averageScore(scores, numScores);
  61.  
  62. //Display!
  63. cout << "Sorted Test Scores: " << endl;
  64. for(int i = 0; i < numScores; i++) {
  65. cout << *(scores + i) << " ";
  66. }
  67. cout << endl;
  68. cout << "Average Score: " << fixed << setprecision(2) << avg << endl;
  69.  
  70. //Free memory
  71. delete [] scores;
  72.  
  73. return 0;
  74. }
  75.  
  76. //Function Definitions
  77. void sortScores(double* scores, int size) {
  78. for(int i = 0; i < size - 1; i++){
  79. for(int j = i + 1; j < size; j++){
  80. if(*(scores + j) < *(scores + i)){
  81. double temp = *(scores + i);
  82. *(scores + i) = *(scores + j);
  83. *(scores + j) = temp;
  84. }
  85. }
  86. }
  87. }
  88.  
  89. double averageScore(double* scores, int size){
  90. double total = 0.0;
  91. for(int i = 0; i < size; i++){
  92. total += *(scores + i);
  93. }
  94. return total/size;
  95. }
Success #stdin #stdout 0.01s 5292KB
stdin
5
75.5
45
89
96
87
stdout
How many test scores will you enter? 
Enter score #1: 
Enter score #2: 
Enter score #3: 
Enter score #4: 
Enter score #5: 
Sorted Test Scores: 
45 75.5 87 89 96 
Average Score: 78.50