Jump to content

Programming Fundamentals/Condition Examples

From Wikibooks, open books for an open world

Temperature

[edit | edit source]

Pseudocode

[edit | edit source]
Function Main
    Declare String choice
    
    Assign choice = GetChoice()
    If Choice = "C" Or Choice = "c"
        Call ProcessCelsius()
    False:
        If Choice = "F" Or Choice = "f"
            Call ProcessFahrenheit()
        False:
            Output "You must enter C to convert to Celsius or F to convert to Fahrenheit!"
        End
    End
End

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 fromScale, Real result, String toScale)
    Output temperature & "° " & fromScale & " is " & result & "° " & toScale
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 scale)
    Declare Real temperature
    
    Output "Enter " & scale & " temperature:"
    Input temperature
Return Real temperature

Function ProcessCelsius
    Declare Real temperature
    Declare Real result
    
    Assign temperature = GetTemperature("Celsius")
    Assign result = CalculateCelsius(temperature)
    Call DisplayResult(temperature, "Fahrenheit", result, "Celsius")
End

Function ProcessFahrenheit
    Declare Real temperature
    Declare Real result
    
    Assign temperature = GetTemperature("Fahrenheit")
    Assign result = CalculateFahrenheit(temperature)
    Call DisplayResult(temperature, "Celsius", result, "Fahrenheit")
End

Output

[edit | edit source]
Enter C to convert to Celsius or F to convert to Fahrenheit:
 c
Enter Fahrenheit temperature:
 100
100° Fahrenheit is 37.7777777777778° Celsius

Enter C to convert to Celsius or F to convert to Fahrenheit:
 f
Enter Celsius temperature:
 100
100° Celsius is 212° Fahrenheit

Enter C to convert to Celsius or F to convert to Fahrenheit:
 x
You must enter C to convert to Celsius or F to convert to Fahrenheit.

Flowchart

[edit | edit source]

Conditions flowchart Conditions ProcessCelsius flowchart Conditions ProcessFahrenheit flowchart Conditions GetChoice flowchart Conditions GetTemperature flowchart Conditions CalculateCelsius flowchart Conditions CalculateFahrenheit flowchart Conditions DisplayResult flowchart

References

[edit | edit source]