RosettaCodeData/Task/Averages-Simple-moving-average/BBC-BASIC/averages-simple-moving-aver...

19 lines
730 B
Plaintext

MAXPERIOD = 10
FOR n = 1 TO 5
PRINT "Number = ";n TAB(12) " SMA3 = ";FNsma(n,3) TAB(30) " SMA5 = ";FNsma(n,5)
NEXT
FOR n = 5 TO 1 STEP -1
PRINT "Number = ";n TAB(12) " SMA3 = ";FNsma(n,3) TAB(30) " SMA5 = ";FNsma(n,5)
NEXT
END
DEF FNsma(number, period%)
PRIVATE nums(), accum(), index%(), window%()
DIM nums(MAXPERIOD,MAXPERIOD), accum(MAXPERIOD)
DIM index%(MAXPERIOD), window%(MAXPERIOD)
accum(period%) += number - nums(period%,index%(period%))
nums(period%,index%(period%)) = number
index%(period%) = (index%(period%) + 1) MOD period%
IF window%(period%)<period% window%(period%) += 1
= accum(period%) / window%(period%)