Soloway's recurring rainfall: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 9: | Line 9: | ||
* http://cs.brown.edu/~kfisler/Pubs/icer14-rainfall/icer14.pdf |
* 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://www.curriculumonline.ie/getmedia/8bb01bff-509e-48ed-991e-3ab5dad74a78/Seppletal-2015-DoweknowhowdifficulttheRainfallProblemis.pdf |
||
* https://en.wikipedia.org/wiki/Moving_average#Cumulative_average |
|||
=={{header|Ada}}== |
|||
<syntaxhighlight lang="ada"> |
|||
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; |
|||
</syntaxhighlight> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 53: | Line 107: | ||
return 0; |
return 0; |
||
} |
|||
</syntaxhighlight> |
|||
=={{header|C++}}== |
|||
<syntaxhighlight lang="cpp"> |
|||
#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; |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|C sharp|C#}}== |
|||
<syntaxhighlight lang="csharp"> |
|||
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); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|D}}== |
|||
<syntaxhighlight lang="d"> |
|||
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); |
|||
} |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Fortran}}== |
|||
'''Fortran 90''' |
|||
<syntaxhighlight lang="fortran"> |
|||
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 |
|||
</syntaxhighlight> |
|||
=={{header|Java}}== |
|||
<syntaxhighlight lang="java"> |
|||
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(); |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Python}}== |
|||
<syntaxhighlight lang="python"> |
|||
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) |
|||
</syntaxhighlight> |
|||
=={{header|Rust}}== |
|||
<syntaxhighlight lang="rust"> |
|||
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); |
|||
} |
|||
} |
|||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |