. There are 15 students in the class. Students had their mid term result in INT105 and teacher from CSE domain wants to find the average marks of the class for course code INT105 and display it. Write a program using C++ programming language that consists of an class having name Student containing one integer array data member which stores the total marks of 15 students and float data member that calculate the stores average marks to 10 students. Use constructor to initialize data members and member functions to calculate and display average marks.
#include <iostream>
using namespace std;
class Student {
int marks[15];
float average;
public:
Student(int arr[]);
void calcAverage();
void displayAverage();
};
Student::Student(int arr[]) {
for (int i=0; i<15; i++) {
marks[i] = arr[i];
}
}
void Student::calcAverage() {
int sum = 0;
for (int i=0; i<10; i++) {
sum += marks[i];
}
average = sum / 10.0;
}
void Student::displayAverage() {
cout << "Average marks is " << average << endl;
}
int main() {
int arr[15] = {70, 80, 90, 98, 89, 95, 76, 88, 94, 67};
Student students(arr);
students.calcAverage();
students.displayAverage();
return 0;
}
Comments
Leave a comment