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
|
int getGrades(int[], int);
int calcStats(int[], int, int&, int&, int&);
int main()
{
const int SIZE = 20;
int grades[SIZE];
int max, min, average;
cout << "Enter up to 20 grades, then enter -1 when finished." << endl;
cout << endl;
getGrades(grades, SIZE);
calcStats(grades, SIZE, max, min, average);
cout << endl;
cout << "The highest grade is: " << max << endl;
cout << "The lowest grade is: " << min << endl;
cout << "The average grade is: " << average << endl;
system("pause");
return 0;
}
int getGrades(int grades[], int SIZE)
{
int count = 0;
int total = 0;
const int min_grade = 0;
const int max_grade = 100;
while (count < SIZE)
{
cout << "Enter value #" << count + 1 << ": ";
cin >> grades[count];
if (grades[count] == -1)
break;
if (grades[count] >= min_grade && grades[count] <= max_grade)
{
count++;
}
}
return grades[count];
}
int calcStats(int grades[], int SIZE, int &max, int &min, int &average)
{
int count = 0;
int total = 0;
int min_grade = 0;
int max_grade = 100;
while (count < SIZE)
{
if (grades[count] == -1)
break;
if (grades[count] >= min_grade && grades[count] <= max_grade)
{
total += grades[count];
count++;
}
}
for (int i = 0; i < count; i++)
{
if (max < grades[i])
{
max = grades[i];
}
else if (min > grades[i])
{
min = grades[i];
}
}
if (count > 0)
{
average = total / count;
}
return max, min, average;
}
|