// grades.cxx /********************************************** /* This program will calculate and print the /* average grade of all the students in a class. /* In addition, it will display the distribution of /* grades in the class, as well as the class maximum and /* class minimum. The user will signal the end of input /* by entering an invalid grade (not in the range 0 -> 100). /* /* Written by: Buzz-Barry Struck /* Modified By: Amruth Kumar /**********************************************/ #include #include #include using namespace std; int main() { // Constants for the grading policy const int A_LIMIT = 91; // A if grade >= 91 const int B_LIMIT = 81; // B if grade >= 81 const int C_LIMIT = 71; // C if grade >= 71 const int D_LIMIT = 55; // D if grade >= 55 int grade; // holds the entered grade int class_size = 0; // holds the number of students in the class int class_max = 0; // holds the class maximum - initialized to min grade int class_min = 100; // holds the class minimum - initialized to max grade int total = 0; // holds the sum of all the grades float average; // holds the value of the average grade int a_scorers = 0; // holds the number of A grades in class int b_scorers = 0; // holds the number of B grades in class int c_scorers = 0; // holds the number of C grades in class int d_scorers = 0; // holds the number of D grades in class int f_scorers = 0; // holds the number of F grades in class cout << "Please, enter a valid grade (0 - 100)\n"; cin >> grade; // Read the grade while( grade >= 0 && grade <= 100 ) { // Values used to calculate the average grade after exiting loop total += grade; // add the new grade to the total class_size++; // increase the number of students // If the entered grade is greater than the maximum grade so far, // record it as the maximum grade if( grade > class_max ) { class_max = grade; } // If the entered grade is less than the minimum grade so far, // record it as the minimum grade if( grade < class_min ) { class_min = grade; } // Increment the appropriate letter grade category if( grade >= A_LIMIT ) // increment the count of A grades { a_scorers++; } else if( grade >= B_LIMIT ) // increment the count of B grades { b_scorers++; } else if( grade >= C_LIMIT ) // increment the count of C grades { c_scorers++; } else if( grade >= D_LIMIT ) // increment the count of D grades { d_scorers++; } else // increment the count of F grades { f_scorers++; } // Reading the next grade from the user cout << "Please, enter the next valid grade (0 - 100)\n"; cin >> grade; // Read the grade } // end of while-loop if( 0 == class_size ) // If no valid grades were entered { cout << "No valid grades were entered. Terminating program\n"; } else { average = (float) total / class_size; // compute the class average cout << class_size << " grades were entered for the class\n"; cout << "The average grade of the class is " << setiosflags( ios::fixed ) << setprecision( 1 ) << average << endl; cout << "The class maximum is " << class_max << endl; cout << "The class minimum is " << class_min << endl; cout << "The grade distribution is as follows:\n"; cout << " A (91 - 100) - " << a_scorers << endl; cout << " B (81 - 90) - " << b_scorers << endl; cout << " C (71 - 80) - " << c_scorers << endl; cout << " D (55 - 70) - " << d_scorers << endl; cout << " F (0 - 54) - " << f_scorers << endl; } return EXIT_SUCCESS; } // end of main