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];
public:
Student(int arr[], int n);
double getAverageMarks(int n);
void displayAverageMarks(int n);
};
Student::Student(int arr[], int n) {
for (int i=0; i<15; i++) {
if (i<n) {
marks[i] = arr[i];
}
else {
marks[i] = 0;
}
}
}
double Student::getAverageMarks(int n) {
int sum = 0;
for (int i=0; i<n; i++) {
sum += marks[i];
}
return static_cast<double>(sum) / n;
}
void Student::displayAverageMarks(int n) {
cout << "Average marks of " << n << " students is "
<< getAverageMarks(n);
}
int main() {
int marks[] = {98, 89, 67, 99, 100, 84, 78, 92, 94, 95};
Student student(marks, 10);
student.displayAverageMarks(10);
return 0;
}
Comments
Leave a comment