Exercise - Writing a Function

About this task

Write a function named AVG that receives a list of numbers separated by blanks and computes their average. The final answer can be a decimal number. To call this function, you would use:
AVG(number1 number2 number3…)

Use the WORDS (see section WORDS) and WORD (see section WORD) built-in functions.

ANSWER
Figure 1. Possible Solution
/******************************* REXX ********************************/
/*  This function receives a list of numbers, adds them, computes    */
/*  their average, and returns the average to the calling program.   */
/*********************************************************************/

 ARG numlist            /* receive the numbers in a single variable  */

 sum = 0                                  /* initialize sum to zero  */

 DO n = 1 TO WORDS(numlist)    /* Repeat for as many times as there  */
                               /* are numbers                        */

    number = WORD(numlist,n)              /* Word #n goes to number  */
    sum = sum + number                    /* Sum increases by number */
 END

 average = sum / WORDS(numlist)           /* Compute the average     */

 RETURN average

Task Task

Feedback


Timestamp icon Last updated: Tuesday, 7 January 2014


http://pic.dhe.ibm.com/infocenter/cicsts/v5r1/topic/com.ibm.cics.rexx.doc//dfhrx/dfhrx00058.html