Answer to Question #323482 in C++ for Dinesh

Question #323482

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.

1
Expert's answer
2022-04-04T16:11:41-0400
#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;
}

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