1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <iomanip>
using namespace std;
void getGrades(double*, int);
void sort(double*, int);
double average(double*, int);
void displayGrades(double*, int, double);
int main()
{
int size = 100;
double test_average;
cout << "Please enter a number of scores to be graded: " << endl;
cin >> size;
double test_scores[100];
getGrades(test_scores, size);
test_average = average(test_scores, size);
sort(test_scores, size);
displayGrades(test_scores, size, test_average);
return 0;
}
// Get the test scores
void getGrades(double *score, int size)
{
for (int count = 0; count < size; count++)
{
do
{
cout << "What is the score of test " << (count + 1) << ": " << endl;
cin >> score[count];
if (score[count] < 0)
{
cout << "Score must be a positive number. Please enter another score " << endl;
}
} while (score[count] < 0);
}
}
// Calculate the average of the scores
double average(double *score, int numScores)
{
double avg_score;
double total = 0;
for (int count = 0; count < numScores; count++)
{
total += score[count];
}
avg_score = (total / numScores);
return avg_score;
}
// Sort scores in ascending order
void sort(double *score, int size)
{
bool swap;
double temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (score[count] > score[count + 1])
{
temp = score[count];
score[count] = score[count + 1];
score[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// Display sorted grades
void displayGrades(double *score, int size, double avg)
{
cout << "Below are the sorted scores ";
cout << "Scores";
cout << "-----------" << endl;
for (int count = 0; count < size; count++)
{
cout << score[count] << endl;
}
// Display average
cout << "Score Average: " << avg << endl;
}
| |