fork download
  1. //Cecilia Rangel CSC5 Chapter 9, P.537, #2
  2. //
  3. /**************************************************************
  4.  *
  5.  * COMPUTE TEST SCORES
  6.  * ____________________________________________________________
  7.  * This program dynammically allocates an array large enough
  8.  * to hold a user defined number of test scores. Once all the
  9.  * scores are entered, the array should be passed to a function
  10.  * that sorts them in ascending order. The program should
  11.  * display the sorted list of scores and averages with
  12.  * appropriate headings.
  13.  *_____________________________________________________________
  14.  * INPUT
  15.  * SIZE : INPUT the ammount of numbers to allocate
  16.  * SCORES : INPUT the test scores to be sorted
  17.  *
  18.  * OUTPUT
  19.  * SORTED : OUTPUT the scores sorted from
  20.  * heighest to lowest
  21.  * AVERAGE : OUTPUT the average of all the scores
  22.  * ***************************************************************/
  23. #include <iostream>
  24. #include <iomanip>
  25. using namespace std;
  26.  
  27. //Function prototypes
  28. void sortElements(int, int*);
  29. int calcAvg(int, int*);
  30.  
  31. int main()
  32. {
  33. int SIZE; //Size of memory allocated
  34. int* ptr; //pointer for memory allocated
  35. ptr = new int[SIZE]; //allocate enough memory for SIZE
  36. int avg; //OUTPUT - average of test scores
  37. //
  38. //User defines size of memory allocated
  39. cout << "Enter the number of test scores to enter.\n";
  40. cin >> SIZE;
  41. //
  42. //acquire test scores
  43. for (int count = 0; count < SIZE; count ++)
  44. {
  45. cout << "Enter test score #" << count + 1 << endl;
  46. cin >> *(ptr + count);
  47. while (*(ptr + count) < 0)
  48. {
  49. cout << "Enter a valid test score.\n";
  50. cin >> *(ptr + count);
  51. }
  52. }
  53. //
  54. //call to functions
  55. avg = calcAvg(SIZE, ptr);
  56. sortElements(SIZE, ptr);
  57. //
  58. //output test scores and average
  59. cout << "Test Scores from lowest grade to highest grade.\n";
  60. for (int count = 0; count < SIZE; count ++)
  61. {
  62. cout << *(ptr + count) << endl;
  63. }
  64. cout << "Test Score average: ";
  65. cout << avg << endl;
  66. return 0;
  67. }
  68.  
  69. void sortElements(int SIZE, int* ptr)
  70. {
  71. int startSearch;
  72. int lowScore;
  73. int lowElement;
  74. for (startSearch = 0; startSearch < SIZE; startSearch++)
  75. {
  76. lowScore = *(ptr + startSearch);
  77. lowElement = startSearch;
  78. for (int count = startSearch + 1; count < SIZE; count++)
  79. {
  80. if (lowScore > *(ptr + count))
  81. {
  82. lowScore = *(ptr + count);
  83. lowElement = count;
  84. }
  85. }
  86. *(ptr + lowElement) = *(ptr + startSearch);
  87. *(ptr + startSearch) = lowScore;
  88. }
  89. }
  90. int calcAvg(int SIZE, int* ptr)
  91. {
  92. int total = 0;
  93. int avg = 0;
  94. for (int count = 0; count < SIZE; count++)
  95. {
  96. total += *(ptr + count);
  97. }
  98. avg = total / SIZE;
  99. return avg;
  100. }
Success #stdin #stdout 0s 5348KB
stdin
5
99
-22
69
52
88
stdout
Enter the number of test scores to enter.
Enter test score #1
Enter test score #2
Enter a valid test score.
Enter test score #3
Enter test score #4
Enter test score #5
Test Scores from lowest grade to highest grade.
0
52
69
88
99
Test Score average: 61