Averages/Simple moving average: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments.
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
Line 3,125:
=={{header|REXX}}==
The same item list was used as for the ALGOL68 example.
<lang rexx>/*REXX program illustrates simple moving average using a simpleconstructed list. */
parse arg p q n . /*get someoptional arguments (maybe)from the C.L. */
if p=='' then p=3 /*the 1st period (the default is: 3).*/
if q=='' then q=5 /* " 2nd " " " 5" 5).*/
if n=='' then n=10 /*the number of items in the list. */
@.=0 /*define stemmedarray array,with initial initzero 0values*/
end /*m*/ /* [] build 1st show simplehalf movingof avg.list*/
/*──────────────────────────────────────────build 1st half of the list. */
do j=1 for n%2; @.j=j; end /* ··· increasing values.*/
/* [↓] build 2nd half of list*/
/*──────────────────────────────────────────build 2nd half of the list. */
do k=n%2 to 1 by -1; @.j=k; j=j+1; end /* ··· decreasing values.*/
 
/*──────────────────────────────────────────perform a simple moving avg.*/
say ' ' " SMA with " ' SMA with '
say ' number ' " period" p' ' ' period' q
say ' ──────── ' "──────────" '──────────'
 
do m=1 for n
/* [↓] perform a simple moving average*/
say center(@.m,10) left(sma(p,m),11) left(sma(q,m),11)
do m=1 for n
end /*m*/ /* [↑] show simple moving avg.*/
exit say center(@.m, 10) left(sma(p,m), 11) /*stick a fork in itleft(sma(q,m), we're done.*/11)
end /*m*/ /* [↑] show a simple moving average.*/
/*──────────────────────────────────SMA subroutine──────────────────────*/
sma:exit procedure expose @.; parse arg p,j; s=0; i=0 /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
do k=max(1,j-p+1) to j+p for p while k<=j; i=i+1
sma: procedure expose @.; parse arg p,j; s=0; i=0
s=s+@.k
end /*do k*/=max(1,j-p+1) to j+p for p while k<=j; i=i+1
s=s+@.k
end /*k*/
return s/i</lang>
'''output''' &nbsp; using the generated default list of numbers:
<pre>
SMA with SMA with