How to resolve the algorithm Averages/Arithmetic mean step by step in the ooRexx programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Arithmetic mean step by step in the ooRexx programming language

Table of Contents

Problem Statement

Write a program to find the mean (arithmetic average) of a numeric vector. In case of a zero-length input, since the mean of an empty set of numbers is ill-defined, the program may choose to behave in any way it deems appropriate, though if the programming language has an established convention for conveying math errors or undefined values, it's preferable to follow it.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Arithmetic mean step by step in the ooRexx programming language

Source code in the oorexx programming language

call testAverage .array~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
call testAverage .array~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, .11)
call testAverage .array~of(10, 20, 30, 40, 50, -100, 4.7, -11e2)
call testAverage .array~new

::routine testAverage
  use arg numbers
  say "numbers =" numbers~toString("l", ", ")
  say "average =" average(numbers)
  say

::routine average
  use arg numbers
  -- return zero for an empty list
  if numbers~isempty then return 0

  sum = 0
  do number over numbers
      sum += number
  end
  return sum/numbers~items

  

You may also check:How to resolve the algorithm Input loop step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Racket programming language
You may also check:How to resolve the algorithm Digital root step by step in the Component Pascal programming language
You may also check:How to resolve the algorithm Empty string step by step in the Zoomscript programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Pascal programming language