first commit
This commit is contained in:
48
RecurringRainfall/Ada/recurringrainfall.adb
Normal file
48
RecurringRainfall/Ada/recurringrainfall.adb
Normal file
@@ -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;
|
||||
33
RecurringRainfall/C/CMakeLists.txt
Normal file
33
RecurringRainfall/C/CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Modify only these if one source file!
|
||||
project(CRecurringRainfall)
|
||||
set(CURRENT_PROJECT_CODE_NAME recurringrainfall)
|
||||
set(FILE_EXT c)
|
||||
# End
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# We want all the warnings and as errors enabled
|
||||
if (MSVC)
|
||||
# warning level 4 and all warnings as errors
|
||||
add_compile_options(/W4 /WX)
|
||||
else()
|
||||
# lots of warnings and all warnings as errors
|
||||
add_compile_options(-Wall -Wextra -pedantic -Werror)
|
||||
endif()
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/${CURRENT_PROJECT_CODE_NAME}.${FILE_EXT})
|
||||
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_INCLUDES}
|
||||
)
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_LIBS}
|
||||
)
|
||||
|
||||
|
||||
42
RecurringRainfall/C/recurringrainfall.c
Normal file
42
RecurringRainfall/C/recurringrainfall.c
Normal file
@@ -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;
|
||||
}
|
||||
4
RecurringRainfall/CMakeLists.txt
Normal file
4
RecurringRainfall/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/C/CMakeLists.txt)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/Cpp/CMakeLists.txt)
|
||||
34
RecurringRainfall/CSharp/recurringrainfall.cs
Normal file
34
RecurringRainfall/CSharp/recurringrainfall.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace LanguageBasics
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
33
RecurringRainfall/Cpp/CMakeLists.txt
Normal file
33
RecurringRainfall/Cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Modify only these if one source file!
|
||||
project(CppRecurringRainfall)
|
||||
set(CURRENT_PROJECT_CODE_NAME recurringrainfall)
|
||||
set(FILE_EXT cpp)
|
||||
# End
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# We want all the warnings and as errors enabled
|
||||
if (MSVC)
|
||||
# warning level 4 and all warnings as errors
|
||||
add_compile_options(/W4 /WX)
|
||||
else()
|
||||
# lots of warnings and all warnings as errors
|
||||
add_compile_options(-Wall -Wextra -pedantic -Werror)
|
||||
endif()
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/${CURRENT_PROJECT_CODE_NAME}.${FILE_EXT})
|
||||
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_INCLUDES}
|
||||
)
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_LIBS}
|
||||
)
|
||||
|
||||
|
||||
54
RecurringRainfall/Cpp/recurringrainfall.cpp
Normal file
54
RecurringRainfall/Cpp/recurringrainfall.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Write a program that will read in integers and
|
||||
output their average. Stop reading when the
|
||||
value 99999 is input.
|
||||
|
||||
Example program output/input:
|
||||
Enter int: 0
|
||||
Average: 0
|
||||
Enter int: 2
|
||||
Average: 1
|
||||
...
|
||||
Enter int: 99999
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
34
RecurringRainfall/D/recurringrainfall.d
Normal file
34
RecurringRainfall/D/recurringrainfall.d
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
RecurringRainfall/Fortran/recurringrainfall.f90
Normal file
40
RecurringRainfall/Fortran/recurringrainfall.f90
Normal file
@@ -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
|
||||
42
RecurringRainfall/Java/recurringrainfall.java
Normal file
42
RecurringRainfall/Java/recurringrainfall.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
24
RecurringRainfall/Python/recurringrainfall.py
Normal file
24
RecurringRainfall/Python/recurringrainfall.py
Normal file
@@ -0,0 +1,24 @@
|
||||
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)
|
||||
|
||||
33
RecurringRainfall/Rust/recurringrainfall.rs
Normal file
33
RecurringRainfall/Rust/recurringrainfall.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
5
RecurringRainfall/directions.txt
Normal file
5
RecurringRainfall/directions.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
/*
|
||||
Write a program that will read in integers and
|
||||
output their average. Stop reading when the
|
||||
value 99999 is input.
|
||||
*/
|
||||
BIN
RecurringRainfall/icer14.pdf
Normal file
BIN
RecurringRainfall/icer14.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user