C++ Program to Calculate Average of Numbers Using Arrays

Updated on December 13, 2024
Calculate average of numbers using arrays header image

Introduction

Calculating averages is a fundamental operation in many programming and data analysis tasks. In C++, you might often find situations where you need to compute the average of a series of numbers stored in an array. This operation exemplifies how you can manipulate arrays and perform arithmetic computations efficiently.

In this article, you will learn how to calculate the average of numbers using arrays in C++. Through detailed examples, discover the application of loops for iteration and the use of basic arithmetic operations to solve this common programming task.

Calculating Average Using a Static Array

Define and Initialize the Array

  1. Start by declaring and initializing an array with predefined values.

  2. Calculate the total number of elements in the array.

  3. Initialize a variable to store the sum of array elements.

    cpp
    #include <iostream>
    using namespace std;
    
    int main() {
        int numbers[] = {8, 4, 6, 9, 5};
        int length = sizeof(numbers) / sizeof(numbers[0]);
        int sum = 0;
    

    The array numbers is declared with five integer elements. The length variable holds the count of these elements, crucial for looping through the array.

Sum the Elements of the Array

  1. Loop through each element in the array using a for loop.

  2. Add each element to the sum variable.

    cpp
    for(int i = 0; i < length; i++) {
        sum += numbers[i];
    }
    

    This loop iterates over each element in the array, accumulating the values into sum.

Calculate and Display the Average

  1. Declare a variable to store the average.

  2. Calculate the average by dividing the sum by the number of elements.

  3. Output the average.

    cpp
    double average = double(sum) / length;
    cout << "Average is: " << average << endl;
    return 0;
    }
    

    The average is calculated by converting the sum to a double for precision during division. The average is then printed to the console.

Calculating Average Using a Dynamic Array

Dynamically Allocate Array and Input Values

  1. Prompt the user for the number of elements.

  2. Dynamically allocate an array of that size.

  3. Loop through the array to input values from the user.

    cpp
    #include <iostream>
    using namespace std;
    
    int main() {
        int n;
        cout << "Enter number of elements: ";
        cin >> n;
        int *numbers = new int[n];
        int sum = 0;
    
        for(int i = 0; i < n; i++) {
            cout << "Enter number " << i + 1 << ": ";
            cin >> numbers[i];
        }
    

    This code segment dynamically allocates an array based on user input for flexible array sizes.

Calculate Sum and Average

  1. Sum the elements input by the user.

  2. Calculate the average.

  3. Display the average and release the allocated memory.

    cpp
    for(int i = 0; i < n; i++) {
        sum += numbers[i];
    }
    double average = double(sum) / n;
    cout << "Average is: " << average << endl;
    delete[] numbers;
    return 0;
    }
    

    After summing the elements, the average is calculated similar to the static array example. Memory is freed using delete[] to avoid memory leaks.

Conclusion

Calculating the average of numbers using arrays in C++ is an excellent way to familiarize yourself with array operations, iteration structures, and basic memory management with dynamic arrays. By mastering these examples, you enhance your ability to handle fundamental data manipulation tasks in C++. Whether using static or dynamic arrays, the principles of calculating averages remain consistent and are essential for many future programming scenarios.