《Perl语言入门》第四版习题(4)
2009-10-13 14:39
204 查看
写一个名为&total 的子程序,返回一列数字的和。提示:子程序不应当有任何的I/O 操作;它处理调用的参数,返回处理后的值给调用者。结合下面的程序来练习,它检测此子程序是否正常工作。第一组数组之和我25。
my @fred = qw{ 1 3 5 7 9 };
my $fred_total = &total(@fred);
print "The total of /@fred is $fred_total./n";
print "Enter some numbers on separate lines: ";
my $user_total = &total(<STDIN>);
print "The total of those numbers is $user_total./n";
利用上题的子程序,写一个程序计算从1 到1000 的数字的和。
额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。利用下面的程序进行测试:
my @fred = &above_average(1..10);
print "/@fred is @fred/n";
print "(Should be 6 7 8 9 10)/n";
my @barney = &above_average(100, 1..10);
print "/@barney is @barney/n";
print "(Should be just 100)/n";
1:
#!/usr/bin/perl -w
#<Leanning Perl 4th Edition> Exercise 4-1
use strict;
sub total {
my $sum=shift @_;
foreach (@_) {
$sum=$sum+$_;
}
$sum;
}
my @fred=qw {1 3 5 7 9};
my $fred_total=&total(@fred);
print "The Total of /@fred is $fred_total./n";
print "Enter some numbers on separate lines: ";
my $user_total = &total(<STDIN>);
print "The Total of those numbers is $user_total./n";
2:
#!/usr/bin/perl -w
#<Leanning Perl 4th Edition> Exercise 4-2
use strict;
sub total {
my $sum=shift @_;
foreach (@_) {
$sum=$sum+$_;
}
$sum;
}
my @array=1 .. 1000;
my $array_total=&total(@array);
print "The sum of 1 to 1000 is $array_total./n";
3:
#!/usr/bin/perl -w
#<Leanning Perl 4th Edition> Exercise 4-3
use strict;
sub average {
my $number=@_;
my $sum=shift @_;
foreach (@_) {
$sum=$sum+$_;
}
my $array_average=$sum/$number;
}
sub above_average {
my @above;
foreach (@_) {
if ($_>&average(@_)) {
push @above,$_;
}
}
@above;
}
my @above_fred = &above_average(1 .. 10);
print "The above average of /@fred is @above_fred/n";
my @above_barney = &above_average(100, 1 .. 10);
print "The above average of /@barney is @above_barney/n";
my @fred = qw{ 1 3 5 7 9 };
my $fred_total = &total(@fred);
print "The total of /@fred is $fred_total./n";
print "Enter some numbers on separate lines: ";
my $user_total = &total(<STDIN>);
print "The total of those numbers is $user_total./n";
利用上题的子程序,写一个程序计算从1 到1000 的数字的和。
额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。利用下面的程序进行测试:
my @fred = &above_average(1..10);
print "/@fred is @fred/n";
print "(Should be 6 7 8 9 10)/n";
my @barney = &above_average(100, 1..10);
print "/@barney is @barney/n";
print "(Should be just 100)/n";
1:
#!/usr/bin/perl -w
#<Leanning Perl 4th Edition> Exercise 4-1
use strict;
sub total {
my $sum=shift @_;
foreach (@_) {
$sum=$sum+$_;
}
$sum;
}
my @fred=qw {1 3 5 7 9};
my $fred_total=&total(@fred);
print "The Total of /@fred is $fred_total./n";
print "Enter some numbers on separate lines: ";
my $user_total = &total(<STDIN>);
print "The Total of those numbers is $user_total./n";
2:
#!/usr/bin/perl -w
#<Leanning Perl 4th Edition> Exercise 4-2
use strict;
sub total {
my $sum=shift @_;
foreach (@_) {
$sum=$sum+$_;
}
$sum;
}
my @array=1 .. 1000;
my $array_total=&total(@array);
print "The sum of 1 to 1000 is $array_total./n";
3:
#!/usr/bin/perl -w
#<Leanning Perl 4th Edition> Exercise 4-3
use strict;
sub average {
my $number=@_;
my $sum=shift @_;
foreach (@_) {
$sum=$sum+$_;
}
my $array_average=$sum/$number;
}
sub above_average {
my @above;
foreach (@_) {
if ($_>&average(@_)) {
push @above,$_;
}
}
@above;
}
my @above_fred = &above_average(1 .. 10);
print "The above average of /@fred is @above_fred/n";
my @above_barney = &above_average(100, 1 .. 10);
print "The above average of /@barney is @above_barney/n";
相关文章推荐
- 《Perl语言入门》第四版习题(2)
- 《Perl语言入门》第四版习题(10)
- 《Perl语言入门》第四版习题(9)
- 《Perl语言入门》第四版习题(6)
- 《Perl语言入门》第四版习题(11)
- 《Perl语言入门》第四版习题(14)
- 《Perl语言入门》第四版习题(7)
- 《Perl语言入门》第四版习题(12)
- 《Perl语言入门》第四版习题(13)
- 《Perl语言入门》第四版习题(8)
- 《Perl语言入门》第四版习题(5)
- 《Perl语言入门》第四版习题(3)
- perl语言入门第四版第5章习题
- C++ primer 第四版 习题4.34.4.35
- CPP第四版第五章:位操作符、sizeof及部分编程习题
- 《解析几何》吕林根,徐子道第四版 习题 1.4.7,1.4.8,1.4.9
- 《疯狂Java讲义》(第四版)学习笔记1-----第四章习题解答
- 《c++ Primer(第四版)》习题9.18
- 《c++ Primer(第四版)》习题 9.36
- 《c++primer(第四版)》习题8.6