Menu
Coddy logo textTech

Student Grade Calculator

Part of the Fundamentals section of Coddy's C++ journey — lesson 74 of 74.

challenge icon

Challenge

Easy

Create a function named calculateAverageGrade that takes an array of integers (representing student grades) and the size of the array as input and returns the average grade as a double. The function should do the following:

  1. Calculate the sum of all grades in the array.
  2. Calculate the average grade by dividing the sum by the number of grades.
  3. Return the average grade

Finally the main function should print the result using the following format: Average grade: [The result].

Try it yourself

#include <iostream>



double calculateAverageGrade(int grades[], int size) {
    // Write your code here
}

int main() {
    int n;

    std::cin >> n;
    std::cin.ignore();
    int arr[n];

    for (int i = 0; i < n; i++) {
        int val;
        std::cin >> val;
        arr[i] = val;
    }


    double averageGrade = calculateAverageGrade(arr, n);
    std::cout << "Average grade: " << averageGrade << std::endl;
    return 0;
}

All lessons in Fundamentals