Please send questions to
st10@humboldt.edu .
// Contract: grade_avg: int[] int -> double
// Purpose: print to the screen the average of the letter
// grades within <lett_freqs>, which contains
// <num_letts> elements, assuming that:
// A = 4.0
// B = 3.0
// C = 2.0
// D = 1.0
// F = 0.0
// ...and any other elements will be IGNORED for
// averaging purposes.
//
// Examples: If int num_grades[6] = {3, 4, 1, 1, 1, 2},
// then grade_avg(num_letts, 6) == 2.7
//
// by: Sharon M. Tuttle
// last modified: 12-02-05
double grade_avg(int lett_freqs[], int num_letts)
{
const int NUM_GRADE_LETTERS = 5;
double grade_sum = 0;
int num_grades = 0;
double grade_points[NUM_GRADE_LETTERS] = {4.0, 3.0, 2.0, 1.0, 0.0};
// gather the sum of the grade points for the grades in lett_freqs
for (int i=0; i < NUM_GRADE_LETTERS; i++)
{
num_grades += lett_freqs[i];
grade_sum += lett_freqs[i] * grade_points[i];
}
// compute the average, avoiding dividing by zero!
if (num_grades == 0)
{
return 0.0;
}
else
{
return grade_sum / num_grades;
}
}