Arrays in COBOL
Our first example demonstrates the use of arrays in COBOL. In COBOL, an array is called a table and is a sequence of elements of a specific length.
In this COBOL program:
We define a table
A-TABLE
with 5 elements, each capable of storing a 3-digit number.We initialize all elements of
A-TABLE
to zero and display it.We set the 5th element of
A-TABLE
to 100, then display the whole table and the 5th element individually.We initialize
B-TABLE
with values 1 through 5 and display it.We create a two-dimensional table
TWO-D-TABLE
with 2 rows and 3 columns, initialize it with values, and display it.
Note that COBOL doesn’t have a built-in function to get the length of a table, so we manually display the length of A-TABLE
.
Also, COBOL doesn’t support dynamic array initialization like Go does. We have to explicitly set each value.
To run this COBOL program, you would typically compile it and then execute the resulting program. The exact commands may vary depending on your COBOL compiler and environment.
This example demonstrates basic array (table) operations in COBOL, including initialization, setting and getting values, and working with multi-dimensional tables.