-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlearningperl4qna.pl~
78 lines (60 loc) · 1.31 KB
/
learningperl4qna.pl~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
use strict;
use warnings;
# Q1 Chapter 4, create subroutine total.
sub total {
my $result = 0;
foreach (@_) {
$result += $_;
}
$result;
}
# With nos. given in the code.
my @fred = qw/1 3 5 7 9/;
my $fred_total = &total(@fred);
print "The total of \@fred is $fred_total\n";
# With nos. to be taken from STDIN.
# print "Enter some nos.:";
# my @user_no = <STDIN>;
# my $user_total = &total(@user_no);
# print "The total of \@user_no is $user_total\n";
# Q2 Chapter 4. Sum of the nos from 1 to 1000.
my @sum_tho = (1 .. 1000);
my $total_tho = &total(@sum_tho);
print "Total of \@sum_tho is $total_tho\n";
# Q3. nos. above the average no.
sub average {
my $sum = 0;
my $noofele = @_;
foreach ( @_ ) {
$sum += $_;
}
$sum / $noofele;
}
sub max {
my ($max_so_far) = shift @_;
foreach (@_) {
if ($_ > $max_so_far) {
$max_so_far = $_;
}
}
$max_so_far;
}
sub above_average {
# my $result = &average(@_);
my $noofele = @_;
my $mean = &average(@_);
if ( $mean < $noofele ) {
++$mean..$noofele;
} else {
&max(@_);
}
}
my @fred1 = &above_average(1..10);
print "\@fred1 is @fred1\n";
my @barney = above_average(100,1..10);
print "\@barney is @barney\n";
#sub max {
#my $result = &average(1..10);
# my $tot = &total(1..10);
#print "Average for \$tot is $result\n";