Write a class named TestScores. The class constructor should accept an array of
ID: 3818696 • Letter: W
Question
Write a class named TestScores. The class constructor should accept an array of integer test_scores and the count of test_scores as its arguments and set two protected member variable: an integer pointer for tests and an integer for the count of tests. Assign the tests pointer the base address of the passed integer test_scores array and the value of passed test_scores count to count. The class will also have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class will throw a INVALID_TESTSCORE exception. Create the empty class InvalidTestScore to be used as the exception object. Write the class and main to demonstrate the class constructor and the calculate average member function in a program. Main can be written however you like (menu based or one run and done, static array or dynamic array of test scores, prompt user for testscores or array initialization list)
to start with
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
protected:
int* test;
int count;
public:
TestScores(const int[], int);
float calcAverage();
};
//All the way down is notes I took from class. I already started the code above, and would like to use that start from that. We are using most of those libraries for c++. Thank you!!
/*
//these are my notes
try{
} catch( //yes){
}catch(// ){
}catch(//...){
//default
}
//more notes
class INVALID_TESTSCORE{};
//float calc Average()
total=0, res=0
for(....)
if(testscore is OutofRange)
throw(Invalid test score())
//update total
return avg;
}
// Main
TestScores ts(arr, count);
float avg;
try{
avg=ts calcAverage();
}catch(INVALID_TESTSCORE err){
cout << "invalid../n";
}while(flag)
*/
Explanation / Answer
#include<iostream>
#include<cstring>
#include<iomanip>
#include <exception>
using namespace std;
//
class InvalidTestScore :public exception
{
virtual const char* what() const throw()
{
return "INVALID_TESTSCORE ";
}
};
class TestScores :public InvalidTestScore
{
protected:
const int* test;
int count;
public:
TestScores(const int arr[], int);
float calcAverage();
};
TestScores::TestScores(const int arr[], int cnt)
{
test = &arr[0] ;
count = cnt;
}
float TestScores::calcAverage()
{
float sum = 0,avg;
InvalidTestScore e;
for (int i = 0; i < count; i++)
{
if (test[i] < 0 || test[i] >100)
{
throw e;
}
sum += test[i];
}
avg = sum / count;
return avg;
}
int main()
{
//create 5 tests
int test[5] = { 80, 70, 89, 90, 100 };
TestScores t(test, 5);
//call calcAverage
try
{
cout << "test score average = " << t.calcAverage() << endl;
}
catch (exception& e)
{
cout << e.what() << endl;
}
}
------------------------------------------------------------------------------------------------------
this is output when tescore has any of the values invalid ie less than 0 or greater than 100
//output
when test array is test[5] = { -80, 70, 89, 90, 100 }, we get output as below
INVALID_TESTSCORE
when test array has test[5] = { 80, 70, 89, 90, 101 },we get below output
INVALID_TESTSCORE
//output
when test array has test[5] = { 80, 70, 89, 90, 100 }, we get output as below
test score average = 85.8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.