Programming Fundamentals/Arrays/BASIC

From Wikiversity
Jump to navigation Jump to search

arrays.bas[edit | edit source]

' This program uses arrays to display temperature conversion tables 
' and temperature as an array subscript to find a given conversion.
'
' References:
'     https://www.mathsisfun.com/temperature-conversion.html
'     https://en.wikibooks.org/wiki/BASIC_Programming

DECLARE SUB Main()
DECLARE SUB BuildC(C())
DECLARE SUB BuildF(F())
DECLARE SUB DisplayArray(ArrayName$, Array())
DECLARE SUB FindTemperature(C(), F())
DECLARE FUNCTION Minimum(Value1, Value2)

Main()

SUB Main()
    DIM C(100) AS SINGLE
    DIM F(212) AS SINGLE
    
    BuildC C()
    BuildF F()
    DisplayArray "C", C()
    DisplayArray "F", F()
    FindTemperature C(), F()
END SUB

SUB BuildC(C())
    DIM Index AS INTEGER
    DIM Value AS SINGLE
    
    FOR Index = 0 TO UBOUND(C)
        Value = Index * 9 / 5 + 32
        C(Index) = Value
    NEXT Index
END SUB

SUB BuildF(F())
    DIM Index AS INTEGER
    DIM Value AS SINGLE
    
    FOR Index = 0 TO UBOUND(F)
        Value = (Index - 32) * 5 / 9
        F(Index) = Value
    NEXT Index
END SUB

SUB DisplayArray(ArrayName$, Array())
    DIM Index
    
    FOR Index = 0 TO UBOUND(Array)
        PRINT ArrayName$ + "[" + STR$(Index) + "] = " + STR$(Array(Index))
    NEXT Index
END SUB

SUB FindTemperature(C(), F())
    DIM Temp
    DIM Size
    
    Size = Minimum(UBOUND(C), UBOUND(F))
    DO
        PRINT "Enter a temperature between 0 and " + STR$(Size)
        INPUT Temp
    LOOP WHILE Temp < 0 OR Temp > Size
    PRINT STR$(Temp) + "° Celsius is " + STR$(C(Temp)) + "° Fahrenheit"
    PRINT STR$(Temp) + "° Fahrenheit is " + STR$(F(Temp)) + "° Celsius"
END SUB

FUNCTION Minimum(Value1, Value2)
    DIM Result
    
    IF Value1 < Value2 THEN
        Result = Value1
    ELSE
        Result = Value2
    END IF
    
    Minimum = Result
END FUNCTION

Try It[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own BASIC compiler / interpreter / IDE.

See Also[edit | edit source]