Write a class named TestScores. The class constructor should accept an array of test scores as its
argument.
The class should have a member function that returns the average of the test scores. If any test
score in an array is negative or greater than 100 it should display a message.
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
class TestScores{
private:
int scores[10];
public:
TestScores(int sc[]){
for(int i=0;i<10;i++){
scores[i]=sc[i];
}
}
double average(){
double sum=0;
for(int i=0;i<10;i++){
sum+=scores[i];
}
return sum/(double)10.0;
}
};
int main(void){
int scores[10];
for(int i=0;i<10;i++){
scores[i]=-1;
while(scores[i]<0 || scores[i]>100){
cout<<"Enter the score "<<(i+1)<<": ";
cin>>scores[i];
if(scores[i]<0 || scores[i]>100){
cout<<"The score is negative or greater than 100\n\n";
}
}
}
TestScores ts(scores);
cout<<"The average of the test scores: "<<ts.average()<<"\n";
system("pause");
return 0;
}
Comments
Leave a comment