Finding+the+partial+sum+of+an+array

//by Richard Russell, July 2015//

The **SUM** function returns the sum of all the elements in a numeric array, and it is much faster than summing the elements yourself in a loop. However you have no control over how many elements are summed: it is always the entire array. To find the sum of the first **n** elements of an array you can use a user-defined function as follows: code format="bb4w" DIM array(999) array = 1 PRINT FNsum(array, 500) code This program prints the value 500, being the sum of the first 500 elements of the array (0 to 499 inclusive). Here is the **FNsum** function itself: code format="bb4w" DEF FNsum(a, N%) IF DIM(a)<>1 ERROR 0, "Array must be one-dimensional" LOCAL !(!^a+1) !(!^a+1) = N%     = SUM(a) code