Jump to content

Programming Fundamentals/Conditions/Pseudocode

From Wikiversity

conditions.txt

[edit | edit source]
... This program asks the user to select Fahrenheit or Celsius conversion
... and input a given temperature. Then the program converts the given 
... temperature and displays the result.

... References:
...     https://www.mathsisfun.com/temperature-conversion.html

Function Main
    Declare String choice
    Declare Real temperature
    Declare Real result
    
    Assign choice = GetChoice()

    If Choice = "C" Or Choice = "c"    
        Assign temperature = GetTemperature("Celsius")
        Assign result = CalculateCelsius(temperature)
        Call DisplayResult(temperature, "Fahrenheit", result, "Celsius")
    False:
        If Choice = "F" Or Choice = "f"
            Assign temperature = GetTemperature("Fahrenheit")
            Assign result = CalculateFahrenheit(temperature)
            Call DisplayResult(temperature, "Celsius", result, "Fahrenheit")
        False:
            Output "You must enter C to convert to Celsius or F to convert to Fahrenheit!"
        End
    End
End

Function GetChoice
    Declare String choice
    
    Output "Enter F to convert to Fahrenheit or C to convert to Celsius:"
    Input Choice
Return String choice

Function GetTemperature (String label)
    Declare Real temperature
    
    Output "Enter " & label & " temperature:"
    Input temperature
Return Real temperature

Function CalculateCelsius (Real fahrenheit)
    Declare Real celsius
    
    Assign celsius = (fahrenheit - 32) * 5 / 9
Return Real celsius

Function CalculateFahrenheit (Real celsius)
    Declare Real fahrenheit
    
    Assign fahrenheit = celsius * 9 / 5 + 32
Return Real fahrenheit

Function DisplayResult (Real temperature, String fromLabel, Real result, String toLabel)
    Output temperature & "° " & fromLabel & " is " & result & "° " & toLabel
End

See Also

[edit | edit source]