Jump to content

COBOL/Arrays

From Wikiversity

arrays.cbl

[edit | edit source]
*> This program uses an array to calculate and display a 
*> Celsius to Fahrenheit temperature conversion table.
*>
*> References:
*>     https://www.tutorialspoint.com/cobol/index.htm
*>     https://open-cobol.sourceforge.io/doc/gnucobol.html

IDENTIFICATION DIVISION.
PROGRAM-ID. ARRAYS.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 CELSIUS.
    05 CELSIUS-VALUE PIC 9(3)v9(2) OCCURS 100 TIMES INDEXED BY C.
01 CELSIUS-OUT      PIC Z(2)9.
01 FAHRENHEIT-OUT   PIC Z(2)9.99.

PROCEDURE DIVISION.

MAIN.
    PERFORM CALCULATE-CELSIUS VARYING C FROM 1 BY 1 UNTIL C > 100.
    PERFORM DISPLAY-CELSIUS VARYING C FROM 1 BY 1 UNTIL C > 100.
    STOP RUN.

CALCULATE-CELSIUS.
    COMPUTE CELSIUS-VALUE(C) = C * 9 / 5 + 32.

DISPLAY-CELSIUS.
    MOVE C TO CELSIUS-OUT.
    MOVE CELSIUS-VALUE(C) TO FAHRENHEIT-OUT.
    DISPLAY CELSIUS-OUT "° Celsius is " FAHRENHEIT-OUT "° Fahrenheit".

Try It

[edit | edit source]

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

See Also

[edit | edit source]