Averages/Simple moving average: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed the wording in the REXX section header.)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 3,328: Line 3,328:


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.
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 a simple moving average using a constructed list. */
<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*/
parse arg p q n . /*obtain optional arguments from the CL*/
if p=='' | p=="," then p= 3 /*the 1st period (the default is: 3).*/
if p=='' | p=="," then p= 3 /*Not specified? Then use the default.*/
if q=='' | q=="," then q= 5 /* " 2nd " " " " 5).*/
if q=='' | q=="," then q= 5 /* " " " " " " */
if n=='' | n=="," then n=10 /*the number of items in the list. */
if n=='' | n=="," then n=10 /* " " " " " " */
@.=0 /*define array with initial zero values*/
@.=0 /*default value, only needed for odd N.*/
do j=1 for n%2; @.j=j; end /*build 1st half of list, increasing #s*/
do j=1 for n%2; @.j=j; end /*build 1st half of list, increasing #s*/
do k=n%2 to 1 by -1; @.j=k; j=j+1; end /* " 2nd " " " decreasing " */
do k=n%2 by -1 to 1; @.j=k; j=j+1; end /* " 2nd " " " decreasing " */


say ' ' " SMA with " ' SMA with '
say ' ' " SMA with " ' SMA with '
say ' number ' " period" p' ' ' period' q
say ' number ' " period" p' ' ' period' q
say ' ──────── ' "──────────" '──────────'
say ' ──────── ' "──────────" '──────────'
do m=1 for n
do m=1 for n /* [↓] perform a simple moving average*/
say center(@.m, 10) left(SMA(p, m), 11) left(SMA(q, m), 11)
say center(@.m, 10) left(SMA(p, m), 11) left(SMA(q, m), 11)
end /*m*/
end /*m*/ /* [↑] show a simple moving average.*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
SMA: procedure expose @.; parse arg p,j; $=0; i=0
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
do k=max(1, j-p+1) to j+p for p while k<=j; i=i+1; $=$+@.k
$=$ + @.k
end /*k*/
return $/i</lang>
end /*k*/
return $/i</lang>
'''output''' &nbsp; using the generated default list of numbers:
'''output''' &nbsp; using the generated default list of numbers:
<pre>
<pre>