Jump to content

Averages/Simple moving average: Difference between revisions

→‎{{header|REXX}}: changed some comments, added whitespace, changed indentations, used better DO loop separators. .
No edit summary
(→‎{{header|REXX}}: changed some comments, added whitespace, changed indentations, used better DO loop separators. .)
Line 2,780:
=={{header|REXX}}==
The same item list was used as for the ALGOL68 example.
<lang rexx>/*REXX program isillustrates illustrate simple moving average using a simple list. */
parse arg p q n . /*get some arguments (maybe). */
if p=='' then p=3 /*the 1st period (default: 3).*/
if q=='' then q=5 /* " 2nd " " 5 */
if n=='' then n=10 /*number of items in the list.*/
@.=0 do i=1 for n /*showdefine anstemmed indentedarray, item list.init 0*/
a.=0
/*──────────────────────────────────────────build 1st half of the list. */
do j=1 for n%2 /*build beginning of the list,*/
do a.j=j 1 for n%2 /* ...··· increasing values. */
end /*@.j=j*/
end /*kj*/
 
/*──────────────────────────────────────────build 2nd half of the list. */
do k=n%2 to 1 by -1 /* ... decreasing values. */
do k=n%2 to 1 by -1 a /* ··· decreasing values.j=k */
@.j=j+1k
j=j+1
end /*k*/
end end /*mk*/
 
/*──────────────────────────────────────────show an indented item list. */
do i=1 for n /*show an indented item list. */
do i=1 for n
say left('',60) 'item' right(i,3)'='right(a.i,3)
say left('',60) 'item' end /*right(i,3)'='right(@.i*/,3)
end /*i*/
do m=1 for n /*OK the, let's start the SMA.*/
/*──────────────────────────────────────────perform a simple moving avg.*/
smaP=sma(p,m) /*simple moving average for P.*/
smaQ=sma(q,m) do m=1 for n /* " " " /*OK then, let's "start the Q.SMA*/
do jsmaP=1sma(p,m) for n%2 /*buildsimple beginningmoving ofaverage thefor list,P.*/
 
smaQ=sma(q,m) /* " " /*show 2 nicely" " formated SMAsQ.*/
say 'm='right(m,3), /*show where we're2 atnicely informatted list.SMAs*/
say " sma("p')m='leftright(sma(p,m),113), /*show nicelywhere alignedwe're smaat Pin list. */
" sma("qp')='left(sma(qp,m),11) , /*show nicely "aligned sma " " " QP. */
" sma("q')='left(sma(q,m),11) /* " " " " Q. */
end /*m*/
end /*m*/
exit
exit do m=1 for n /*OKstick the,a let'sfork startin theit, we're SMAdone.*/
/*────────────────────────────────────────SMA subroutine────────────────*/
/*──────────────────────────────────SMA subroutine──────────────────────*/
sma: procedure expose A.; arg p,j; s=0; i=0
sma: procedure expose @.; parse arg p,j; do k=max(1,j-p+1) to j+p fors=0; p while k<i=j0
do k=max(1,j-p+1) to j+p for p while k<=j; i=i+1
s=s+a@.k
end end /*k*/
return s/i</lang>
</lang>
{{out}} using the defaults
<pre style="height:30ex">
Cookies help us deliver our services. By using our services, you agree to our use of cookies.