pcoady has asked for the wisdom of the Perl Monks concerning the following question:
For this I need a program to read in an unknown number of non-negative values from the user until a negative number is entered. Once a negative number is entered the program needs to print out three values: the largest, smallest, and average of the numbers.
This is what I have so far. I am unsure on how to get the program to recognize the largest and smallest.
#!/usr/bin/perl use Modern::Perl; my ( $total, $howmany, $largest, $smallest, $number, $average ) = ( 0, + 0, 0, 0, 0, 0 ); print "Please enter a number or -1 to end: "; chomp ( $number = <> ); while ( $number != -1 ) { $total += $number; $howmany ++; print "Please enter a number or -1 to end: "; chomp ( $number = <> ); } if ( $howmany == 0 ) { say "There were no numbers to average."; } else { say "The average of the numbers was ", $total / $howmany; say "The largest number was ", $largest; say "The smallest number was ", $smallest;
something like this is what I am thinking. for (my $x = 9999999; $x >= 1 ; $x-- ) { say $x } Im just not sure how to continue it through all the variables as they get larger
|
|---|