...
/Solution Review: Calculate the Average Marks of a Class
Solution Review: Calculate the Average Marks of a Class
Let's go over the solution review of the challenge given in the previous lesson.
We'll cover the following...
We'll cover the following...
Solution
Press the RUN button and see the output!
Press + to interact
C++
#include <iostream>using namespace std;// Function calculate_averagedouble average(double marks[], int size) {double sum = 0;double average = 0;// Add all the elements of arrayfor (int i = 0; i < size; i++) {sum = sum + marks[i];}// Calculate average by dividing sum by sizeaverage = sum / size;return average;}// main functionint main() {// Initialize array sizeint size = 8;// Declare variable to store output of functiondouble result;// Initialize arraydouble number [size] = {67, 89, 56, 43, 29, 15, 90,67};// Call function and store its output in resultresult = average (number,size);// Print value of resultcout << "average = " << result;}
Explanation
The basic formula for calculating the average of the values is given below:
...