Averages/Arithmetic mean: Difference between revisions
Content added Content deleted
m (moved Mean to Average/Arithmetic mean: collecting all averages under a common prefix) |
(PowerShell) |
||
Line 642: | Line 642: | ||
enddefine; |
enddefine; |
||
</pre> |
</pre> |
||
=={{header|PowerShell}}== |
|||
The hard way by calculating a sum and dividing: |
|||
<lang powershell>function mean ($x) { |
|||
if ($x.Count -eq 0) { |
|||
return 0 |
|||
} else { |
|||
$sum = 0 |
|||
foreach ($i in $x) { |
|||
$sum += $i |
|||
} |
|||
return $sum / $x.Count |
|||
} |
|||
}</lang> |
|||
or, shorter, by using the <code>Measure-Object</code> cmdlet which already knows how to compute an average: |
|||
<lang powershell>function mean ($x) { |
|||
if ($x.Count -eq 0) { |
|||
return 0 |
|||
} else { |
|||
return ($x | Measure-Object -Average).Average |
|||
} |
|||
}</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |