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 104 105
|
#include <fstream>
#include <iostream>
using namespace std;
const int SIZE = 6;
// function to sort scores
int compare(const void*pa, const void* pb)
{
const int& a = *static_cast<const int*>(pa);
const int& b = *static_cast<const int*>(pb);
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
// function to average scores
double getAverage(int* score, int n)
{
int sum = 0;
int i = 0;
for (i = 0; i < n; i++)
sum += score[i];
double average = double(sum) / n;
return average;
}
// function to find A-scores
int countScoresGreater(int* score, int nScores, int b, int c)
{
int nGreater = 0;
int i;
for (i = 0; i < nScores; i++)
if (score[i] >= b && score[i] < c) nGreater++;
return nGreater;
}
// main hub for all
int main()
{
//create an empty list
const int MAX_SCORES = 6;
int nScores = 0;
int score[MAX_SCORES];
// prompt for how many students
cout << "How many records would you like to view? ";
cin >> nScores;
cin.ignore(1000, 10);
cout << " " << endl;
// read and save the scores
for (int i = 0; i < nScores; i++)
{
// read score from user
int aScore;
cout << "Enter score: ";
cin >> aScore;
cin.ignore(1000, 10);
if(i < MAX_SCORES)
score[i] = aScore;
}
qsort (score, MAX_SCORES, sizeof(int), compare);
cout << "\n Sorted: ";
int i;
for (i = 0; i < MAX_SCORES; i++)
cout << score[i] << ' ';
cout << endl;
int max = score[0];
int min = score[0];
for (i = 0; i < MAX_SCORES; i++)
{
if (max < score[i]) max = score[i];
if (min > score[i]) min = score[i];
}
cout << " " << endl;
cout << "highest score: " << max << endl;
cout << "lowest score: " << min << endl;
cout << "average score: " << getAverage(score, MAX_SCORES) << endl;
cout << "number of A scores: " << countScoresGreater(score, nScores, 90, 101) << endl;
cout << "number of B scores: " << countScoresGreater(score, nScores, 80, 90) << endl;
cout << "number of C scores: " << countScoresGreater(score, nScores, 70, 80) << endl;
cout << "number of passing scores: " << countScoresGreater(score, nScores, 70, 101) << endl;
cin >> i;
cin.ignore(1000, 10);
cin.ignore();
cin.get();
return 0;
}
| |