Three Dimensional Array with Index
Three Dimensional Array with Index Example
Scenario - Accessing three dimensional array with index.
Requirement - Let us declare a table to store two class details with two student details with 6 subjects marks. WS-CLASS is variable with all class information OCCURS 2 times to capture the two classes information. WS-STUDENT is variable with all student information OCCURS 2 times to capture the two students information. WS-MARKS-GRP is variable that OCCURS 6 times to capture 6 subjects marks.
Code -
----+----1----+----2----+----3----+----4----+----5----+
* Three Dimentional Table with Index
IDENTIFICATION DIVISION.
PROGRAM-ID. THRDIMIX.
AUTHOR. MTH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SCHOOL.
02 WS-CLASS OCCURS 2 TIMES INDEXED BY IDX-ONE.
03 WS-STUDENT OCCURS 2 TIMES INDEXED BY IDX-TWO.
05 WS-ROLL-NO PIC X(03).
05 WS-NAME PIC X(10).
05 WS-MARKS-GRP.
10 WS-MARKS PIC 9(03) OCCURS 6 TIMES
INDEXED BY IDX-THREE.
PROCEDURE DIVISION.
MOVE "011" TO WS-ROLL-NO(1,1)
MOVE "PAWAN Y" TO WS-NAME(1,1)
MOVE 079077082076089092 TO WS-MARKS-GRP(1,1)
MOVE "012" TO WS-ROLL-NO(1,2)
MOVE "KUMAR Y" TO WS-NAME(1,2)
MOVE 083079078089093086 TO WS-MARKS-GRP(1,2)
MOVE "021" TO WS-ROLL-NO(2,1)
MOVE "SRINIVAS" TO WS-NAME(2,1)
MOVE 079077082076089092 TO WS-MARKS-GRP(2,1)
MOVE "022" TO WS-ROLL-NO(2,2)
MOVE "SRIDHAR" TO WS-NAME(2,2)
MOVE 083079078089093086 TO WS-MARKS-GRP(2,2)
DISPLAY "Class1 Info: " WS-CLASS(1).
DISPLAY " "
DISPLAY "Class2 Info: " WS-CLASS(2).
DISPLAY " "
* Initializing first dimension Index
SET IDX-ONE TO 1.
DISPLAY "Students Information => "
* Loop for displaying 2 classes information
PERFORM UNTIL IDX-ONE > 2
* Initializing second dimension index
SET IDX-TWO TO 1
* Loop for displaying 2 classes information
PERFORM UNTIL IDX-TWO > 2
DISPLAY "Roll No: " WS-ROLL-NO (IDX-ONE,IDX-TWO)
DISPLAY "Name: " WS-NAME (IDX-ONE,IDX-TWO)
* Initializing 3rd dimension Index
SET IDX-THREE TO 1
* Loop for displaying 6 subjects information
DISPLAY "MARKS: "
PERFORM UNTIL IDX-THREE > 6
* Accessing subject marks
DISPLAY WS-MARKS(IDX-ONE, IDX-TWO, IDX-THREE)
* Increasing the 3rd dimension Index by 1
SET IDX-THREE UP BY 1
END-PERFORM
* Increasing the 3rd dimension Index by 1
SET IDX-TWO UP BY 1
END-PERFORM
* Increasing the first dimension Index by 1
SET IDX-ONE UP BY 1
END-PERFORM.
STOP RUN.
JCL to execute the above COBOL program −
//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID //* //STEP01 EXEC PGM=THRDIMTB //STEPLIB DD DSN=MATEPK.COBOL.LOADLIB,DISP=SHR //SYSOUT DD SYSOUT=*
When the program compiled and executed, it gives the following result −
Class1 Info: 011PAWAN Y 079077082076089092012KUMAR Y 083079078089093086 Class2 Info: 021SRINIVAS 079077082076089092022SRIDHAR 083079078089093086 Students Information => Roll No: 011 Name: PAWAN Y MARKS: 079 077 082 076 089 092 Roll No: 012 Name: KUMAR Y MARKS: 083 079 078 089 093 086 Roll No: 021 Name: SRINIVAS MARKS: 079 077 082 076 089 092 Roll No: 022 Name: SRIDHAR MARKS: 083 079 078 089 093 086
Explaining Example -
In the above example:
- WS-CLASS(1) represents class1 information and WS-CLASS(2) represents class2 information.
- IDX-ONE is index declared on WS-CLASS 1st dimension array. The index is initialized and incremented using SET statement.
- WS-STUDENT is the second dimension in the WS-CLASS array. WS-STUDENT(1,1) represents class1 first student information and WS-STUDENT(1,2) represents class1 second student information and so on.
- WS-ROLL-NO(1,1), WS-NAME(1,1), WS-MARKS-GRP(1,1) represents class1 first student roll number, name and marks. similarly, WS-ROLL-NO(1,2), WS-NAME(1,2), WS-MARKS-GRP(1,2) represents class1 second student roll number, name, marks and so on.
- IDX-TWO is index declared on WS-STUDENT 2nd dimension array.
- WS-MARKS is the third dimension in the array WS-CLASS. WS-MARKS(1,1,1) represents class1 first student subject1 marks, WS-MARKS(1,1,2) represents class1 first student subject2 marks and so on.
- IDX-THREE is index declared on WS-MARKS 3rd dimension array.
- WS-CLASS(1) represents the class1 informaton. i.e., two students information - WS-STUDENT(1,1), WS-STUDENT(1,2) and so on.