In this section, we will delve into multi-dimensional arrays in COBOL. Multi-dimensional arrays are a powerful feature that allows you to handle complex data structures efficiently. By the end of this section, you will understand how to declare, initialize, and manipulate multi-dimensional arrays in COBOL.
Key Concepts
- Definition: Multi-dimensional arrays are arrays of arrays, allowing for more complex data structures.
- Declaration: How to declare multi-dimensional arrays in COBOL.
- Initialization: Methods to initialize multi-dimensional arrays.
- Accessing Elements: Techniques to access and manipulate elements within multi-dimensional arrays.
- Practical Examples: Real-world scenarios where multi-dimensional arrays are useful.
Declaration of Multi-dimensional Arrays
In COBOL, multi-dimensional arrays are declared using the OCCURS clause. Here is the syntax for declaring a two-dimensional array:
01 MULTI-DIM-ARRAY.
05 FIRST-DIMENSION OCCURS 10 TIMES.
10 SECOND-DIMENSION OCCURS 5 TIMES.
15 ELEMENT PIC 9(2).Explanation
MULTI-DIM-ARRAY: The name of the array.FIRST-DIMENSION OCCURS 10 TIMES: The first dimension of the array, which can hold 10 elements.SECOND-DIMENSION OCCURS 5 TIMES: The second dimension of the array, nested within the first dimension, which can hold 5 elements.ELEMENT PIC 9(2): The data type and size of each element in the array.
Initialization of Multi-dimensional Arrays
Initialization of multi-dimensional arrays can be done using the VALUE clause or through procedural code. Here is an example of initializing a two-dimensional array:
01 MULTI-DIM-ARRAY.
05 FIRST-DIMENSION OCCURS 2 TIMES.
10 SECOND-DIMENSION OCCURS 3 TIMES.
15 ELEMENT PIC 9(2) VALUE ZEROS.Procedural Initialization
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 2
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 3
MOVE 0 TO ELEMENT (I, J)
END-PERFORM
END-PERFORM.Explanation
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 2: Outer loop to iterate over the first dimension.PERFORM VARYING J FROM 1 BY 1 UNTIL J > 3: Inner loop to iterate over the second dimension.MOVE 0 TO ELEMENT (I, J): Initializes each element to zero.
Accessing Elements in Multi-dimensional Arrays
Accessing elements in a multi-dimensional array involves specifying the indices for each dimension. Here is an example:
Explanation
MOVE 25 TO ELEMENT (1, 1): Assigns the value 25 to the element at the first row and first column.DISPLAY ELEMENT (1, 1): Displays the value of the element at the first row and first column.
Practical Example
Let's consider a practical example where we use a two-dimensional array to store and display a multiplication table.
Code Example
IDENTIFICATION DIVISION.
PROGRAM-ID. MULTIPLICATION-TABLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MULTI-DIM-ARRAY.
05 ROW OCCURS 10 TIMES.
10 COLUMN OCCURS 10 TIMES.
15 ELEMENT PIC 99.
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 10
COMPUTE ELEMENT (I, J) = I * J
END-PERFORM
END-PERFORM.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 10
DISPLAY ELEMENT (I, J) WITH NO ADVANCING
IF J < 10 THEN
DISPLAY " " WITH NO ADVANCING
ELSE
DISPLAY ""
END-IF
END-PERFORM
END-PERFORM.
STOP RUN.Explanation
- Data Division: Declares a two-dimensional array
MULTI-DIM-ARRAYwith 10 rows and 10 columns. - Procedure Division:
- First Loop: Fills the array with the product of the row and column indices.
- Second Loop: Displays the multiplication table.
Exercises
Exercise 1: Initialize and Display a 3x3 Identity Matrix
Task: Write a COBOL program to initialize a 3x3 identity matrix and display it.
Solution:
IDENTIFICATION DIVISION.
PROGRAM-ID. IDENTITY-MATRIX.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MATRIX.
05 ROW OCCURS 3 TIMES.
10 COLUMN OCCURS 3 TIMES.
15 ELEMENT PIC 9 VALUE 0.
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3
MOVE 1 TO ELEMENT (I, I)
END-PERFORM.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 3
DISPLAY ELEMENT (I, J) WITH NO ADVANCING
IF J < 3 THEN
DISPLAY " " WITH NO ADVANCING
ELSE
DISPLAY ""
END-IF
END-PERFORM
END-PERFORM.
STOP RUN.Explanation
- Data Division: Declares a 3x3 matrix initialized to zero.
- Procedure Division:
- First Loop: Sets the diagonal elements to 1.
- Second Loop: Displays the matrix.
Summary
In this section, we covered the following key points about multi-dimensional arrays in COBOL:
- How to declare multi-dimensional arrays using the
OCCURSclause. - Methods to initialize multi-dimensional arrays both declaratively and procedurally.
- Techniques to access and manipulate elements within multi-dimensional arrays.
- Practical examples and exercises to reinforce the concepts.
Understanding multi-dimensional arrays is crucial for handling complex data structures in COBOL, and mastering these concepts will significantly enhance your programming skills.
