Compute the sum of the elements of the array. - C++ Data Type

C++ examples for Data Type:Array

Description

Compute the sum of the elements of the array.

Demo Code

#include <iostream> 
using namespace std; 

int main() //from  www  .  j a va 2s. co m
{ 
    const int arraySize = 10; // constant variable indicating size of array 
    int a[ arraySize ] = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 }; 
    int total = 0; 

    // sum contents of array a 
    for ( int i = 0; i < arraySize; ++i ) 
        total += a[ i ]; 

    cout << "Total of array elements: " << total << endl; 
}

Result


Related Tutorials