Sum of Array Elements in C++

In this exercise, you will create a program that calculates the sum of all the elements in an array. Summing the elements of an array is a common task in programming, which is often required in various applications such as statistical analysis, data processing, and number manipulation.

The goal is to iterate through the array, add each element to a total sum, and finally print the result. This is a fundamental operation, which helps to understand array manipulation and iteration in C++.

You will implement a function that loops through the array, adds each value to a running total, and then returns the sum.

Group

Arrays and Vectors in C++

Objective

1. Define an array with several integer values.
2. Create a function that loops through the array to calculate the sum of its elements.
3. Return the total sum from the function.
4. Print the sum of the array elements.
5. Ensure that the program outputs the correct sum of all elements.

Calculate the sum 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 sum of array elements
int sumArray(int arr[], int size) {
    int sum = 0; // Initialize sum to 0

    // Loop through the array to add each element to sum
    for (int i = 0; i < size; i++) {
        sum += arr[i]; // Add the current element to sum
    }

    return sum; // Return the total sum
}

// Function to print the elements of the array
void printArray(int arr[], int size) {
    // Loop through the array and print each element
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " "; // Print each number followed by a space
    }
    cout << endl; // Print a newline after the array
}

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

    // Call the function to calculate the sum
    int totalSum = sumArray(numbers, size);

    // Print the sum of the array elements
    cout << "The sum of the array elements is: " << totalSum << endl;

    return 0; // End of program
}

 Output

The sum of the array elements is: 75

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++.

  • Search for a Number in an Array in C++

    In this exercise, you will create a program that searches for a specific number in an array. Searching through an array is a common task in programming, which is used in various ap...

  • 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, w...

  • 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...