Soloway's recurring rainfall: Difference between revisions

m
 
(6 intermediate revisions by 6 users not shown)
Line 401:
 
<syntaxhighlight lang="Delphi">
 
{This code would normally be in a library somewhere, but it is included here for clarity}
 
{TKeywaiter interface}
Line 587 ⟶ 589:
 
</pre>
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
repeat
print "Enter integral rainfall (99999 to quit): "
i = number input
until i = 99999
if error = 1 or i < 0 or i mod 1 <> 0
print "Must be an integer no less than 0, try again."
else
n += 1
sum += i
print " The current average rainfall is " & sum / n
.
.
</syntaxhighlight>
 
=={{header|Fortran}}==
Line 1,109 ⟶ 1,128:
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 999999
Average rainfall 0.00 units over 0 time periods.</pre>
 
=={{header|RPL}}==
{{works with|HP|28}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ "Enter rainfall, then ENTER" 1 DISP
"" "0123456789"
'''DO DO UNTIL''' KEY '''END'''
'''IF''' DUP2 POS '''THEN'''
ROT OVER + DUP 2 DISP ROT ROT
'''END'''
'''UNTIL''' "ENTER" == '''END'''
DROP STR→
≫ '<span style="color:blue">READB</span>' STO
≪ "Enter 9999 to end" 4 DISP
(0,0) 1 CF CLLCD
DO READB
'''IF''' DUP 9999 == '''THEN''' 1 SF
'''ELSE'''
1 R→C +
"Average = " OVER RE LAST IM / →STR +
3 DISP
'''END'''
'''UNTIL''' 1 FS? '''END'''
SWAP DROP CLMF
'''IFERR''' RE LAST IM / '''THEN''' DROP2 '''END'''
≫ '<span style="color:blue">RFALL</span>' STO
|
<span style="color:blue">READB</span> ''( → n )''
Initialize variables
Loop
if keystroke is an accepted char
add char to output and display it
until ENTER is pressed
clean stack and convert to integer
<span style="color:blue">RFALL</span> ''( → ) ''
initialize counters and flag, clear screen
loop
get rainfall
if break value then set flag
otherwise
accumulate rainfall and increment count
display average rainfall
until flag set
clean stack, unfreeze display
put average rainfall in stack if count ≠ 0
|}
 
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">QUIT = 99999
num = nil
count = 0
avg = 0.0
 
loop do
begin
print "Enter rainfall int, #{QUIT} to quit: "
input = gets
num = Integer(input)
rescue ArgumentError
puts "Invalid input #{input}"
redo
end
break if num == QUIT
count += 1
inv_count = 1.0/count
avg = avg + inv_count*num - inv_count*avg
end
 
puts "#{count} numbers entered, averaging #{average}"
</syntaxhighlight>
 
=={{header|Rust}}==
Line 1,143 ⟶ 1,243:
}
}
}
</syntaxhighlight>
=={{header|Swift}}==
<syntaxhighlight lang="swift">
import Foundation
 
var mean: Double = 0
var count: Int = 0
var prompt = "Enter integer rainfall or 99999 to exit:"
var term = " "
print(prompt, terminator: term)
while let input = readLine() {
defer {
print("count: \(count), mean: \(mean.formatted())\n\(prompt)", terminator: term)
}
guard let val = Int(input) else {
print("Integer values only")
continue
}
if val == 99999 {
(prompt, term) = ("Done","\n")
break
}
count += 1
mean += Double(val)/Double(count) - mean/Double(count)
}
</syntaxhighlight>
Line 1,148 ⟶ 1,273:
=={{header|Wren}}==
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "./ioutil" for Input
 
var n = 0
1,983

edits