in reply to Subroutine help

I am surprised that your teacher didn't give you enough hints to do this. I am also surprised that your teacher threw in this "red herring" of 6. The subs handle any number of numbers.

View this as a "homework gift". You will need to work harder for the next one.

#!/usr/bin/perl -w use strict; my @test = (1,2,3,4,5,6,7,8,9); print sum(@test),"\n"; print max(@test),"\n"; sub sum { my @nums = @_; my $sum =0; foreach my $num (@nums) { $sum+= $num; } return ($sum); } sub max { my @nums = @_; my $max = -9999999999; foreach my $num (@nums) { if ($num>$max) { $max = $num; } } return ($max); } __END__ PRINTS: 45 9
Ok, a very simple command ui.
#!/usr/bin/perl -w use strict; print "enter 6 numbers followed by spaces:"; my @nums = split(/\s/,<STDIN>); die "Input Error" if @nums !=6; print sum(@nums),"\n"; print max(@nums),"\n"; sub sum { my @nums = @_; my $sum =0; foreach my $num (@nums) { $sum+= $num; } return ($sum); } sub max { my @nums = @_; my $max = -9999999999; foreach my $num (@nums) { if ($num>$max) { $max = $num; } } return ($max); }

Replies are listed 'Best First'.
Re^2: Subroutine help
by hackernet1337 (Initiate) on Aug 25, 2009 at 07:06 UTC
    Thanks alot guys!! Thank you Marshal, your code was just what i needed, thanks alot. is there like a thanks button or rep addder in this forum? Anyway, thank you all for your help.