Jump to content

Soloway's recurring rainfall: Difference between revisions

m (→‎{{header|J}}: ignore unacceptable values (not shown) when computing the average)
(→‎{{header|Perl}}: Added Perl)
Line 539:
rainfall_problem()
</syntaxhighlight>
 
=={{header|Perl}}==
FWIW
<syntaxhighlight lang="perl" line>use strict;
use warnings;
 
use Scalar::Util qw(looks_like_number);
 
my ($periods, $accumulation, $rainfall);
 
sub so_far { printf "Average rainfall %.2f units over %d time periods.\n", ($accumulation / $periods) || 0, $periods }
 
while () {
while () {
print 'Integer units of rainfall in this time period? (999999 to finalize and exit)>: ';
$rainfall = <>;
last if looks_like_number($rainfall) and $rainfall and $rainfall == int $rainfall;
print "Invalid input, try again.\n";
}
(so_far, exit) if $rainfall == 999999;
$periods++;
$accumulation += $rainfall;
so_far;
}</syntaxhighlight>
{{out}}
<pre>Integer units of rainfall in this time period? (999999 to finalize and exit)>: raindrops
Invalid input, try again.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 2.00
Average rainfall 2.00 units over 1 time periods.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: hail
Invalid input, try again.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 10**0
Invalid input, try again.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 1
Average rainfall 1.50 units over 2 time periods.
Integer units of rainfall in this time period? (999999 to finalize and exit)>:
Invalid input, try again.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 999999
Average rainfall 1.50 units over 2 time periods.</pre>
 
=={{header|Phix}}==
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.