Add all the element values

The approach is straightforward.  An accumulator variable of type double named sum is declared and initialized to a value of zero in Listing 5.

Then a while loop is used to iterate on the array, fetching the value of one element during each iteration of the loop, and adding that value to the cumulative sum.

After all of the element  values have been fetched and added to the accumulator, the loop terminates.