Contoh Program Sortir Data dengan Bahasa Cobol :
IDENTIFICATION DIVISION.
PROGRAM-ID. SORTIR-DATA.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 999.
77 I PIC 999.
77 J PIC 999.
77 K PIC 999.
77 TEMP PIC 999.
77 D PIC X.
01 AAA.
02 A PIC 999 OCCURS 100 TIMES.
PROCEDURE DIVISION.
MAIN.
DISPLAY (1 1) ERASE.
DISPLAY (9, 30) "INPUT BILANGAN : ".
DISPLAY (10, 27) "___________________".
ACCEPT (11, 36) N WITH PROMPT.
DISPLAY (1 1) ERASE.
DISPLAY (6, 30) "MASUKKAN BILANGAN-BILANGAN".
MOVE 6 TO LIN.
PERFORM ACC-PARA VARYING I FROM 1 BY 1 UNTIL I > N.
PERFORM BUB-PARA VARYING I FROM 1 BY 1 UNTIL I > N.
DISPLAY (1 1) ERASE.
DISPLAY (5, 30) "HASIL (BUBBLE SORT) :".
DISPLAY (6, 30) "____________________".
MOVE 6 TO LIN.
PERFORM DIS-PARA VARYING I FROM 1 BY 1 UNTIL I > N.
STOP RUN.
ACC-PARA.
COMPUTE LIN = LIN + 1.
ACCEPT (LIN, 35) A(I) WITH PROMPT.
IF LIN = 20
DISPLAY (1 1) ERASE
DISPLAY (6, 30) "CONTINUE"
MOVE 6 TO LIN.
BUB-PARA.
IF A(I) > A(J)
MOVE A(I) TO TEMP
MOVE A(J) TO A(I)
MOVE TEMP TO A(J).
DIS-PARA.
COMPUTE LIN = LIN + 1.
DISPLAY (LIN, 33) A(I).
IF LIN = 20
DISPLAY (24, 30) "PRESS AN KEY"
ACCEPT D
DISPLAY (1 1) ERASE
MOVE 6 TO LIN.
Program ini akan mengurutkan bilangan dari yang terkecil sampai terbesar.
Tidak ada komentar :
Posting Komentar