This C++ program will calculate the average of 4 grades by using weight calculation. It will then display the average and give the user a chance to review the grades.
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- bool isitvalid(double[]);
- bool printarray(double[], int);
- int main()
- {
- // To hold the weights and the grades
- double weights[4];
- double grades[4];
- // To hold the averages
- double avrGrade = 0;
- double avrClassGrade = 0;
- int numOfStudents = 0;
- char respChar = ‘N’;
- int revGrade;
- bool anotherStudent = false;
- // Do until the user decides not to enter anymore students
- do
- {
- // Loop for the user to enter the weight
- for(int i = 0; i < 4; i++)
- {
- cout << “Please enter weight for grade (e.g. 0.3) #” << i + 1 << “: “;
- cin >> weights[i];
- }
- // Loop for the user to enter the grades (until all are valid)
- do
- {
- for(int i = 0; i < 4; i++)
- {
- cout << “Please enter grade #” << i + 1 << “: “;
- cin >> grades[i];
- }
- }
- while(isitvalid(grades) == false);
- // Loop to calculate the average
- for(int i = 0; i < 4; i++)
- {
- avrGrade += weights[i] * grades[i];
- }
- // Show the final grade
- cout << “The final grade is: ” << avrGrade << “\n“;
- // If the user wants to review the grades, call the printarray() function
- cout << “Enter the number of grades you want to review (0 to continue): “;
- cin >> revGrade;
- if(revGrade > 0)
- {
- printarray(grades, revGrade);
- }
- cout << “Would you like to enter another student? Type Y for Yes anything else for No: “;
- cin >> respChar;
- // Holds the average class grade
- avrClassGrade += avrGrade;
- numOfStudents++;
- }
- while(respChar == ‘Y’);
- // When the loops and conditions are all complete show the class average
- cout << “\nThe class average is ” << avrClassGrade / numOfStudents << “\n“;
- system(“PAUSE”);
- return 0;
- }
- bool isitvalid(double grades[])
- {
- // Will be set to true if a bad grade is found
- bool badGrade = false;
- for(int i = 0; i < 4; i++)
- {
- if(grades[i] < 0 || grades[i] > 100)
- {
- badGrade = true;
- }
- }
- if(badGrade == true)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- bool printarray(double grades[], int numOfGrades)
- {
- // Loop through the number of grades the user has decided to review and show them
- for(int i = 0; i < numOfGrades; i++)
- {
- cout << “Grade #” << i + 1 << ” is ” << grades[i] << “\n“;
- }
- }