in reply to Re: John Guttag's book - 2nd exercise. My attempt in Perl.
in thread John Guttag's book - 2nd exercise. My attempt in Perl.
Coded as such:
use strict; use warnings; # Initialize my ($x, $y, $z) = (-11,-13,4); my $maxodd; # Intentionally left as undef # Check $x (though $maxodd will be undef, write it consistently in cas +e code is revectored later) if ($x % 2) { if ( (!defined $maxodd) || ( (defined $maxodd) && ($x > $maxodd) ) + ) { $maxodd = $x; } } # Check $y if ($y % 2) { if ( (!defined $maxodd) || ( (defined $maxodd) && ($y > $maxodd) ) + ) { $maxodd = $y; } } # Check $z if ($z % 2) { if ( (!defined $maxodd) || ( (defined $maxodd) && ($z > $maxodd) ) + ) { $maxodd = $z; } } # Report results if (!defined $maxodd) { print "All are even numbers\n"; } else { print "$maxodd is the greatest odd number\n"; } # Fini exit;
This screams for the use of a subroutine, though I suspect the author isn't at that point in the book yet.
|
|---|