User Tools

Site Tools


finding_20the_20partial_20sum_20of_20an_20array

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

finding_20the_20partial_20sum_20of_20an_20array [2018/03/31 13:19]
127.0.0.1 external edit
finding_20the_20partial_20sum_20of_20an_20array [2018/04/17 16:14] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //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:​\\ ​ //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 bb4w>
         DIM array(999)         DIM array(999)
         array() = 1         array() = 1
         PRINT FNsum(array(),​ 500)         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:​\\ ​ 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 bb4w>
         DEF FNsum(a(), N%)         DEF FNsum(a(), N%)
         IF DIM(a())<>​1 ERROR 0, "Array must be one-dimensional"​         IF DIM(a())<>​1 ERROR 0, "Array must be one-dimensional"​
Line 11: Line 14:
         !(!^a()+1) = N%         !(!^a()+1) = N%
         = SUM(a())         = SUM(a())
 +</​code>​
finding_20the_20partial_20sum_20of_20an_20array.txt ยท Last modified: 2018/04/17 16:14 by tbest3112