How to resolve the algorithm Soloway's recurring rainfall step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Soloway's recurring rainfall step by step in the Julia 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 Julia programming language

The provided Julia code defines a function rainfall_problem that prompts users to enter rainfall amounts until they enter a sentinel value (defaulting to 999999) to stop the input. The rainfall amounts can be either positive or non-negative, depending on the value of the allownegative parameter (defaulting to true).

Here's a step-by-step explanation of the code:

  1. Function Definition:

    function rainfall_problem(sentinel = 999999, allownegative = true)

    This line defines a function named rainfall_problem that takes two optional parameters: sentinel (defaulting to 999999) and allownegative (defaulting to true).

  2. Variable Initialization:

    total, entries = 0, 0

    Two variables, total and entries, are initialized to 0. total will store the cumulative rainfall, and entries will keep track of the number of valid rainfall entries.

  3. Input Loop:

    while true
       # ...
    end

    This is an infinite while loop that continues until the user enters the sentinel value.

  4. User Prompt:

    print("Enter rainfall as $(allownegative ? "" : "nonnegative ")integer ($sentinel to exit): ")

    This line prompts the user to enter the rainfall amount. It also mentions whether negative values are allowed or not based on the allownegative parameter.

  5. Input Handling:

    n = tryparse(Int, readline())

    This line reads the user's input from the standard input (the keyboard) using readline(). It then tries to parse the input as an integer using tryparse(). If the input is not a valid integer, n will be nothing.

  6. Sentinel Check:

    if n == sentinel
       break

    If the entered value (n) is equal to the sentinel value, it means the user wants to stop entering rainfall amounts. In this case, the while loop is broken.

  7. Error Handling:

    elseif n == nothing || !allownegative && n < 0
       println("Error: bad input. Try again\n")

    If the input is not a valid integer (n == nothing) or if negative values are not allowed and the input is negative, an error message is printed, and the user is asked to try again.

  8. Rainfall Accumulation:

    total += n
    entries += 1
    println("Average rainfall is currently ", total / entries)

    If the input is a valid integer and meets the negative value check, it is added to the total cumulative rainfall. The entries count is also incremented. After each valid input, the average rainfall is calculated and printed to the console.

  9. End of Loop: Once the sentinel value is entered, or if the input is invalid and not fixed by the user, the while loop ends.

  10. Final Check:

if entries == 0 println("No entries to calculate!") end

After the loop ends, this code checks if there were no valid rainfall entries (`entries == 0`). If there were no valid entries, a message is printed stating that there are no entries to calculate the average for.

11. **Function Call**:
 ```julia
rainfall_problem()

Finally, the rainfall_problem function is called without any arguments, using the default values for sentinel and allownegative. This prompts the user to enter rainfall amounts until they enter the sentinel value (999999).

Source code in the julia programming language

"""
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()


  

You may also check:How to resolve the algorithm Copy a string step by step in the Delphi programming language
You may also check:How to resolve the algorithm Jordan-Pólya numbers step by step in the Pascal programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the Fortran programming language
You may also check:How to resolve the algorithm Bernoulli numbers step by step in the Swift programming language
You may also check:How to resolve the algorithm First class environments step by step in the Phix programming language