Two-Level Tables
Part of the Tables and Text section of Coddy's COBOL Programming journey. Lesson 4 of 19.
An OCCURS inside another makes a table of rows and columns:
01 HALL.
05 HALL-ROW OCCURS 3 TIMES.
10 SEAT PIC X OCCURS 4 TIMES.HALL holds 3 rows of 4 seats, 12 characters. A seat takes two subscripts, the outer one first: SEAT(2, 3) is row 2, seat 3.
One subscript on the outer level names a whole row, a group of 4 characters:
DISPLAY HALL-ROW(2). *> X..XA loop inside a loop visits every element, row by row:
PERFORM VARYING R FROM 1 BY 1 UNTIL R > 3
PERFORM VARYING S FROM 1 BY 1
UNTIL S > 4
DISPLAY SEAT(R, S)
END-PERFORM
END-PERFORM.Challenge
EasyThe program reads a row into R and a seat into S. Display Taken when SEAT of that row and seat holds X, and Free otherwise.
Example input, one line per ACCEPT: 1, 2.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. SEATS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 HALL.
05 HALL-ROW OCCURS 3 TIMES.
10 SEAT PIC X OCCURS 4 TIMES.
01 R PIC 9.
01 S PIC 9.
PROCEDURE DIVISION.
MOVE "XX..X..X...." TO HALL.
ACCEPT R.
ACCEPT S.
*> Display Taken or Free here
STOP RUN.
All lessons in Tables and Text
Practice on your own: Online COBOL compiler