CEE15
Introduction to Computing for Civil Engineers
59
Chapter 6
Array Processing
6.1 What is an Array?
An array is a collection of data of the
same
type. Array elements are
indexed
or
subscripted
, just like
x
1
,
x
2
, .
..,
x
n
in mathematics. An Array is an example of a
compound object
(
i.e.
, an object which can
have more than one value).
6.2 Important Components of an Array
A one-dimensional array has the following important components:
•
A
name
•
A
type
: this is the type of
all
array elements.
•
An
extent
: this is the
range
of the indices or subscripts of array elements. For example, the range
of an array can be 1 to 10 (
i.e.
, element 1, element 2, element 3, .
.., element 10) or -3 to 5 (
i.e.
,
element -3, element -2, .
.., element 4, element 5).
Indices or subscripts must be integers within the range. The smallest and the largest indices or
subscripts are referred to as the
lower
bound and the
upper
bound, respectively.
•
The extent of an array is simply the following:
smaller-integer
:
larger-integer
where
smaller-integer
and
larger-integer
are the lower bound and the upper bound of the extent. Thus,
if array indices are in the range of 0 and 11, the extent is
0:11
; if array indices are in the range of -3 and
21, the extent is
-3:21
. However, if the lower bound of an extent is 1, it can be omitted as well as the
colon following it.
6.3 Declaring an Array
There are two ways to declare an array:
(1)type, DIMENSION( extent ) :: name-1, name-2, .
.., name-n
§
type
is the type (real, integer, character or logical) of the arrays
name-1
,
name-2
, .
..,
name-n
§
DIMENSION
is a required keyword, and
extent
gives the range of the array indices.
(2)
type name-1,name-2, …, name-n
DIMENSION name-1(extent-1),name-2(extent-2), …, name-n(extent-n)
Examples:
REAL, DIMENSION(-1:1)
:: a, Sum
INTEGER, DIMENSION(0:100) :: InputData
•
The elements of arrays
a
and
Sum
are
REAL
and the indices are in the range of -1 and 1.
•
The elements of array
InputData
are
INTEGER ’S
and the indices are in the range of 0 and 100.
The integers in an extent can be
PARAMETER ’S
:
Examples:
INTEGER, PARAMETER :: MaximumSize = 100
LOGICAL, DIMENSION(1:MaximumSize) :: AnswerSheet
INTEGER, PARAMETER :: LowerBound = -10
INTEGER, PARAMETER :: UpperBound =
10
REAL, DIMENSION(LowerBound:UpperBound) :: Score, Mark