How to resolve the algorithm Soloway's recurring rainfall step by step in the J programming language
How to resolve the algorithm Soloway's recurring rainfall step by step in the J programming language
Table of Contents
Problem Statement
Soloway's Recurring Rainfall is commonly used to assess general programming knowledge by requiring basic program structure, input/output, and program exit procedure. The problem: Write a program that will read in integers and output their average. Stop reading when the value 99999 is input. For languages that aren't traditionally interactive, the program can read in values as makes sense and stopping once 99999 is encountered. The classic rainfall problem comes from identifying success of Computer Science programs with their students, so the original problem statement is written above -- though it may not strictly apply to a given language in the modern era. Implementation Details: 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:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Soloway's recurring rainfall step by step in the J programming language
Source code in the j programming language
require'general/misc/prompt'
task=: {{
list=. ''
while. do.
y=. (#~ >.=<.)_.".prompt'Enter rainfall int, 99999 to quit: '
if. 99999 e. y do. (+/%#)list return. end.
if. y-:y do. echo 'New average: ',":(+/%#)list=. list,y
else. echo 'invalid input, reenter'
end.
end.
}}
task''
Enter rainfall int, 99999 to quit: 2
New average: 2
Enter rainfall int, 99999 to quit: 3
New average: 2.5
Enter rainfall int, 99999 to quit: 5
New average: 3.33333
Enter rainfall int, 99999 to quit: 7
New average: 4.25
Enter rainfall int, 99999 to quit: 11
New average: 5.6
Enter rainfall int, 99999 to quit: 99999
5.6
You may also check:How to resolve the algorithm Spelling of ordinal numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Tcl programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the Perl programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the Picat programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the tbas programming language