How to resolve the algorithm Averages/Simple moving average step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Simple moving average step by step in the PL/I programming language

Table of Contents

Problem Statement

Computing the simple moving average of a series of numbers. Create a stateful function/class/instance that takes a period and returns a routine that takes a number as argument and returns a simple moving average of its arguments so far. A simple moving average is a method for computing an average of a stream of numbers by only averaging the last   P   numbers from the stream,   where   P   is known as the period. It can be implemented by calling an initialing routine with   P   as its argument,   I(P),   which should then return a routine that when called with individual, successive members of a stream of numbers, computes the mean of (up to), the last   P   of them, lets call this   SMA(). The word   stateful   in the task description refers to the need for   SMA()   to remember certain information between calls to it:

Stateful   also means that successive calls to   I(),   the initializer,   should return separate routines that do   not   share saved state so they could be used on two independent streams of data. Pseudo-code for an implementation of   SMA   is:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Simple moving average step by step in the PL/I programming language

Source code in the pl/i programming language

SMA: procedure (N) returns (float byaddr);
   declare N fixed;
   declare A(*) fixed controlled,
           (p, q) fixed binary static initial (0);

   if allocation(A) = 0 then signal error;

   p = p + 1; if q < 20 then q = q + 1;
   if p > hbound(A, 1) then p = 1;
   A(p) = N;
   return (sum(float(A))/q);

I: ENTRY (Period);
   declare Period fixed binary;

   if allocation(A) > 0 then FREE A;
   allocate A(Period);
   A = 0;
   p = 0;
end SMA;

*process source attributes xref;
 mat: Proc Options(main);
 Dcl a(10) Dec Fixed(8,6);
 Dcl s     Dec Fixed(10,8);
 Dcl n Bin Fixed(31) init(hbound(a)); /* number of items in the list. */
 Dcl p Bin Fixed(31) init(3);         /* the 1st period               */
 Dcl q Bin Fixed(31) init(5);         /* the 2nd period               */
 Dcl m Bin Fixed(31);
 Call i(a);

 Put Edit('            SMA with   SMA with',
          '  number    period 3   period 5',
          ' --------  ---------- ----------')
         (Skip,a);
 Do m=1 To n;
   Put Edit(m,sma(p,m),sma(q,m))(Skip,f(5),2(f(13,6)));
   End;

 i: Proc(a);
 Dcl a(*) Dec Fixed(8,6);
 Dcl (j,m) Bin Fixed(31);
 Do j=1 To hbound(a)/2;
   a(j)=j;                            /* ··· increasing values.       */
   End;
 Do k=hbound(a)/2 To 1 By -1;
   a(j)=k;                            /* ··· decreasing values.       */
   j+=1;
   End;
 End;

 sma: Proc(p,j) Returns(Dec Fixed(8,6));
 Dcl s Dec fixed(8,6) Init(0);
 Dcl i Bin Fixed(31) Init(0);
 Dcl j Bin Fixed(31) Init((hbound(a)+1));
 Dcl (p,i,k,ka,kb) Bin Fixed(31);
   ka=max(1,j-p+1);
   kb=j+p;
   Do k=ka To kb While(k<=j);
     i+=1;
     s+=a(k)
     End;
   s=s/i+0.5e-6;
   Return(s);
 End;
 End;

  

You may also check:How to resolve the algorithm 100 doors step by step in the Golfscript programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Swahili programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the tbas programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the PureBasic programming language