Soloway's recurring rainfall: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|ALGOL 68}}: Allow for negative rainfall)
Line 76: Line 76:
<syntaxhighlight lang="algol68">
<syntaxhighlight lang="algol68">
BEGIN # read a sequence of integers, terminated by 99999 and outpout their average #
BEGIN # read a sequence of integers, terminated by 99999 and outpout their average #
INT end value = 99999;
INT end value = 99999;
INT sum := 0;
INT sum := 0;
INT count := 0;
INT count := 0;
BOOL invalid value := FALSE;
on value error( stand in, ( REF FILE f )BOOL: invalid value := TRUE );
WHILE
WHILE
INT n := 0;
INT n := 0;
WHILE
WHILE
STRING s;
print( ( "Enter rainfall (integer) or ", whole( end value, 0 ), " to quit: " ) );
print( ( "Enter rainfall (integer) or ", whole( end value, 0 ), " to quit: " ) );
read( ( s, newline ) );
read( ( n, newline ) );
BOOL valid := UPB s >= LWB s; # invalid if the string is empty #
invalid value
FOR s pos FROM LWB s TO UPB s WHILE valid DO
IF valid := s[ s pos ] >= "0" AND s[ s pos ] <= "9"
THEN
n *:= 10 +:= ( ABS s[ s pos ] - ABS "0" )
FI
OD;
NOT valid
DO
DO
print( ( "Invalid input, please enter an integer", newline ) )
print( ( "Invalid input, please enter an integer", newline ) );
invalid value := FALSE
OD;
OD;
n /= end value
n /= end value