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, &nbsp; 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= 3 3 /*Not specified? Then use the default.*/
if q=='' | q=="," then q= 5 5 /* " " " " " " */
if n=='' | n=="," then n=10 10 /* " " " " " " */
@.=0 0 /*default value, only needed for odd N.*/
do j=1 for n%2; @.j= j; end /*build 1st half of list, increasing #s*/
end /*j*/
do k=n%2 by -1 to 1; @.j=k; j=j+1; end /* " 2nd " " " decreasing " */
 
do k=n%2 by -1 to 1; @.j= k; j= j+1 say '/* " 2nd ' " " SMA with " decreasing " ' SMA with '*/
end /*k*/
say ' number ' " period" p' ' ' period' q
say ' ──────── ' "──────────" '──────────'
 
do m=1 for n; say center(@.m,' 10) number left(SMA(p, m),' 11) left( " SMA(q, m),with 11);period" p' ' end " SMA with period" q
say ' ──────── ' "───────────────────" '───────────────────'
say ' number ' " period" p' ' pad=' ' period' q
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; end
return $ end /i<*k*/lang>
return $/i /*SMA ≡ simple moving average. */</lang>
'''{{out|output''' |text=&nbsp; when using the generated default list ofinput numbers:}}
<pre>
number SMA with period 3 SMA with period SMA with5
──────── ─────────────────── ───────────────────
number period 3 period 5
1 2 1 3 1
──────── ────────── ──────────
12 1.5 1.5
3 2 1.5 1.5 2
4 3 2 2.5
5 4 3 2.5 3
5 4 4.66666667 3.8
54 4.66666667 3 4.82
43 4.66666667 4.2
2 3 4 4 3.28
1 2 3 3.8
1 2 3
</pre>