Answer to Question #315903 in C++ for khan

Question #315903

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.


1
Expert's answer
2022-03-22T09:07:30-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog