lmayer@elgin.edu

ARRAYS

Arrays

Amount of storage to be reserved in main memory must be declared.
DIM statement - allocation of storage

DIM  [SHARED] Arrayname(size)
lower bound is 0 unless otherwise stated

The OPTION BASE statement allows you to control the lower bound
OPTION BASE 1
DIM Month(12)

The OPTION BASE statement can only be used once in a program and must proceed the DIM statement.
Affects only array without lower bounds.

Or you can use the TO option to set the lower bound
DIM Month (1 TO 12)

Dynamic Allocation:

Size = 50
DIM Arrayname (Size)

Lower = 1
Upper = 10
DIM Array2 (Lower TO Upper)

Referencing Array elements:
Subscript to the array name references a particular element of the array

DIM Month(1 TO 12)
Month (4)

Variables can be used as subscripts.
This is useful in loops, for example:

‘Monthly Sales Analysis

‘*******************

DIM Month(1 TO 12)

CLS ‘clear screen

‘***Read Monthly Sales into Month

FOR Number = 1 TO 12

   Read Month (Number)

NEXT Number

‘*** Display Monthly Sales

FOR Number = 1 TO 12

   PRINT Month (Number)

NEXT Number

'Monthly Sales Analysis

'*****************************

DIM Month.sales(1 TO 12)

CLS  'clear screen

Total.sales = 0

'*** Read in monthly sales and sum

FOR Number = 1 TO 12

   Read Month.sales (Number)

   Total.sales = Total.sales + Month.sales (number)

NEXT Number

"*** Compute and display average sales

Avg.sales = Total.sales/12

PRINT USING "The average of monthly sales is $$##,###.##"; Avg.sales

PRINT

'*** Print Monthly Sales in four columns

FOR Number = 1 TO 12 STEP 4

   PRINT Month.sales (Number), Month.sales (Number+1), Month.sales (Number +2),      Month.sales (Number +3

NEXT Number

PRINT

PRINT "Job Complete"

 

Parallel arrays are two or more arrays with corresponding elements