Answer to Question #180621 in C++ for awais

Question #180621

Suppose that there is only 1kb space is left on stack, but you want to input the marks for 100 students

and calculate their average. Write an efficient C++ program to complete the task.


1
Expert's answer
2021-04-12T10:52:18-0400
#include <iostream>
using namespace std;

void inputMarks(double studentsMarks[], int studentNumber) {
    int i = 0;

    while ( i < studentNumber ) {
        cout << i + 1 << ". " << "Enter student mark: ";
        cin >> studentsMarks[i];
        i += 1;
    }
}

double CalculateAvg(double array[], int size) {
    double sum = 0;
    double avg = 0;

    for ( int i = 0; i < size; i++ ) {
        sum += array[i]; 
    }
    avg = sum / size;
    return avg;
}

int main() {
    const int studentNumber = 100;
    double *studentsMarks = new double[studentNumber];
    double avgMarks;

    inputMarks(studentsMarks, studentNumber);

    avgMarks = CalculateAvg(studentsMarks, studentNumber);

    cout << "\nThe average mark of " << studentNumber << " students is: " << avgMarks << endl;

    delete studentsMarks;

    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