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
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int getData(int max_size, string name[], float score[]);
void highestLowestAverageByScore(float tScoreArray[], int tSize, float & tHighestGradeScore, float & tLowestGradeScore, float & tAverageGradeScore);
int main()
{
const int SIZE = 100;
string name[SIZE];
float score[SIZE];
const int actual_size = getData(SIZE, name, score);
if (actual_size > 0)
{
cout << "\n Name Grade\n"
<< " --------------- --------\n";
for (int i = 0; i < actual_size; i++)
{
cout << setw(18) << name[i]
<< setw(14) << score[i] << '\n';
}
}
float highestGradeScore = 0;
float lowestGradeScore = 0;
float averageGradeScore = 0;
highestLowestAverageByScore(score, SIZE, highestGradeScore, lowestGradeScore, averageGradeScore);
cout << "The class average: " << averageGradeScore << endl;
cout << "Highest grade: " << highestGradeScore << endl;
cout << "Lowest grade: " << lowestGradeScore << endl;
system("pause");
return 0;
}
int getData(int max_size, string name[], float score[])
{
for (int i = 0; i < max_size; i++)
{
int count = 0;
cout << "Enter name or enter key to stop: ";
getline(cin, name[i]);
if (name[i].empty()) return i;
cout << "Enter grade: ";
cin >> score[i];
if (!cin) return i;
else cin.ignore(1000, '\n');
}
return max_size;
}
void highestLowestAverageByScore(float tScoreArray[], int tSize, float & tHighestGradeScore, float & tLowestGradeScore, float & tAverageGradeScore)
{
tHighestGradeScore = tScoreArray[0];
tLowestGradeScore = tScoreArray[0];
for (int i = 1; i < tSize; i++)
{
if (tScoreArray[i] > tHighestGradeScore)
{
tHighestGradeScore = tScoreArray[i];
}
if (tScoreArray[i] < tLowestGradeScore)
{
tLowestGradeScore = tScoreArray[i];
}
}
tAverageGradeScore = (tHighestGradeScore + tLowestGradeScore) / 2;
}
|