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.
#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;
}
Comments
Leave a comment