Task[edit]
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.

See also
Tasks for calculating statistical measures



in one go
moving (sliding window)
moving (cumulative)

Mean
Arithmetic

Statistics/Basic
Averages/Arithmetic mean
Averages/Pythagorean means


Averages/Simple moving average




Geometric

Averages/Pythagorean means




Harmonic

Averages/Pythagorean means




Quadratic

Averages/Root mean square




Circular

Averages/Mean angle
Averages/Mean time of day




Median

Averages/Median




Mode

Averages/Mode




Standard deviation

Statistics/Basic



Cumulative standard deviation






/* NetRexx */
options replace format comments java crossref symbols nobinary
 
launchSample()
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method arithmeticMean(vv = Vector) public static signals DivideException returns Rexx
  sum = 0
  n_ = Rexx
  loop n_ over vv
    sum = sum + n_
    end n_
  mean = sum / vv.size()
 
  return mean
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method launchSample() public static
  TRUE_  = 1 == 1
  FALSE_ = \TRUE_
  tracing = FALSE_
  vectors = getSampleData()
  loop v_ = 0 to vectors.length - 1
    say 'Average of:' vectors[v_].toString()
    do
      say '          =' arithmeticMean(vectors[v_])
    catch dex = DivideException
      say 'Caught "Divide By Zero"; bypassing...'
      if tracing then dex.printStackTrace()
    catch xex = RuntimeException
      say 'Caught unspecified run-time exception; bypassing...'
      if tracing then xex.printStackTrace()
    end
    say
    end v_
  return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getSampleData() private static returns Vector[]
  seed = 1066
  rng = Random(seed)
  vectors =[ -
    Vector(Arrays.asList([Rexx 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])), -
    Vector(), -
    Vector(Arrays.asList([Rexx rng.nextInt(seed), rng.nextInt(seed), rng.nextInt(seed), rng.nextInt(seed), rng.nextInt(seed), rng.nextInt(seed)])), -
    Vector(Arrays.asList([Rexx rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble()])), -
    Vector(Arrays.asList([Rexx '1.0', '2.0', 3.0])), -
    Vector(Arrays.asList([Rexx '1.0', 'not a number', 3.0])) -
    ]
  return vectors
 