Calculate the Average of Array Elements in C++

In this exercise, you will create a function that calculates the average of the elements in an array. This is a common task when working with arrays, especially in data analysis, where calculating averages is often required.

The goal is to sum all the elements in the array, divide the sum by the total number of elements, and return the result as the average. This exercise will help you practice working with arrays and basic mathematical operations in C++.

Group

Arrays and Vectors in C++

Objective

1. Define an array with several integer values.
2. Create a function that takes the array and its size as input.
3. Inside the function, sum all the elements of the array.
4. Divide the sum by the size of the array to calculate the average.
5. Return the calculated average.
6. Test the program with different arrays.

Implement a function that calculates the average of the elements of an array.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include iostream for input and output
using namespace std; // Use the standard namespace

// Function to calculate the average of the elements in the array
double calculateAverage(int arr[], int size) {
    double sum = 0; // Initialize a variable to store the sum of the elements
    
    // Loop through the array to sum all the elements
    for (int i = 0; i < size; i++) {
        sum += arr[i]; // Add each element to the sum
    }
    
    return sum / size; // Return the average (sum divided by the number of elements)
}

int main() {
    // Define an array of numbers
    int numbers[] = {10, 20, 30, 40, 50};
    int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the size of the array

    // Call the function to calculate the average
    double average = calculateAverage(numbers, size);

    // Print the result
    cout << "The average of the array elements is: " << average << endl;

    return 0; // End of program
}

 Output

The average of the array elements is: 30

Share this C++ Exercise


More C++ Programming Exercises of Arrays and Vectors in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Remove a Specific Element from an Array in C++

    In this exercise, you will develop a program that removes a specific element from an array. Removing elements from an array is a common task, especially when working with data that...

  • Find the Largest Number in an Array in C++

    In this exercise, you will implement a function that finds the largest number in an array. This task is useful when working with datasets where you need to extract the maximum valu...

  • Reverse an Array in C++

    In this exercise, you will implement a program that reverses an array. This task is useful in many scenarios such as reversing a list of elements, manipulating data structures, and...

  • Sum Two Arrays Element by Element in C++

    In this exercise, you will implement a function that sums two arrays element by element. This means that for two given arrays of the same size, the function will return a new array...

  • Remove Duplicates from an Array in C++

    In this exercise, you will create a program that removes duplicates from an array of numbers. The function will iterate over the array and remove any duplicate values, leaving only...

  • Check if an Array is Symmetric in C++

    In this exercise, you will create a function to determine if an array is symmetric. An array is considered symmetric if it reads the same forward and backward, meaning the first el...