COBOL Table/Array:

COBOL Include Statement COBOL Table Subscript

Advertisements


A table/Array is a set of similar items which are combined logically and each item should has the similar data definition/description as the other items in the set.

In detail, an array/table contains the data items which are having same in type, length and declaration which are to be repeated/non-unique.

COBOL provides the above method to combine the similar data and can be referred easily.


Table structure example:



In the above table structure, the data will be represented by using the row and column number.

The data in the array also follows the same way to refer the data which is stored in the table.

The repeated data items can be declared by using the OCCURS Clause in COBOL. Let’s discuss what is the OCCURS clause.


COBOL OCCURES:

COBOL uses OCCURES clause to define a table/Array in the programming construction.

COBOL OCCURES clause specifies the number about how many times the data item to be repeated.

There are two parts in the table/array declaration. First one is the table name and the second one is data item.

In the above two, table name should be declared with 01 level number which will not have any OCCURES associated with it.

Data item should be declared with level numbers from 02 to 49 and OCCURES clause always associated with it.

But the PIC clause may not always come with OCCURES. Sometimes both declared for same data item if the user wants to use the same data item name for referring each data item in table.


How the table/Array Can be defined?


Let’s take the above table structure with one row and all five columns to see how the table will be defined in COBOL.


Table structure example:



Single dimensional Array

Before defining any data as a table in COBOL,

as a first step define the hierarchy to make the declaration easier.

As a second step, gather the information about which data is in occurrences.


1. For the table like above, the hierarchy would be


	            TABLE
		      |
		     \/
	             ROWS
		      |
		     \/
	           COLUMNS

2. The COLUMN has the occurrences of 5 and should be declared a 5 times during the declaration.

Based on the above hierarchy, the declaration would be like below


01 TABLE.
     02 ROW.
           03 COLUMN OCCURES 5 TIMES.
	  		04 DATA         PIC     X(05).

The above declaration can also be representing in below way by combining COLUMN and DATA.


01 TABLE.
     02 ROW.
           03 COLUMN-DATA PIC     X(05) OCCURES 5 TIMES.

The above two declarations are exactly same but the representation while declaration is different.

Note that 01 level number should not come with OCCURS clause.

Let’s use some real-time example to understand the above concept fully.


Problem:


We have a Btech first year student and need to capture his 6 subject’s marks in a first semester.

To resolve the above problem, follow the steps discussed earlier.


1. Define hierarchy.



	            BTECH-1ST-YEAR
		   	|
		  	\/
	             STUDENT
		   	|
		  	\/
	           SUBJECT-MARKS


2. Gather the information about data items which had the repeated occurrences.

SUBJECT-MARKS have occurrences of 6.


The declaration would be,

	01 BTECH-1ST-YEAR.
	     02 STUDENT.
	           03 SUBJECT-MARKS    PIC 9(03) OCCURES 6 TIMES.

Refer complete program for this example below.


Two dimensional Array


Structure:



To understand better, let’s take the above table completely and will see how the declaration would be

As we discussed above,

First step define the hierarchy to make the declaration easier.

Second step, gather the information about which data is in occurrences.


1. For the table like above, the hierarchy would be


	            TABLE
		   	|
		  	\/
	             ROWS
		   	|
		  	\/
	           COLUMNS


2. The ROW has 4 occurrences and COLUMN has the occurrences of 5. So ROW and COLUMN should be declared with the corresponding OCCURANCES during the declaration.


Based on the above hierarchy, the declaration would be like below

	01 TABLE.
	     02 ROW OCCURES 4 TIMES.
	           03 COLUMN OCCURES 5 TIMES.
		  		04 DATA         PIC     X(05).

Let’s use some real-time example to understand the above concept fully.


Problem:


We have a Btech first year with 60 students in a section and need to capture 6 subject’s marks in a first semester for all 60 students.

To resolve the above problem, follow the steps discussed earlier.


1. Define hierarchy.


	            BTECH-1ST-YEAR
		   	|
		  	\/
	             STUDENT
		   	|
		  	\/
	           SUBJECT-MARKS

2. Gather the information about data items which had the repeated occurrences.


STUDENT has occurrences of 60.

SUBJECT-MARKS have occurrences of 6.


The declaration would be,

	01 BTECH-1ST-YEAR.
	     02 STUDENT OCCURES 60 TIMES.
	           03 SUBJECT-MARKS    PIC  9(03) OCCURES 6 TIMES.

Refer complete program for this example below.


How the data will be stored in memory for table OCCURES?


The table occurs will be stored in subsequent memory locations.

Let’s take the above example to understand better.


01 BTECH-1ST-YEAR.
     02 STUDENT.
           03 SUBJECT-MARKS    PIC 9(03) OCCURES 6 TIMES

The above data will be shown to represent like below, if they are stored in subsequent memory locations.


Diagram:



In the above diagram, OCCURRENCES represents the number of times the data repeated.

A memory position (item level) represents the OCCURRENCE level memory representation. In the above example, each occurrence will occupy 3 bytes and ending byte of each item represented in the diagram.

Memory positions (Byte level) represent the Byte level memory representation.


Note:  The memory occupation will start from 1st byte in mainframe.


How the OCCURES can be referred during the programming constructions?


There are two ways to refer the OCCURRENCE element.

  1. Subscript
  2. Index

Let’s discuss about the two in detail.


1. Subscript:

Number of occurrences of array element is called as Subscript.

Take the above memory representation Diagram to understand better about Subscript.


	01 BTECH-1ST-YEAR.
	     02 STUDENT.
	           03 SUBJECT-MARKS    PIC 9(03) OCCURES 6 TIMES.

Subscript topic can be explained in details here


2. Index:

Number of displacement positions of an array known as Index.

Take the above memory representation Diagram to understand better about index.


01 BTECH-1ST-YEAR.
     02 STUDENT.
           03 SUBJECT-MARKS    PIC 9(03) OCCURES 6 TIMES.

Index topic can be explained in details here


Single Dimentional array example:


Example to use an array to calculate percentage of student


Code:


 IDENTIFICATION DIVISION.                                        
 PROGRAM-ID. PERFTIMI.                                           
 ENVIRONMENT DIVISION.                                           
 DATA DIVISION.                                                  
 WORKING-STORAGE SECTION.                                        
 01 STD-DET OCCURES 6 TIMES INDEXED BY STD-INDEX.                
    05 STD-MARKS              PIC 9(03).                         
 01 TOTAL-MARKS               PIC 9(03) VALUE ZERO.              
 01 STD-PERCENT               PIC 9(03).9(02).                   
 01 I                         PIC 9(01).                         
 PROCEDURE DIVISION.                                             
     MOVE ZEROES     TO TOTAL-MARKS.                             
     PERFORM VARYING I FROM 1 BY 1                                                      
       UNTIL I > 6                                               
        SET STD-INDEX TO 1                                       
        ACCEPT STD-MARKS (STD-INDEX)                             
        ADD STD-MARKS (STD-INDEX) TO TOTAL-MARKS                 
        SET STD-INDEX UP BY 1                                    
     END-PERFORM.                                                
     COMPUTE STD-PERCENT = TOTAL-MARKS/6.                        
     DISPLAY 'STUDENT PERCENTAGE : ' STD-PERCENT.                
     STOP RUN.                                                   


COBOL Include Statement COBOL Table Subscript

Advertisements