Write a C++ program named Lab8. cpp to calculate the average score of students f
ID: 3681358 • Letter: W
Question
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 function calculateAverage () with the following header: double calculateAverage(vector scores); The calculateAverage () function should take only valid scores in the range of [ 0, 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 input 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 Lab8_2. 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 run your program with different cases, and test if your program catches the two types of exceptions.Explanation / Answer
/**The c++ program that prompts user to enter scores
in a range of 0-100 and finds the average score value
if the no exception is occured otherwise prints the type
of exception occured.*/
//header files
#include<iostream>
#include<exception>
#include<stdlib.h>
#include<string>
#include<vector>
using namespace std;
//function prototype
double calculateAverage(vector<double> scores);
//OutOfRange class
class OutOfRange : public exception
{
public:
void error() const throw()
{
cout<<"Out of Range Exception"<<endl;
system("pause");
exit(0);
}
};
// DivideByZero class
class DivideByZero : public exception
{
public:
void error() const throw()
{
cout<<"Divide By Zero Exception"<<endl;
system("pause");
exit(0);
}
};
int main()
{
//Create a vector of type double
vector<double> scores;
char * p;
string usermarks;
cout<<"Enter score in a range of 0 -100 :";
cin>>usermarks;
while(usermarks.at(0)!='Q'&& usermarks.at(0)!='q')
{
scores.push_back(strtod(usermarks.c_str(),&p));
cout<<"Enter score in a range of 0 -100 :";
cin>>usermarks;
}
cout<<"Average Score :"<<
calculateAverage(scores)<<endl;
//pause the program output on console
system("pause");
return 0;
}
/**The method calculateAverage that takes vector of type double
and check if the score is out of range then throws an error
and check if the size of the vector is zero then throw divide by zero
exception otherwise returns the average score value.
*/
double calculateAverage(vector<double> scores)
{
try
{
if(scores.size()==0)
throw DivideByZero();
}
catch(DivideByZero& e)
{
e.error();
}
double sum = 0;
for (int i = 0; i < scores.size(); i++)
{
try
{
if(scores[i]<0 || scores[i]>100)
{
OutOfRange out;
throw out;
}
sum = sum + scores[i];
}
catch(OutOfRange& e)
{
e.error();
}
}
return sum / scores.size();
}
----------------------------------------------------------------------------------------------------------
Sample output:
Enter score in a range of 0 -100 :1
Enter score in a range of 0 -100 :2
Enter score in a range of 0 -100 :3
Enter score in a range of 0 -100 :4
Enter score in a range of 0 -100 :5
Enter score in a range of 0 -100 :q
Average Score :3
Enter score in a range of 0 -100 :1
Enter score in a range of 0 -100 :2
Enter score in a range of 0 -100 :200
Enter score in a range of 0 -100 :q
Out of Range Exception
Enter score in a range of 0 -100 :q
Divide By Zero Exception
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.