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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Arithmetic mean step by step in the Nim 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 Nim programming language

Source code in the nim programming language

import strutils

proc mean(xs: openArray[float]): float =
  for x in xs:
    result += x
  result = result / float(xs.len)

var v = @[1.0, 2.0, 2.718, 3.0, 3.142]
for i in 0..5:
  echo "mean of first ", v.len, " = ", formatFloat(mean(v), precision = 0)
  if v.len > 0: v.setLen(v.high)

  

You may also check:How to resolve the algorithm Department numbers step by step in the RPL programming language
You may also check:How to resolve the algorithm Create a file step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Associative array/Iteration step by step in the Lang5 programming language
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Gambas programming language