Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Calculate Average of Numbers Using Arrays in C++



An array stores multiple values of the same data type. To find the average, we add all the numbers and then divide the total by how many numbers there are. In this article, we'll show you how to write a C++ program to calculate the average of numbers using an array

For example, if we have the numbers 40, 80, 10, 75, and 25, their total is 230, and the average is 230 / 5 = 46.

Approaches for Calculating Average Using Arrays

In C++, we can calculate the average of numbers in an array in two main ways:

Using Iterative Method

In the iterative approach, we use a loop to go through each number in the array, calculate the sum of all elements, and then divide the sum by the total number of elements to find the average.

Example

Here's a complete C++ program where we calculate the average of the entire array elements using the iterative method (i.e., using a loop).

#include <iostream>
using namespace std;

int main() {
    // Define a static array with values
    float numbers[] = {40, 80, 10, 75, 25};  // Array of numbers
    int n = sizeof(numbers) / sizeof(numbers[0]);  // Calculate number of elements
    float sum = 0.0, average;
    cout<<"Array elements are: ";

    // Loop through the array and calculate the sum
    for (int i = 0; i < n; ++i) {
        sum += numbers[i];  // Add each element to sum
        cout<<numbers[i]<<" ";
    }
    cout<<endl;
    // Calculate the average
    average = sum / n;

    // Display the average
    cout << "Average (Iterative) = " << average << endl;
    return 0;
}

The output of the above program shows the array elements and the average.

Array elements are: 40 80 10 75 25 
Average (Iterative) = 46

Time Complexity: O(n) because we loop through each element once.

Space Complexity: O(n) because we store all the elements in the array.

Using Recursion

In the recursive approach, we define a function that calls itself to calculate the sum of array elements. With each call, the function handles one element and moves to the next, until it reaches the last element. Once the total sum is calculated, we divide it by the number of elements to find the average.

Example

Below is a complete C++ program where we calculate the average of the entire array elements using the recursive method.

#include <iostream>
using namespace std;

// Recursive function to calculate sum
float calculateSum(float arr[], int n) {
    // Base case: If there are no elements, return 0
    if (n == 0)
        return 0;
    // Add the last element of the array to the sum
    return arr[n - 1] + calculateSum(arr, n - 1);
}

int main() {
    // Define a static array with values
    float numbers[] = {40, 80, 10, 75, 25};
    int n = sizeof(numbers) / sizeof(numbers[0]);
    float average;
    float sum = calculateSum(numbers, n);
    // Calculate the average
    average = sum / n;

    // Display the array elements and average
    cout << "Array elements are: ";
    for (int i = 0; i < n; ++i) {
        cout << numbers[i] << " ";
    }
    cout << endl;
    cout << "Average = " << average << endl;

    return 0;
}

Once you run the above code, the output will display the array elements and the average.

Array elements are: 40 80 10 75 25 
Average = 46

Time Complexity: O(n) because we visit each element once using recursion.

Space Complexity: O(n) because each recursive call adds a new stack frame.

Updated on: 2025-05-13T17:10:42+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements