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

Published on 12 May 2024 09:40 PM

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

Source code in the powershell programming language

function mean ($x) {
    if ($x.Count -eq 0) {
        return 0
    } else {
        $sum = 0
        foreach ($i in $x) {
            $sum += $i
        }
        return $sum / $x.Count
    }
}

function mean ($x) {
    if ($x.Count -eq 0) {
        return 0
    } else {
        return ($x | Measure-Object -Average).Average
    }
}

  

You may also check:How to resolve the algorithm Brilliant numbers step by step in the RPL programming language
You may also check:How to resolve the algorithm Fairshare between two and more step by step in the Racket programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the sed programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Find Chess960 starting position identifier step by step in the 11l programming language