finding_20the_20partial_20sum_20of_20an_20array
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| finding_20the_20partial_20sum_20of_20an_20array [2018/03/31 13:19] – external edit 127.0.0.1 | finding_20the_20partial_20sum_20of_20an_20array [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| 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(), | PRINT FNsum(array(), | ||
| + | </ | ||
| 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())<> | IF DIM(a())<> | ||
| Line 11: | Line 14: | ||
| !(!^a()+1) = N% | !(!^a()+1) = N% | ||
| = SUM(a()) | = SUM(a()) | ||
| + | </ | ||
finding_20the_20partial_20sum_20of_20an_20array.1522502361.txt.gz · Last modified: 2024/01/05 00:17 (external edit)