Answer to Question #221443 in C for Patil

Question #221443
We want to calculate the total marks of each student of a class in Physics, Chemistry and Mathematics and the average marks of the class. The number of students in the class are entered by the user. Create a class named Marks with data members for roll number, name and marks. Create three other classes inheriting the Marks class, namely Physics, Chemistry and Mathematics, which are used to define marks in individual subject of each student. Roll number of each student will be generated automatically.
1
Expert's answer
2021-07-29T23:30:05-0400
#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */


using namespace std;




class Marks{
private:
	int rollNumber;
	string name;
	int *marks;
	int numberMarks;
	float averageMarks;
public:
	Marks(int numberMarks){
		this->rollNumber = rand() % 1000 + 10000;
		this->marks=new int[numberMarks];
		this->numberMarks=numberMarks;
	}
	virtual void readStudentInfo(){
		cin.ignore();
		cout<<"Enter the student's name: ";
		getline(cin,this->name);
		for(int i=0;i<this->numberMarks;i++){
			cout<<"Enter the student's mark "<<(i+1)<<": ";
			cin>>this->marks[i];
		}
	}
	~Marks(){
		delete[] this->marks;
	}


	virtual void display(){
		cout<<"Roll number: "<<rollNumber<<endl;
		cout<<"Student name: "<<name<<endl;
		cout<<"The total marks of student: "<<calculateTotalMarks()<<endl;
		cout<<fixed<<"The average marks: "<<setprecision(2)<<calculateAverageMarks()<<endl;
	}
	int calculateTotalMarks(){
		int totalMarks=0;
		for(int i=0;i<this->numberMarks;i++){
			totalMarks+=this->marks[i];
		}
		return totalMarks;
	}
	float calculateAverageMarks(){
		return (float)calculateTotalMarks()/(float)this->numberMarks;
	}
};
class Physics : public Marks{
public:
	Physics(int numberMarks) : Marks(numberMarks){}


	void readStudentInfo(){
		cout<<"Enter information about Physics student: "<<endl;
		Marks::readStudentInfo();
	}


	void display(){
		cout<<"Physics Student: "<<endl;
		Marks::display();
	}
};




class Chemistry : public Marks{
public:
	Chemistry(int numberMarks) : Marks(numberMarks){}


	void readStudentInfo(){
		cout<<"Enter information about Chemistry student: "<<endl;
		Marks::readStudentInfo();
	}


	void display(){
		cout<<"Chemistry Student: "<<endl;
		Marks::display();
	}
};




class Mathematics : public Marks {
public:
	Mathematics(int numberMarks) : Marks(numberMarks){}


	void readStudentInfo(){
		cout<<"Enter information about Mathematics student: "<<endl;
		Marks::readStudentInfo();
	}


	void display(){
		cout<<"Mathematics Student: "<<endl;
		Marks::display();
	}
};


int main(){
	srand (time(NULL));


	int numberStudentsInPhysicsClass=0;
	int numberStudentsInChemistryClass=0;
	int numberStudentsInMathematicsClass=0;


	float totalMarkInPhysicsClass=0;
	float totalMarkInChemistryClass=0;
	float totalMarkInMathematicsClass=0;
	
	float averageMarkInPhysicsClass=0;
	float averageMarkInChemistryClass=0;
	float averageMarkInMathematicsClass=0;






	cout<<"Enter the number of students in the Physics class: ";
	cin>>numberStudentsInPhysicsClass;
	cout<<"Enter the number of students in the Chemistry class: ";
	cin>>numberStudentsInChemistryClass;
	cout<<"Enter the number of students in the Mathematics class: ";
	cin>>numberStudentsInMathematicsClass;


	int numberStudentsInClass=numberStudentsInPhysicsClass+numberStudentsInChemistryClass+numberStudentsInMathematicsClass;
	int counter=0;
	Marks** marks=new Marks*[numberStudentsInClass];


	for(int i = 0; i < numberStudentsInPhysicsClass; i++){
		Physics* physics=new Physics(3);
		physics->readStudentInfo();
		marks[counter]=physics;
		totalMarkInPhysicsClass+=marks[counter]->calculateTotalMarks();
		counter++;
	}
	for(int i = 0; i < numberStudentsInChemistryClass; i++){
		Chemistry* chemistry=new Chemistry(3);
		chemistry->readStudentInfo();
		marks[counter]=chemistry;
		totalMarkInChemistryClass+=marks[counter]->calculateTotalMarks();
		counter++;
	}
	for(int i = 0; i < numberStudentsInMathematicsClass; i++){
		Mathematics* mathematics=new Mathematics(3);
		mathematics->readStudentInfo();
		marks[counter]=mathematics;
		totalMarkInMathematicsClass+=marks[counter]->calculateTotalMarks();
		counter++;
	}


	for(int i = 0; i < numberStudentsInClass; i++){
		marks[i]->display();
		cout<<"\n";
	}
	
	averageMarkInPhysicsClass=totalMarkInPhysicsClass/float(numberStudentsInPhysicsClass*3);
	averageMarkInPhysicsClass=totalMarkInChemistryClass/float(numberStudentsInChemistryClass*3);
	averageMarkInMathematicsClass=totalMarkInMathematicsClass/float(numberStudentsInMathematicsClass*3);
	cout << "\n\nThe average marks: \n";
	cout<<fixed<<"The average marks in the Physics class: "<<setprecision(2)<<averageMarkInPhysicsClass<<endl;
	cout<<fixed<<"The average marks in the Chemistry class: "<<setprecision(2)<<averageMarkInPhysicsClass<<endl;
	cout<<fixed<<"The average marks in the Mathematics class: "<<setprecision(2)<<averageMarkInMathematicsClass<<endl;
	for(int i = 0; i < numberStudentsInClass; i++){
		delete marks[i];
	}
	delete[] marks;






	int i;
	cin>>i;
	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
APPROVED BY CLIENTS