Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sample Output: THANK YOU! 5. Write a C++ program named Lab8.cpp to calculate the

ID: 3681567 • Letter: S

Question

Sample Output:

THANK YOU!

5. Write a C++ program named Lab8.cpp to calculate the average score of students for the Midterm Exam Your program will repeatedly ask user to input a score for a student until user press "Q" or "q to quit Define a fiumction calculateAverage() with the following header: double calculateAverage (vector scores); The calculateAverage() fnction should take only valid scores in the range of [o, 100]. If the scores are out of range throw an OutOfRange exception; if the size of the vector is 0 (it represents that user does not imput any valid score before clicking to quit), throw a DivideByZero exception. Define each of these two types of exceptions as a class similar to the one shown in Lab82.cpp. Write the comments for this function, specifying the pre conditions, post conditions and invariants and what exceptions are thrown so that the caller understands how it behaves. Compile and nun your program with different cases, and test if your program catches the two types of exceptions.

Explanation / Answer

#include<iostream>
#include<vector>

using namespace std;

double calculateAverage(vector<double> scores);

int main(){
  
   vector<double> marks;
  
   int score;
   int count = 0;
   cout<<"Enter a student's score :"<<endl;
   cout<<"Press Q to quit."<<endl;
  
   while(cin>>score){
       try{
           if(score < 0 || score > 100) // throw exception if score is in not range
               throw score;
           else{
               marks.push_back(score); // put into vector
               count++;
           }
       }
       catch(int num){
           cout<<"Exception: "<<num<<" is out of range"<<endl;
       }
       cout<<"Enter a student's score :"<<endl;
       cout<<"Press Q to quit."<<endl;
   }
   cout<<" ";
   try{
       double average = calculateAverage(marks);
       cout<<"The average score of "<<count<<" student is: "<<average<<endl;
   }
   catch(char const* message){
       cout<<message<<endl;
   }
}

double calculateAverage(vector<double> scores){
  
   double sum = 0;
   if(scores.size() == 0) // if size os zero, throw an exception
       throw "DivideByZero exception";
   for(unsigned int i=0; i<scores.size(); i++) // getting sum of all scores
       sum = sum + scores[i];
   return sum/scores.size(); // return average
}

/*

Output:

Enter a student's score :
Press Q to quit.
-78
Exception: -78 is out of range
Enter a student's score :
Press Q to quit.
-98
Exception: -98 is out of range
Enter a student's score :
Press Q to quit.
78
Enter a student's score :
Press Q to quit.
990
Exception: 990 is out of range
Enter a student's score :
Press Q to quit.
89
Enter a student's score :
Press Q to quit.
-89
Exception: -89 is out of range
Enter a student's score :
Press Q to quit.
91
Enter a student's score :
Press Q to quit.
72
Enter a student's score :
Press Q to quit.
Q

The average score of 4 student is: 82.5

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote