Array
1. DECLARATION
DCL days(365) FIXED DEC(4);
2. BOUNDS
Default LOWER BOUND is 1
We can also specify the upper and the lower bound
e.g. DCL TABLE(0:11) FIXED DEC(4,1);
Bounds may be constants, variables, expressions, or asterisks.
l Bounds that are variables or expressions are determined when the storage is allocated for the array.
l An asterisk means the bounds are defined later either in an ALLOCATE statement or via an argument to a subroutine or function procedure.
HELLO3 : PROC OPTIONS( MAIN );
DCL SAMPLE_ARRAY(500) FIXED BIN(31) INIT((500)7);
DCL DATA_SUM FIXED BIN(31) INIT(0);
CALL ODDSUM(SAMPLE_ARRAY,DATA_SUM);
PUT SKIP LIST('SUM IS',DATA_SUM);
ODDSUM : PROC(ARRAY,SUM);
DCL ARRAY(*) FIXED BIN(31);
DCL SUM FIXED BIN(31);
DCL HIGH FIXED BIN(15);
DCL LOW FIXED BIN(15);
DCL INDEX FIXED BIN(15);
DCL (HBOUND,LBOUND,MOD) BUILTIN;
LOW = LBOUND(ARRAY,1);
HIGH = HBOUND(ARRAY,1);
IF MOD(LOW,2) = 0 THEN
LOW = LOW + 1;
SUM = 0;
DO INDEX = LOW TO HIGH BY 2;
SUM = SUM + ARRAY(INDEX);
END;
END ODDSUM;
END HELLO3;
3. DIMENSIONS
The number of sets of upper and lower bounds specifies the dimensions in an array. e.g.
l DCL TABLE(6,2) FIXED DEC(5);
l DCL AXIS(-3:3,-4:4) FLOAT DEC(6) INIT(63(0));
l DCL POPULATION(2,50,10) FLOAT DEC(6);
4. SUBSCRIPTS
An element of an array is referred by means of a subscript. Range of subscripts is determined by the bounds of the array. e.g.
AMOUNT = PRICE * QTY * COMMISSION(3,1);
5. ARRAY MANIPULATION BUILTIN FUNCTIONS:
l DIM: It provides the current extent for a specified dimension in a given array
I = DIM(ARRAY_NAME,N);
l LBOUND: It finds the current lower boundary for a specified dimension of a given array.
I = LBOUND(ARRAY_NAME,N);
l HBOUND: It finds the current UPPER bound for a specified dimension of a given array.
I = HBOUND(ARRAY_NAME.N);
l SUM: It finds the sum of all elements in an array.
DCL AVERAGE FIXED DEC(3);
DCL GRADE(5) FIXED DEC(3) INIT(10,20,30,40,50);
AVERAGE = SUM(GRADE)/5; /* AVERAGE = 30 */
5. INIT
l If too many INITIAL values are specified then the excess values are ignored, whereas if not enough values are specified, remainder of the array is not initialized.
l Initial values specified for an array are assigned to successive elements of the array in the order where right hand subscript varies most rapidly.
DCL A(2,2) FIXED DEC (6) INIT(1,2,3,4); will result in
A(1,1) = 1, A(1,2) = 2, A(2,1) = 3, A(2,2) = 4.
l Each item in the INITIAL value may be a constant, an iteration specification, or a repetition factor. It has one of the following general forms.
((Iteration factor)constant); /* DCL A(25) FIXED DEC(6) INIT((25)0) */
((iteration factor)item,item….) /* DCL B(10) FIXED DEC(6) INIT((7)0,1,2,3) */
l If only one parenthesized decimal integer constant precedes a string initial value, it is interpreted as a repetition factor for the string. If two appear, the first is taken as the initialization iteration factor and the second a string repetition factor.
DCL TABLE(1) CHAR(10) INIT((2)’A’);
DCL TABLE(2) CHAR(10) INIT((2)(2)’A’);
DCL TABLE(10) CHAR(10) INIT((*)’A’);
6. ARRAY ASSIGNMENT:
Two types of move types may be specified for arrays:
l Scalar to scalar
An entire array is assigned to a single (scalar) value.e.g.
DCL MONTHS(12) FIXED DEC(4,1);
MONTHS = 0;
l Array to array.
One array can be moved to another array (provided the arrays have identical dimensions and bounds)
DCL A(5,5) FLOAT DEC(6);
DCL B(5,5) FLOAT DEC(6);
A = B;
7. DEBUGING TIPS.
l AGGREGATE compiler option.
It specifies that the compiler to include the aggregate length table. This table will specify the amount of storage required (length) for all arrays.
l SUBSCRIPTRANGE condition.
It can raised whenever there is a reference to an element outside the boundaries of an array.
DCL ARRAY(10) FLOAT DEC(6);
GET LIST(K);
ARAY(K) = J; /* IF K is > 10 condition is raised */