No such thing as a small change PerlMonks

### Re^4: Finding the max()/min()

by fnicholas (Initiate)
 on Jan 26, 2012 at 15:11 UTC ( #950115=note: print w/replies, xml ) Need Help??

in reply to Re^3: Finding the max()/min()

Here you go:

This is how one can find a maximum number in an array iteratively and recursively in Perl

```
#!/usr/bin/perl

my (@setOfnumbers);

@setOfnumbers = ( 9, 7, 90, 3, 8, 412, 67, 2, 45, 53, 1, 3,89 );

print "Finding maximum number iteratively ",
getMaximumNumber(@setOfnumbers) , "\n";
print "Finding maximum number recursively ",
findMaximumNumber(@setOfnumbers) , "\n";

#Finding maximum number iteratively
sub getMaximumNumber{
my (\$maximumNumber);
\$maximumNumber = @_[0];
foreach(@_){
if (\$_ > \$maximumNumber){
\$maximumNumber = \$_;
}
}
return \$maximumNumber;
}

#Finding maximum number recursively
sub findMaximumNumber{

my (\$maximumNumber);

if (@_ == 1){
\$maximumNumber = shift(@_);
return \$maximumNumber;
}
else{
\$maximumNumber = shift(@_);
return (findMaximumNumber(@_) > \$maximumNumber)
? findMaximumNumber(@_) : \$maximumNumber;
}

}

Create A New User
Node Status?
node history
Node Type: note [id://950115]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-08-21 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (322 votes). Check out past polls.

Notices?