I need some help with a problem.
Problem: Using a for loop, compute the average of the first 20 odd numbers. Print only the average.
I have figured out how to count the first 20 odd numbers in the for loop but I can't figure out how to get the average where it only prints the average. Right now it is printing the total of all the odd numbers of 400.
Any help would be greatly appreciative. Also, if you could explain in simple terms as I am a beginner. Thanks!
{
$avg=0
for ($i =1 ; $i –le 39 ; $i =2) {
$avg = $avg $i
}
echo $avg
}
CodePudding user response:
To complement henry groves' helpful answer:
The Measure-Object
cmdlet has an -Average
switch that automatically calculates the average of all numbers it receives via the pipeline and reports it in the .Average
property of the [Microsoft.PowerShell.Commands.GenericMeasureInfo]
instance it outputs:
(
& { for ($i =1 ; $i –le 39 ; $i =2) { $i } } | Measure-Object -Average
).Average
This outputs 20
. (To assign the result to variable $avg
, simply prepend $avg =
to the entire command.)
Note that for
, as a language statement, cannot directly participate in a pipeline, hence its invocation via a script block ({ ... }
) called with &
.
As an aside:
For looping over a range of integers with an increment of 1
(or -1
), PowerShell offers ..
, the convenient range operator; e.g. 1..3
is effectively the same as for ($i=1; $i -le 3; $i) { $i }
; 3..1
works analogously.
It would be great if ..
also supported specifiable increments (stepping), such as 2
in your case, which isn't supported as of PowerShell 7.2.x.
GitHub issue #7928 proposes adding such support in the future, which could take the following form.
# WISHFUL THINKING as of PowerShell 7.2.x
# If implemented, would be the equivalent of:
# for ($i =1 ; $i –le 39 ; $i = 2) { $i }
1..39..2
CodePudding user response:
The average (or arithmetic mean) is defined as the sum of the data divided by the number of items in the data set.
therefore you must divide the final sum of odd numbers by two.
$avg=0
for ($i =1 ; $i –lt 20 ; $i =2) {
$avg = $avg $i
}
$avg = $avg / 20
echo $avg