C++ Code Example: sum up all numbers from an array

This code calculates the sum of all elements in the integer array numbers.

The array numbers is initialized with 9 integer values and its length is calculated using the standard library function end() minus begin().

In the for loop, the variable sum is incremented by the value of each element in the array numbers.

Finally, the sum of all elements in the array is output to the console.

#include <iostream>
using namespace std;

int main() {
    int numbers[] = {33, 431, 41, 76, 83, 99, 46, 9, 22};
    int sum = 0;
    int arrayLength = end(numbers) - begin(numbers);

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

    cout << "Sum of all elements in the array is: " << sum;

    return 0;
}
Output
Sum of all elements in the array is: 840