################################################### # # # 2/24/21 # # Program takes finds the average in an # # array and lists out the numbers that were # # above the average found. # # # ################################################### ### Limitations - added 3/1/2021 ### This code will throw a fatal "Illegal division by zero" ### error if @numbers doesn't contain at least one element. my @numbers = qw(4 12 18 21 35); my $average = find_average(@numbers); print "The average found for this list is: $average \n" ; my @above_avg = above_average($average, @numbers); print "The numbers that were found above average are: @above_avg \n +"; sub find_average { my @nums = @_; my $sum; foreach my $num (@nums) { $sum += $num; } my $avg = $sum / @nums; return $avg; } sub above_average { my ($average_num, @nums) = @_; my @final_list; foreach my $num (@nums) { if ($num > $average_num) { push @final_list, $num } } return @final_list; } __END__ PRINTS: The average found for this list is: 18 The numbers that were found above average are: 21 35
In reply to Re: Passing Variables
by Marshall
in thread Passing Variables
by catfish1116
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |