Anonymous user
Averages/Simple moving average: Difference between revisions
m
→{{header|REXX}}: changed whitespace, reduce number of lines in the title, added a comment, aligned END statements with their DO statement, used a template for the output section.
(Add Factor example) |
m (→{{header|REXX}}: changed whitespace, reduce number of lines in the title, added a comment, aligned END statements with their DO statement, used a template for the output section.) |
||
Line 3,521:
=={{header|REXX}}==
The same list of numbers was used as in the '''ALGOL68''' example.
The 1<sup>st</sup> and 2<sup>nd</sup> periods (number of values) were parametrized, as well as the total number of values.
<lang rexx>/*REXX program illustrates and displays a simple moving average using a constructed list*/
parse arg p q n . /*obtain optional arguments from the CL*/
if p=='' | p=="," then p=
if q=='' | q=="," then q=
if n=='' | n=="," then n=
@.=
do j=1 for n%2; @.j= j
end /*j*/
do k=n%2 by -1 to 1; @.j= k; j= j+1
end /*k*/
say ' number ' " period" p' ' ' period' q▼
say ' ──────── ' "───────────────────" '───────────────────'
do m=1 for n; say center(@.m, 10) pad left(SMA(p, m), 19) left(SMA(q, m), 19)
end /*m*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
SMA: procedure expose @.; parse arg p,j; i= 0 ; $= 0
do k=max(1, j-p+1) to j+p for p while k<=j; i= i + 1; $= $ + @.k
return $/i /*SMA ≡ simple moving average. */</lang>
<pre>
number SMA with period 3 SMA with period
──────── ─────────────────── ───────────────────
3 2
4 3
5 4
5
2 3
1 2
▲ 1 2 3
</pre>
|