in reply to Any improvement over the code possible??
Consider the following:
sub find_above_average { my $mean = &calculate_average(@_); return grep $_ > $mean, @_; }
Remove the say line and print the average outside of the subroutine, and use grep to return only those values greater than the value of $mean.
sub calculate_average { @_ > 0 or die 'No numbers sent for averaging.'; my $sum = 0; foreach (@_) { $sum += $_; } return $sum / @_; }
Check for an empty list at the top to avoid a division by zero error.
|
|---|