Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Soloways-recurring-rainfall/00-META.yaml
Normal file
2
Task/Soloways-recurring-rainfall/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Soloway's_recurring_rainfall
|
||||
23
Task/Soloways-recurring-rainfall/00-TASK.txt
Normal file
23
Task/Soloways-recurring-rainfall/00-TASK.txt
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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:'''
|
||||
* Only Integers are to be accepted as input
|
||||
* Output should be floating point
|
||||
* Rainfall can be negative (https://www.geographyrealm.com/what-is-negative-rainfall/)
|
||||
* For languages where the user is inputting data, the number of data inputs can be "infinite"
|
||||
* 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 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:'''
|
||||
* http://cs.brown.edu/~kfisler/Pubs/icer14-rainfall/icer14.pdf
|
||||
* https://www.curriculumonline.ie/getmedia/8bb01bff-509e-48ed-991e-3ab5dad74a78/Seppletal-2015-DoweknowhowdifficulttheRainfallProblemis.pdf
|
||||
* https://en.wikipedia.org/wiki/Moving_average#Cumulative_average
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
BEGIN # read a sequence of integers, terminated by 99999 and outpout their average #
|
||||
INT end value = 99999;
|
||||
INT sum := 0;
|
||||
INT count := 0;
|
||||
BOOL invalid value := FALSE;
|
||||
on value error( stand in, ( REF FILE f )BOOL: invalid value := TRUE );
|
||||
WHILE
|
||||
INT n := 0;
|
||||
WHILE
|
||||
print( ( "Enter rainfall (integer) or ", whole( end value, 0 ), " to quit: " ) );
|
||||
read( ( n, newline ) );
|
||||
invalid value
|
||||
DO
|
||||
print( ( "Invalid input, please enter an integer", newline ) );
|
||||
invalid value := FALSE
|
||||
OD;
|
||||
n /= end value
|
||||
DO
|
||||
sum +:= n;
|
||||
count +:= 1;
|
||||
print( ( "New average: ", fixed( sum / count, -12, 4 ), newline ) )
|
||||
OD
|
||||
END
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
with Ada.Text_IO;
|
||||
with Ada.Text_IO.Unbounded_IO;
|
||||
with Ada.Strings.Unbounded;
|
||||
with Ada.Integer_Text_IO;
|
||||
with Ada.IO_Exceptions;
|
||||
|
||||
procedure RecurringRainfall is
|
||||
Current_Average : Float := 0.0;
|
||||
Current_Count : Integer := 0;
|
||||
Input_Integer : Integer;
|
||||
|
||||
-- Recursively attempt to get a new integer
|
||||
function Get_Next_Input return Integer is
|
||||
Input_Integer : Integer;
|
||||
Clear_String : Ada.Strings.Unbounded.Unbounded_String;
|
||||
begin
|
||||
Ada.Text_IO.Put("Enter rainfall int, 99999 to quit: ");
|
||||
Ada.Integer_Text_IO.Get(Input_Integer);
|
||||
return Input_Integer;
|
||||
exception
|
||||
when Ada.IO_Exceptions.Data_Error =>
|
||||
Ada.Text_IO.Put_Line("Invalid input");
|
||||
-- We need to call Get_Line to make sure we flush the kb buffer
|
||||
-- The pragma is to ignore the fact that we are not using the result
|
||||
pragma Warnings (Off, Clear_String);
|
||||
Clear_String := Ada.Text_IO.Unbounded_IO.Get_Line;
|
||||
-- Recursively call self -- it'll break when valid input is hit
|
||||
-- We disable the infinite recursion because we're intentionally
|
||||
-- doing this. It will "break" when the user inputs valid input
|
||||
-- or kills the program
|
||||
pragma Warnings (Off, "infinite recursion");
|
||||
return Get_Next_Input;
|
||||
pragma Warnings (On, "infinite recursion");
|
||||
end Get_Next_Input;
|
||||
|
||||
begin
|
||||
loop
|
||||
Input_Integer := Get_Next_Input;
|
||||
exit when Input_Integer = 99999;
|
||||
|
||||
Current_Count := Current_Count + 1;
|
||||
Current_Average := Current_Average + (Float(1) / Float(Current_Count))*Float(Input_Integer) - (Float(1) / Float(Current_Count))*Current_Average;
|
||||
|
||||
Ada.Text_IO.Put("New Average: ");
|
||||
Ada.Text_IO.Put_Line(Float'image(Current_Average));
|
||||
|
||||
end loop;
|
||||
end RecurringRainfall;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
10 LET SUM = 0
|
||||
20 FOR RAINFALL = 1 TO 1E38
|
||||
30 INPUT "ENTER RAINFALL (AS AN INTEGER, OR 99999 TO QUIT): ";I
|
||||
40 IF I = 99999 THEN END
|
||||
50 IF I < > INT (I) THEN PRINT "?REENTER, MUST BE AN INTEGER": GOTO 30
|
||||
60 LET SUM = SUM + I
|
||||
70 PRINT " THE NEW AVERAGE RAINFALL IS "SUM / RAINFALL
|
||||
80 NEXT RAINFALL
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
i: 0
|
||||
sum: 0
|
||||
|
||||
while ø [
|
||||
n: input "Enter rainfall as integer (99999 to quit): "
|
||||
try? -> n: to :integer n
|
||||
else [
|
||||
print "Input must be an integer."
|
||||
continue
|
||||
]
|
||||
if 99999 = n -> break
|
||||
'i + 1
|
||||
'sum + n
|
||||
print ~« The current average rainfall is |sum // i|.
|
||||
]
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
n = 0
|
||||
sum = 0
|
||||
|
||||
while True
|
||||
input "Enter integral rainfall (99999 to quit): ", i
|
||||
if i = 99999 then exit while
|
||||
if (i < 0) or (i <> int(i)) then
|
||||
print "Must be an integer no less than 0, try again."
|
||||
else
|
||||
n += 1
|
||||
sum += i
|
||||
print " The current average rainfall is "; sum/n
|
||||
end if
|
||||
end while
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
#include <iostream>
|
||||
#include <limits>
|
||||
|
||||
int main()
|
||||
{
|
||||
float currentAverage = 0;
|
||||
unsigned int currentEntryNumber = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
int entry;
|
||||
|
||||
std::cout << "Enter rainfall int, 99999 to quit: ";
|
||||
std::cin >> entry;
|
||||
|
||||
if (!std::cin.fail())
|
||||
{
|
||||
if (entry == 99999)
|
||||
{
|
||||
std::cout << "User requested quit." << std::endl;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentEntryNumber++;
|
||||
currentAverage = currentAverage + (1.0f/currentEntryNumber)*entry - (1.0f/currentEntryNumber)*currentAverage;
|
||||
|
||||
std::cout << "New Average: " << currentAverage << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Invalid input" << std::endl;
|
||||
std::cin.clear();
|
||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
namespace RosettaCode
|
||||
{
|
||||
class CSharpRecurringRainfall
|
||||
{
|
||||
static int ReadNextInput()
|
||||
{
|
||||
System.Console.Write("Enter rainfall int, 99999 to quit: ");
|
||||
string input = System.Console.ReadLine();
|
||||
|
||||
if (System.Int32.TryParse(input, out int num))
|
||||
{
|
||||
return num;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Console.WriteLine("Invalid input");
|
||||
return ReadNextInput();
|
||||
}
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
double currentAverage = 0;
|
||||
int currentEntryNumber = 0;
|
||||
|
||||
for (int lastInput = ReadNextInput(); lastInput != 99999; lastInput = ReadNextInput())
|
||||
{
|
||||
currentEntryNumber++;
|
||||
currentAverage = currentAverage + (1.0/(float)currentEntryNumber)*lastInput - (1.0/(float)currentEntryNumber)*currentAverage;
|
||||
System.Console.WriteLine("New Average: " + currentAverage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// Unused variables
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
float currentAverage = 0;
|
||||
unsigned int currentEntryNumber = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
int ret, entry;
|
||||
|
||||
printf("Enter rainfall int, 99999 to quit: ");
|
||||
ret = scanf("%d", &entry);
|
||||
|
||||
if (ret)
|
||||
{
|
||||
if (entry == 99999)
|
||||
{
|
||||
printf("User requested quit.\n");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentEntryNumber++;
|
||||
currentAverage = currentAverage + (1.0f/currentEntryNumber)*entry - (1.0f/currentEntryNumber)*currentAverage;
|
||||
|
||||
printf("New Average: %f\n", currentAverage);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Invalid input\n");
|
||||
while (getchar() != '\n'); // Clear input buffer before asking again
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
import std.stdio;
|
||||
|
||||
void main()
|
||||
{
|
||||
float currentAverage = 0;
|
||||
uint currentEntryNumber = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
int entry;
|
||||
|
||||
write("Enter rainfall int, 99999 to quit: ");
|
||||
|
||||
try {
|
||||
readf("%d", entry);
|
||||
readln();
|
||||
}
|
||||
catch (Exception e) {
|
||||
writeln("Invalid input");
|
||||
readln();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (entry == 99999) {
|
||||
writeln("User requested quit.");
|
||||
break;
|
||||
} else {
|
||||
currentEntryNumber++;
|
||||
currentAverage = currentAverage + (1.0/currentEntryNumber)*entry - (1.0/currentEntryNumber)*currentAverage;
|
||||
|
||||
writeln("New Average: ", currentAverage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,156 @@
|
|||
{This code would normally be in a library somewhere, but it is included here for clarity}
|
||||
|
||||
{TKeywaiter interface}
|
||||
|
||||
type TKeyWaiter = class(TObject)
|
||||
private
|
||||
FControl: TWinControl;
|
||||
FControlCAbort: boolean;
|
||||
protected
|
||||
procedure HandleKeyPress(Sender: TObject; var Key: Char);
|
||||
public
|
||||
KeyChar: Char;
|
||||
ValidKey: boolean;
|
||||
AbortWait: boolean;
|
||||
constructor Create(Control: TWinControl);
|
||||
function WaitForKey: char;
|
||||
function WaitForInteger: integer;
|
||||
function WaitForReal: double;
|
||||
property ControlCAbort: boolean read FControlCAbort write FControlCAbort;
|
||||
end;
|
||||
|
||||
|
||||
{ TMemoWaiter implementation }
|
||||
|
||||
type TControlHack = class(TWinControl) end;
|
||||
|
||||
constructor TKeyWaiter.Create(Control: TWinControl);
|
||||
{Save the control we want to wait on}
|
||||
begin
|
||||
FControl:=Control;
|
||||
FControlCAbort:=False;
|
||||
end;
|
||||
|
||||
procedure TKeyWaiter.HandleKeyPress(Sender: TObject; var Key: Char);
|
||||
{Handle captured key press}
|
||||
begin
|
||||
KeyChar:=Key;
|
||||
ValidKey:=True;
|
||||
if ControlCAbort then AbortWait:=KeyChar = #$03;
|
||||
end;
|
||||
|
||||
|
||||
function TKeyWaiter.WaitForKey: char;
|
||||
{Capture keypress event and wait for key press control}
|
||||
{Spends most of its time sleep and aborts if the user}
|
||||
{sets the abort flag or the program terminates}
|
||||
begin
|
||||
ValidKey:=False;
|
||||
AbortWait:=False;
|
||||
TControlHack(FControl).OnKeyPress:=HandleKeyPress;
|
||||
repeat
|
||||
begin
|
||||
Application.ProcessMessages;
|
||||
Sleep(100);
|
||||
end
|
||||
until ValidKey or Application.Terminated or AbortWait;
|
||||
Result:=KeyChar;
|
||||
end;
|
||||
|
||||
|
||||
function TKeyWaiter.WaitForInteger: integer;
|
||||
var C: char;
|
||||
var S: string;
|
||||
begin
|
||||
Result:=0;
|
||||
S:='';
|
||||
{Wait for first numeric characters}
|
||||
repeat
|
||||
begin
|
||||
C:=WaitForKey;
|
||||
if AbortWait or Application.Terminated then exit;
|
||||
end
|
||||
until C in ['+','-','0'..'9'];
|
||||
{Read characters and convert to}
|
||||
{integer until non-integer arrives}
|
||||
repeat
|
||||
begin
|
||||
S:=S+C;
|
||||
C:=WaitForKey;
|
||||
if AbortWait or Application.Terminated then exit;
|
||||
end
|
||||
until not (C in ['+','-','0'..'9']);
|
||||
Result:=StrToInt(S);
|
||||
end;
|
||||
|
||||
|
||||
type TCharSet = set of char;
|
||||
|
||||
function TKeyWaiter.WaitForReal: double;
|
||||
var C: char;
|
||||
var S: string;
|
||||
const RealSet: TCharSet = ['-','+','.','0'..'9'];
|
||||
begin
|
||||
Result:=0;
|
||||
S:='';
|
||||
{Wait for first numeric characters}
|
||||
repeat
|
||||
begin
|
||||
C:=WaitForKey;
|
||||
if AbortWait or Application.Terminated then exit;
|
||||
end
|
||||
until C in RealSet;
|
||||
{Read characters and convert to}
|
||||
{integer until non-integer arrives}
|
||||
repeat
|
||||
begin
|
||||
S:=S+C;
|
||||
C:=WaitForKey;
|
||||
if AbortWait or Application.Terminated then exit;
|
||||
end
|
||||
until not (C in RealSet);
|
||||
Result:=StrToFloat(S);
|
||||
end;
|
||||
|
||||
|
||||
|
||||
{===========================================================}
|
||||
|
||||
|
||||
function WaitForReal(Memo: TMemo; Prompt: string): double;
|
||||
{Wait for double entered into TMemo component}
|
||||
var KW: TKeyWaiter;
|
||||
begin
|
||||
{Use custom object to wait for and capture reals}
|
||||
KW:=TKeyWaiter.Create(Memo);
|
||||
try
|
||||
Memo.Lines.Add(Prompt);
|
||||
Memo.SelStart:=Memo.SelStart-1;
|
||||
Memo.SetFocus;
|
||||
Result:=KW.WaitForReal;
|
||||
finally KW.Free; end;
|
||||
end;
|
||||
|
||||
var Count: integer;
|
||||
var Average: double;
|
||||
|
||||
procedure RecurringRainfall(Memo: TMemo);
|
||||
var D: double;
|
||||
begin
|
||||
Count:=0;
|
||||
Average:=0;
|
||||
while true do
|
||||
begin
|
||||
D:=WaitForReal(Memo,'Enter integer rainfall (99999 to quit): ');
|
||||
if Application.Terminated then exit;
|
||||
if (Trunc(D)<>D) or (D<0) then
|
||||
begin
|
||||
Memo.Lines.Add('Must be integer >=0');
|
||||
continue;
|
||||
end;
|
||||
if D=99999 then break;
|
||||
Inc(Count);
|
||||
Average := Average + (1 / Count) * D - (1 / Count)*Average;
|
||||
Memo.Lines.Add('New Average: '+FloatToStrF(Average,ffFixed,18,2));
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
function getNextInput() result(Input)
|
||||
implicit none
|
||||
integer :: Input
|
||||
integer :: Reason
|
||||
Reason = 1
|
||||
|
||||
do while (Reason > 0)
|
||||
print *, "Enter rainfall int, 99999 to quit: "
|
||||
read (*,*,IOSTAT=Reason) Input
|
||||
|
||||
if (Reason > 0) then
|
||||
print *, "Invalid input"
|
||||
end if
|
||||
enddo
|
||||
|
||||
end function getNextInput
|
||||
|
||||
program recurringrainfall
|
||||
implicit none
|
||||
real :: currentAverage
|
||||
integer :: currentCount
|
||||
integer :: lastInput
|
||||
integer :: getNextInput
|
||||
|
||||
currentAverage = 0
|
||||
currentCount = 0
|
||||
|
||||
do
|
||||
lastInput = getNextInput()
|
||||
|
||||
if (lastInput == 99999) exit
|
||||
|
||||
currentCount = currentCount + 1
|
||||
currentAverage = currentAverage + (1/real(currentCount))*lastInput - (1/real(currentCount))*currentAverage
|
||||
|
||||
print *, 'New Average: ', currentAverage
|
||||
enddo
|
||||
|
||||
|
||||
end program recurringrainfall
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
Dim As Integer n = 0, sum = 0
|
||||
Dim As Single i
|
||||
|
||||
Do
|
||||
Input "Enter integral rainfall (99999 to quit): ", i
|
||||
If i = 99999 Then
|
||||
Exit Do
|
||||
Elseif (i < 0) Or (i <> Int(i)) Then
|
||||
Print "Must be an integer no less than 0, try again."
|
||||
Else
|
||||
n += 1
|
||||
sum += i
|
||||
Print " The current average rainfall is"; sum/n
|
||||
End If
|
||||
Loop
|
||||
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
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.
|
||||
}}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
class recurringrainfall
|
||||
{
|
||||
private static int GetNextInt()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
System.out.print("Enter rainfall int, 99999 to quit: ");
|
||||
String input = System.console().readLine();
|
||||
|
||||
try
|
||||
{
|
||||
int n = Integer.parseInt(input);
|
||||
return n;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Invalid input");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void recurringRainfall() {
|
||||
float currentAverage = 0;
|
||||
int currentEntryNumber = 0;
|
||||
|
||||
while (true) {
|
||||
int entry = GetNextInt();
|
||||
|
||||
if (entry == 99999)
|
||||
return;
|
||||
|
||||
currentEntryNumber++;
|
||||
currentAverage = currentAverage + ((float)1/currentEntryNumber)*entry - ((float)1/currentEntryNumber)*currentAverage;
|
||||
|
||||
System.out.println("New Average: " + currentAverage);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
recurringRainfall();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
def isinteger: test("^ *[+-]?[0-9]+ *$");
|
||||
|
||||
def soloway:
|
||||
label $out
|
||||
| foreach (inputs, null) as $x (null;
|
||||
.invalid = false
|
||||
| if $x == null then .break = true
|
||||
elif ($x|isinteger) then ($x|tonumber) as $n
|
||||
| if $n == 99999
|
||||
then .break = true
|
||||
else .sum += $n | .n += 1
|
||||
end
|
||||
else .invalid = $x
|
||||
end;
|
||||
if .break then ., break $out
|
||||
elif .invalid then .
|
||||
else empty
|
||||
end)
|
||||
| (select(.invalid) | "Invalid entry (\(.invalid)). Please try again."),
|
||||
(select(.break and .sum) | "Average is: \(.sum / .n)") ;
|
||||
|
||||
soloway
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
"""
|
||||
Two annotated example outputs
|
||||
were given: 1) a run with three positive inputs, a zero, and
|
||||
a negative number before the sentinel; 2) a run in which
|
||||
the sentinel was the first and only input.
|
||||
"""
|
||||
function rainfall_problem(sentinel = 999999, allownegative = true)
|
||||
total, entries = 0, 0
|
||||
while true
|
||||
print("Enter rainfall as $(allownegative ? "" : "nonnegative ")integer ($sentinel to exit): ")
|
||||
n = tryparse(Int, readline())
|
||||
if n == sentinel
|
||||
break
|
||||
elseif n == nothing || !allownegative && n < 0
|
||||
println("Error: bad input. Try again\n")
|
||||
else
|
||||
total += n
|
||||
entries += 1
|
||||
println("Average rainfall is currently ", total / entries)
|
||||
end
|
||||
end
|
||||
if entries == 0
|
||||
println("No entries to calculate!")
|
||||
end
|
||||
end
|
||||
|
||||
rainfall_problem()
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
import std/[strformat, strutils]
|
||||
|
||||
const EndValue = 99999
|
||||
|
||||
|
||||
var sum, count = 0.0
|
||||
|
||||
while true:
|
||||
|
||||
var value: int
|
||||
|
||||
# Read input until a valid integer if found.
|
||||
var input: string
|
||||
while true:
|
||||
stdout.write &"Enter an integer value, {EndValue} to terminate: "
|
||||
stdout.flushFile()
|
||||
try:
|
||||
input = stdin.readLine()
|
||||
value = input.parseInt()
|
||||
break
|
||||
except ValueError:
|
||||
echo &"Expected an integer: got “{input}”"
|
||||
except EOFError:
|
||||
quit "\nEncountered end of file. Exiting.", QuitFailure
|
||||
|
||||
# Process value.
|
||||
if value == EndValue:
|
||||
echo "End of processing."
|
||||
break
|
||||
count += 1
|
||||
sum += value.toFloat
|
||||
echo &" Current average is {sum / count}."
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">inputs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5.4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"five"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">99999</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">average</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show_average</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" The current average rainfall is %g\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">average</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">r</span> <span style="color: #008080;">in</span> <span style="color: #000000;">inputs</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Enter integral rainfall, 99999 to quit: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">/*or r<0*/</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000080;font-style:italic;">-- printf(1,"Must be a non-negative integer, try again.\n")</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Must be an integer, try again.\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">=</span><span style="color: #000000;">99999</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">average</span> <span style="color: #0000FF;">+=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">-</span><span style="color: #000000;">average</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">n</span>
|
||||
<span style="color: #000000;">show_average</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">show_average</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
Define.i n=0, sum=0
|
||||
Define.f i
|
||||
|
||||
If OpenConsole()
|
||||
Repeat
|
||||
Print("Enter integral rainfall (99999 to quit): ")
|
||||
i=ValF(Input())
|
||||
If i=99999
|
||||
Break
|
||||
ElseIf i<0 Or i<>Int(i)
|
||||
PrintN("Must be an integer no less than 0, try again.")
|
||||
Else
|
||||
n+1
|
||||
sum+i
|
||||
PrintN(" The current average rainfall is "+StrF(sum/n,5))
|
||||
EndIf
|
||||
ForEver
|
||||
EndIf
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
import sys
|
||||
|
||||
def get_next_input():
|
||||
try:
|
||||
num = int(input("Enter rainfall int, 99999 to quit: "))
|
||||
except:
|
||||
print("Invalid input")
|
||||
return get_next_input()
|
||||
return num
|
||||
|
||||
current_average = 0.0
|
||||
current_count = 0
|
||||
|
||||
while True:
|
||||
next = get_next_input()
|
||||
|
||||
if next == 99999:
|
||||
sys.exit()
|
||||
else:
|
||||
current_count += 1
|
||||
current_average = current_average + (1.0/current_count)*next - (1.0/current_count)*current_average
|
||||
|
||||
print("New average: ", current_average)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
n = 0
|
||||
sum = 0
|
||||
|
||||
DO
|
||||
INPUT "Enter integral rainfall (99999 to quit): ", i
|
||||
IF i = 99999 THEN
|
||||
EXIT DO
|
||||
ELSEIF (i < 0) OR (i <> INT(i)) THEN
|
||||
PRINT "Must be an integer no less than 0, try again."
|
||||
ELSE
|
||||
n = n + 1
|
||||
sum = sum + i
|
||||
PRINT " The current average rainfall is"; sum / n
|
||||
END IF
|
||||
LOOP
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
[ $ "bigrat.qky" loadfile ] now!
|
||||
|
||||
[ 0 0
|
||||
[ $ "Enter an integer (99999 to end): "
|
||||
[ input $->n not iff
|
||||
[ drop
|
||||
$ " Try again: " ]
|
||||
again ]
|
||||
dup 99999 != while
|
||||
+ dip 1+
|
||||
again ]
|
||||
drop
|
||||
cr
|
||||
over 0 = iff
|
||||
[ 2drop
|
||||
say "No data entered." ] done
|
||||
say "Average: "
|
||||
swap 10 point$ echo$ ] is srr ( --> )
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
# Write a program that will read in integers and
|
||||
# output their average. Stop reading when the
|
||||
# value 99999 is input.
|
||||
|
||||
|
||||
my ($periods, $accumulation, $rainfall) = 0, 0;
|
||||
|
||||
loop {
|
||||
loop {
|
||||
$rainfall = prompt 'Integer units of rainfall in this time period? (999999 to finalize and exit)>: ';
|
||||
last if $rainfall.chars and $rainfall.Numeric !~~ Failure and $rainfall.narrow ~~ Int and $rainfall ≥ 0;
|
||||
say 'Invalid input, try again.';
|
||||
}
|
||||
last if $rainfall == 999999;
|
||||
++$periods;
|
||||
$accumulation += $rainfall;
|
||||
say-it;
|
||||
}
|
||||
|
||||
say-it;
|
||||
|
||||
sub say-it { printf "Average rainfall %.2f units over %d time periods.\n", ($accumulation / $periods) || 0, $periods }
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
fn main() {
|
||||
|
||||
let mut current_average:f32 = 0.0;
|
||||
let mut current_entry_number:u32 = 0;
|
||||
|
||||
loop
|
||||
{
|
||||
let current_entry;
|
||||
|
||||
println!("Enter rainfall int, 99999 to quit: ");
|
||||
let mut input_text = String::new();
|
||||
std::io::stdin().read_line(&mut input_text).expect("Failed to read from stdin");
|
||||
let trimmed = input_text.trim();
|
||||
match trimmed.parse::<u32>() {
|
||||
Ok(new_entry) => current_entry = new_entry,
|
||||
Err(..) => { println!("Invalid input"); continue; }
|
||||
};
|
||||
|
||||
if current_entry == 99999
|
||||
{
|
||||
println!("User requested quit.");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
current_entry_number = current_entry_number + 1;
|
||||
current_average = current_average + (1.0 / current_entry_number as f32)*(current_entry as f32) - (1.0 / current_entry_number as f32)*current_average;
|
||||
|
||||
println!("New Average: {}", current_average);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
LET n = 0
|
||||
LET sum = 0
|
||||
|
||||
DO
|
||||
PRINT "Enter integral rainfall (99999 to quit): "
|
||||
INPUT i
|
||||
IF i = 99999 THEN
|
||||
EXIT DO
|
||||
ELSEIF (i < 0) OR (i <> INT(i)) THEN
|
||||
PRINT "Must be an integer no less than 0, try again."
|
||||
ELSE
|
||||
LET n = n + 1
|
||||
LET sum = sum + i
|
||||
PRINT " The current average rainfall is"; sum / n
|
||||
END IF
|
||||
LOOP
|
||||
END
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
import "./ioutil" for Input
|
||||
|
||||
var n = 0
|
||||
var sum = 0
|
||||
while (true) {
|
||||
var i = Input.integer("Enter integral rainfall (99999 to quit): ")
|
||||
if (i == 99999) break
|
||||
n = n + 1
|
||||
sum = sum + i
|
||||
System.print(" The current average rainfall is %(sum/n)")
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
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);
|
||||
];
|
||||
]
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
// Rosetta Code problem: https://rosettacode.org/wiki/Soloway%27s_Recurring_Rainfall
|
||||
// by Jjuanhdez, 09/2022
|
||||
|
||||
n = 0
|
||||
sum = 0
|
||||
|
||||
do
|
||||
input "Enter integral rainfall (99999 to quit): " i
|
||||
if (i < 0) or (i <> int(i)) then
|
||||
print "Must be an integer no less than 0, try again."
|
||||
elsif i = 99999
|
||||
break
|
||||
else
|
||||
n = n + 1
|
||||
sum = sum + i
|
||||
print " The current average rainfall is ", sum/n
|
||||
end if
|
||||
loop
|
||||
Loading…
Add table
Add a link
Reference in a new issue