summing_20part_20of_20an_20array
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
summing_20part_20of_20an_20array [2018/03/31 13:19] – external edit 127.0.0.1 | summing_20part_20of_20an_20array [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
//by Richard Russell, October 2013 (original concept by Michael Hutton)//\\ \\ The [[http:// | //by Richard Russell, October 2013 (original concept by Michael Hutton)//\\ \\ The [[http:// | ||
+ | <code bb4w> | ||
total = 0 | total = 0 | ||
FOR index% = first% TO last% | FOR index% = first% TO last% | ||
total += array(index%) | total += array(index%) | ||
NEXT | NEXT | ||
+ | </ | ||
where **first%** and **last%** define the range of indices (inclusive) over which the sum should be calculated.\\ \\ However this approach is relatively slow, especially if the number of elements being summed is large, and it would be desirable to have a faster method. Fortunately there is a way, although the code isn't very nice: | where **first%** and **last%** define the range of indices (inclusive) over which the sum should be calculated.\\ \\ However this approach is relatively slow, especially if the number of elements being summed is large, and it would be desirable to have a faster method. Fortunately there is a way, although the code isn't very nice: | ||
+ | <code bb4w> | ||
DEF FN_SumPartialArray(d(), | DEF FN_SumPartialArray(d(), | ||
- | LOCAL I% | + | LOCAL i%% |
- | | + | |
- | LOCAL ?I%, I%!1, a() | + | LOCAL ?i%%, i%%!1, a() |
- | ?I% = 1 : I%!1 = last%-first%+1 | + | ?i%% = 1 : i%%!1 = last%-first%+1 |
- | | + | |
= SUM(a()) | = SUM(a()) | ||
+ | </ | ||
The array **a()** must be the same type (i.e. have the same suffix character, if any) as the array being summed.\\ \\ Note that during execution of the function the array contents are temporarily changed, so don't use this method if your program has a timer interrupt in which the array is accessed.\\ \\ If errors are trapped (using ON ERROR) and you need your program to resume execution after an error (e.g. pressing the **Esc**ape key), add a statement to ensure the array contents are restored: | The array **a()** must be the same type (i.e. have the same suffix character, if any) as the array being summed.\\ \\ Note that during execution of the function the array contents are temporarily changed, so don't use this method if your program has a timer interrupt in which the array is accessed.\\ \\ If errors are trapped (using ON ERROR) and you need your program to resume execution after an error (e.g. pressing the **Esc**ape key), add a statement to ensure the array contents are restored: | ||
+ | <code bb4w> | ||
DEF FN_SumPartialArray(d(), | DEF FN_SumPartialArray(d(), | ||
- | LOCAL I% | + | LOCAL i%% |
- | | + | |
- | LOCAL ?I%, I%!1, a() | + | LOCAL ?i%%, i%%!1, a() |
ON ERROR LOCAL RESTORE LOCAL : ERROR ERR, REPORT$ | ON ERROR LOCAL RESTORE LOCAL : ERROR ERR, REPORT$ | ||
- | ?I% = 1 : I%!1 = last%-first%+1 | + | ?i%% = 1 : i%%!1 = last%-first%+1 |
- | | + | |
= SUM(a()) | = SUM(a()) | ||
+ | </ |
summing_20part_20of_20an_20array.1522502385.txt.gz · Last modified: 2024/01/05 00:16 (external edit)