Soloway's recurring rainfall: Difference between revisions

Content added Content deleted
(Applesoft BASIC)
(Added XPL0 example.)
Line 15: Line 15:
* A complete implementation should handle error cases reasonably (asking the user for more input, skipping the bad value when encountered, etc)
* A complete implementation should handle error cases reasonably (asking the user for more input, skipping the bad value when encountered, etc)


The purpose of this problem, as originally proposed in the 1980's through it's continued use today, is to just show fundamentals of CS: iteration, branching, program structure, termination, management of data types, input/output (where applicable), etc with things like input validation or management of numerical limits being more "advanced". It isn't meant to literally be a rainfall calculator so implementations should strive to implement the solution clearly and simply.
The purpose of this problem, as originally proposed in the 1980's through its continued use today, is to just show fundamentals of CS: iteration, branching, program structure, termination, management of data types, input/output (where applicable), etc with things like input validation or management of numerical limits being more "advanced". It isn't meant to literally be a rainfall calculator so implementations should strive to implement the solution clearly and simply.


'''References:'''
'''References:'''
Line 848: Line 848:
The current average rainfall is 4.25
The current average rainfall is 4.25
Enter integral rainfall (99999 to quit): 99999
Enter integral rainfall (99999 to quit): 99999
</pre>

=={{header|XPL0}}==
The problem is very simple except for the vague requirements for the user
interface and how to deal with unaccepted rainfall amounts.
<syntaxhighlight lang="XPL0">
real Rain, Sum, Count;
[Sum:= 0.; Count:= 0.;
loop [loop [Text(0, "Enter rainfall amount, or 99999 to quit: ");
Rain:= RlIn(0);
if Rain < 0. then
Text(0, "Must not be negative.^m^j")
else if Floor(Rain) # Rain then
Text(0, "Must be an integer.^m^j")
else quit;
];
if Rain = 99999. then quit;
Sum:= Sum + Rain;
Count:= Count + 1.;
Text(0, "Average rainfall is ");
RlOut(0, Sum/Count);
CrLf(0);
];
]</syntaxhighlight>

{{out}}
<pre>
Enter rainfall amount, or 99999 to quit: 5.4
Must be an integer.
Enter rainfall amount, or 99999 to quit: five
5
Average rainfall is 5.00000
Enter rainfall amount, or 99999 to quit: -2
Must not be negative.
Enter rainfall amount, or 99999 to quit: 4
Average rainfall is 4.50000
Enter rainfall amount, or 99999 to quit:
2.00000001
Must be an integer.
Enter rainfall amount, or 99999 to quit: .3e1
Average rainfall is 4.00000
Enter rainfall amount, or 99999 to quit: 99999
</pre>
</pre>