0% found this document useful (0 votes)
65 views

What Is An Array?

Arrays allow for the efficient storage and manipulation of related data. An array is a collection of data elements of the same type, where each element can be uniquely accessed via an index. One-dimensional arrays store elements in a linear fashion, while multi-dimensional arrays can represent tables or matrices with multiple indices to access each element. Arrays are declared with a variable name followed by parentheses containing the lower and upper bounds of each dimension. Individual elements, whole arrays, or sections of arrays can then be referenced using indices within the parentheses.

Uploaded by

heshambm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

What Is An Array?

Arrays allow for the efficient storage and manipulation of related data. An array is a collection of data elements of the same type, where each element can be uniquely accessed via an index. One-dimensional arrays store elements in a linear fashion, while multi-dimensional arrays can represent tables or matrices with multiple indices to access each element. Arrays are declared with a variable name followed by parentheses containing the lower and upper bounds of each dimension. Individual elements, whole arrays, or sections of arrays can then be referenced using indices within the parentheses.

Uploaded by

heshambm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Arrays

Many problems that we encounter lend themselves to


the idea of subscripted variables. Most numerical
methods have subscripted variables; for instance, take
the Runge-Kutta method for solving ordinary differential
equations:

What is an Array?
• An array is a collection of data of the same type.
Array elements are indexed or subscripted like x1, x2,
. . .. xn in mathematics.

1
1-D Arrays
• By now, I’m sure you have run across cases or
examples where you could have seen a use for
indexed variable names
• We do this with Fortran arrays
• Here is an example of the simplest type of array is a
1-D array: y(i+1) = 2.0*y(i)

When a valid Fortran identifier (variable name) is


followed immediately by a parenthesis, Fortran
recognizes and treats this as an array subscript.

Example
How x is stored
implicit none in computer
memory
integer x(10)
INTEGER i
.
do i=1,10 .
.
x(i) = i
end do
WRITE(*,*) x x(1)
end x(2)
x(3)
.
Program output: .
.

x(10)
1 2 3 4 5 6 7 8 9 10
.
.
.

2
Example
An obvious example of the usefulness of arrays is when you need to read in a lot of
related variables, such as temperature measurements taken at regular intervals
over time.

Without arrays, you would have to do something like this:

read(*,*) temp1
read(*,*) temp2
read(*,*) temp3
.
.
.
read(*,*) temp49
read(*,*) temp50

With arrays, the same thing can be accomplished much more easily:

real temp(50)
do i=1,50
read(*,*) temp(i)
end do

Declaration of Arrays
There are several ways to declare arrays:
real temp(50) or
real :: temp(50)
real, dimension temp(50) or
real, dimension :: temp(50)
real temp
dimension temp(50)

integer parameter n=50


real temp(n), pressure(n), wind_speed(n)

3
The DIMENSION Attribute
• A Fortran 90 program uses the DIMENSION attribute
to declare arrays.
• The DIMENSION attribute requires three components
in order to complete an array specification,
– rank,
– shape, and
– extent.
• The rank of an array is the number of “indices” or
“subscripts.” The maximum rank is 7 (i.e., seven-
dimensional).
• The shape of an array indicates the number of
elements in each “dimension.”
7

The DIMENSION Attribute


• The rank and shape of an array is represented as
(s1,s2,…,sn), where n is the rank of the array and si
(1≤ i ≤ n) is the number of elements in the i-th
dimension.
• A(7) means array A with rank 1 and with 7 elements
• A(5,9) means a rank 2 array (i.e., a table) whose first
and second dimensions have 5 and 9 elements,
respectively.
• (10,10,10,10) means a rank 4 array that has 10
elements in each dimension.

4
The DIMENSION Attribute
• The extent is written as m:n, where m and n (m ≤ n)
are Integers. We saw this in the SELECT CASE,
substring, etc.
• Each dimension has its own extent.
• An extent of a dimension is the range of its index. If
m: is omitted, the default is 1.
• A(-3:2) means possible indices are A(-3),A(-2) ,A(-1),
A(0),A(1),A(2)
• A(5:8) means possible indices are 5,6,7,8
• A(7) means possible indices are 1,2,3,4,5,6,7

Dimensions of Arrays
Examples: real temp(50)

which creates an array in which the first subscript is 1, and the last is 50, giving a
total of 50 values in the array.
However, arrays are not limited to a value of 1 as the lower subscript.
The general form is:

real temp(L:U)

where: L is the lower limit of the array


U is the upper limit of the array

Note: L must be less than U.

10

10

5
Example

integer x(-5:5) x(-5) = -5


do i=-5,5 x(-4) = -4
x(i) = i x(-3) = -3
end do x(-2) = -2
end x(-1) = -1
x(0) = 0
x(1) = 1
x(2) = 2
x(3) = 3
x(4) = 4
x(5) = 5

11

11

2-D Arrays
2-D arrays are a simple extension of the 1-D array concept:

real x(5,5)

produces a matrix of 5 rows and 5 columns:

x(1,1) x(1,2) x(1,3) x(1,4) x(1,5)


x(2,1) x(2,2) x(2,3) x(2,4) x(2,5) As you can see, the first
subscript denotes rows,
x(3,1) x(3,2) x(3,3) x(3,4) x(3,5)
and the second subscript
x(4,1) x(4,2) x(4,3) x(4,4) x(4,5) denotes columns.

x(5,1) x(5,2) x(5,3) x(5,4) x(5,5)

12

12

6
Use of Array
• Fortran 90 has, in general, three different ways to use
arrays: referring to
– individual array element, 
write(*,*) x(3), write(*,*) x(3,5)

– referring to the whole array, and  write(*,*) A


– referring to a section of an array.
 write (*,*) velocity(3:6) , write(*,*) flow(2:4, 3:5)

• The first one is very easy. One just starts with the array
name, followed by () between which are the indices
separated by ,.
• Note that each index must be an INTEGER or an
expression evaluated to an INTEGER, and the value of
an index must be in the range of the corresponding 13
extent. But, Fortran 90 won’t check it for you
13

Example Use of Array


• Suppose we have the following declarations
INTEGER, PARAMETER :: L_BOUND = 3, U_BOUND = 10
INTEGER, DIMENSION(L_BOUND:U_BOUND) :: x

DO i = L_BOUND, U_BOUND DO i = L_BOUND, U_BOUND


x(i) = i IF (MOD(i,2) == 0) THEN
END DO x(i) = 0
ELSE
x(i) = 1
END IF
END DO

array x() has 3,4,5,…, 10 array x() 1 0 1 0 1 0 1 0

14

14

7
Example Use of Array
Suppose we have the following declarations:
INTEGER, PARAMETER :: L_BOUND = 3, U_BOUND = 10
INTEGER, DIMENSION(L_BOUND:U_BOUND, &
L_BOUND:U_BOUND) :: a
DO i = L_BOUND, U_BOUND DO i = L_BOUND, U_BOUND
DO j = L_BOUND, U_BOUND DO j = i+1, U_BOUND
A(i,j) = 0 t = a(i,j)
END DO a(i,j) = a(j,i)
a(i,i) = 1 a(j,i) = t
END DO END DO
END DO

Swapping the lower and


generate an identity matrix upper diagonal parts
(i.e., the transpose of a
15
matrix)

15

Allocatable Arrays
• The default array type is compile-time
– array size is set in program
– if the program doesn’t need as much storage as
what is declared, then the program is wasting
memory storage
– if the program needs more storage than what is
declared, it may fail
• Fortran 90 introduces allocatable arrays
– allocatable arrays are run-time
– array size is set during the running of the program,
not at compile time
– allows arrays to be sized just right for the
particular application
16

16

8
Allocatable Arrays
• Allocatable arrays are distinguished from compile-time
arrays in two ways:
– a special declaration statement
– an allocate statement, in which the array size is
actually set
– a deallocate statement is also available, to return
memory to the pool

17

17

Declaration of Allocatable Array

Form:
type, DIMENSION(:), ALLOCATABLE :: list

or

type, DIMENSION(:) :: list


ALLOCATABLE :: list

where

list is a list of array names.

Purpose:
Declares that each of the identifiers in the list is an array whose size will
be specified during execution.

18

18

9
ALLOCATE Statement

Form: Note: ALLOCATE is an


ALLOCATE(list) executable statement –
it can go anywhere in
or the program.
ALLOCATE(list, STAT=status-variable)

where
list is a list of array specifications of the form:
array-name(l:u)
where the pair l:u is a pair of integer expressions, and
status-variable is an integer variable.
Purpose:
Allocates space for each array listed. The range of subscripts is given by
l:u. If l is one, only u need be specified (without the :). In the second
form, status-variable will be set to zero if allocation is successful, but will be
assigned some system-dependant error value if there is insufficient
memory, or if the array has already been allocated.

19

19

DEALLOCATE Statement

Form: Note: DEALLOCATE is


DEALLOCATE(list) an executable
statement – it can go
or anywhere in the
DEALLOCATE(list, STAT=status-variable) program.

where
list is a list of names of currently allocated arrays
status-variable is an integer variable

Purpose:
Releases the memory allocated to the arrays listed. In the second form, the
integer variable status-variable will be set to zero if deallocation is
successful, but it will be assigned some system-dependent error value if it is
not successful, for example, if no memory was previously allocated to an
array.

20

20

10
Example
implicit none
INTEGER, DIMENSION(:), ALLOCATABLE :: x
INTEGER n, errstat, i
WRITE(*,*) "enter size for array x: "
READ(*,*) n
ALLOCATE(x(n))
do i=1,n
x(i) = i
end do enter size for array x: 10
do i=1,n
WRITE(*,*) x(i) 1
end do 2
DEALLOCATE(x) 3
end 4
5
Output: 6
7
8
9
10
Program Completed
Press Enter to Continue. 21

21

Array Constructors
• Rather than assign array values one by one, it is
convenient to give an array a set of values using an
array constructor.

• An array constructor is a sequence of scalar values


defined along one dimension only.

• An array constructor is a list of values, separated by


commas and delimited by the pair of two-character
symbols ``(/'' and ``/)''.

22

22

11
Array Constructors
• There are three possible forms for the array
constructor values:
1. A scalar expression as in
X (1:4) = (/ 1.2, 3.5, 1.1, 1.5 /)

2. An array expression as in
X (1:4) = (/ A (I, 1:2), A (I+1, 2:3) /)
X(1)= A(I,1)
X(2)= A(I,2)
X(3)= A(I+1,2)
X(4)= A(I+1,3)

3. An implied do loop as in
x (1:4) = (/ (sqrt (real (i)), i = 1, 4) /) 23
x (1:4) = (/sqrt(y(i)),i=5,8)/)
23

Y(5:8)= (/4,9,16,25/)
Do i=5,8
x (1:4) = sqrt(y(i))
enddo

24

24

12
Array Constants
An array constant may be constructed of a list of values enclosed between (/ and /):

(/value1, value2, … , valuek /)

where each valuek is a constant expression. For example:

(/1,2,3,4,5,6,7,8,9,10/)

is a one-dimensional array constant of size 10 consisting of the first 10 integers.

This method can be tedious if you have very many constants to assign to an array,
so there is a method that automates this, called the implied-do:

(/(i,i=1,10)/)

does exactly the same as above, but automates the process with the implied-do.

25

25

Examples
integer :: a(10)
a = (/1,2,3,4,5,6,7,8,9,10/)

integer :: a(10) All four of these do


a = (/(i,i=1,10)/) exactly the same thing,
which is to assign the
integer values 1,2,…,10
integer :: a(10) to the integer array a of
a = (/1,2,(i,i=3,10)/) size 10.

integer :: a(10)
do i=1,10
a(i) = i
end do

26

26

13
There is a simple way to assign a constant value to an entire array:

integer :: a(100)
a = 0

assigns the value zero to EACH MEMBER of the array a. This is typically used to
initialize the values of array variables.

It is always a good idea to initialize all the values of all your array variables
(usually to zero for integer or real arrays, and to “ “ (a blank) for character
arrays).

This is because initial values of arrays are undefined, and if you perform logical
tests on array values that are undefined, results may be unpredictable.

27

27

Array Expressions
Operators and functions normally applied to simple expressions may also be applied
to arrays having the same number of elements and to arrays and simple expressions.
The operations to an array are carried out elementwise.

Example: implicit none implicit none


INTEGER a(5), b(5), c(5) INTEGER a(5), b(5), c(5)
a = (/1,2,3,4,5/) a = (/1,2,3,4,5/)
b = (/6,7,8,9,10/) b = (/6,7,8,9,10/)
c = a + b c = a * b
WRITE(*,*) c WRITE(*,*) c
end end

Output: 7 9 11 13 15 6 14 24 36 50

The numbers in a are added The numbers in a are multiplied


to the numbers in b, by the numbers in b,
elementwise. elementwise.

28

28

14
Array Sections and Subarrays
Sometimes it is necessary to construct arrays by selecting elements from another
array, called a parent array. These new arrays, called array sections or
subarrays, are constructed by using expressions of the form:

array-name(subscript-triplet)
or
array-name(vector-subscript)

A subscript triplet has the form:

lower : upper : stride

and specifies the elements in positions lower, lower + stride, lower +


2*stride, … going as far as possible without going beyond upper if stride >
0, or below upper if stride < 0. If lower is omitted, the lower bound in the
array declaration is used; if upper is omitted, the upper bound in the array
declaration is used; if stride is omitted, it is taken to be 1.

29

29

Example

implicit none
INTEGER a(10), b(10), i
a = (/(i,i=1,10)/) assigns integer values 1-10 to a
b = a(10:1:-1)
has the effect of reversing the values
WRITE(*,*) a
WRITE(*,*) b
end

1 2 3 4 5 6 7 8 9 10
output
10 9 8 7 6 5 4 3 2 1

30

30

15
for array-name(vector-subscript)

A vector subscript is a sequence of subscripts of the parent array.

Example: implicit none


INTEGER b(10),i
CHARACTER a(10)
a = (/'A','B','C','D','E','F','G','H','I','J’/)
b = (/2,3,4,5,6,7,8,9,10,1/)
WRITE(*,*) a
DO i=1,10
a = a(b) assigns the 2nd element of a to the 1st element of a
WRITE(*,*) a assigns the 3rd element of a to the 2nd element of a
END DO assigns
.. the 4th element of a to the 3rd
end .
assigns the 10th element of a to the 9th element of a
assigns the 1st element of a to the 10th element of a
Output: ABCDEFGHIJ
BCDEFGHIJA
CDEFGHIJAB
DEFGHIJABC
EFGHIJABCD
FGHIJABCDE
GHIJABCDEF
HIJABCDEFG
IJABCDEFGH
JABCDEFGHI
ABCDEFGHIJ

31

31

As an aside, note that the same thing (in this case) can be accomplished with the
Fortran intrinsic function cshift:

implicit none
INTEGER i
CHARACTER a(10)
a = (/'A','B','C','D','E','F','G','H','I','J'/)
WRITE(*,*) a
DO i=1,10
a = CSHIFT(a,1)
WRITE(*,*) a
cshift(a,n) shifts the members
END DO of the array a by n places. If n is
end positive, the shift is to the left; if n is
negative, the shift is to the right.
Output: ABCDEFGHIJ
Array elements “pushed” to the left
BCDEFGHIJA
CDEFGHIJAB of the first member are re-inserted
DEFGHIJABC at the end of the array for n > 0.
EFGHIJABCD
FGHIJABCDE Array elements “pushed” to the right
GHIJABCDEF of the last member are re-inserted at
HIJABCDEFG the beginning of the array for n < 0.
IJABCDEFGH
JABCDEFGHI
ABCDEFGHIJ

32

32

16
Input/Output with Arrays
I/O with arrays is greatly facilitated by the implied do-loop.

Consider the following problem:

implicit none 1
INTEGER i, a(50) 2
do i=1,50 which produces 3
.
.
a(i) = i the output: .
48
end do
49
do i=1,50 50
WRITE(*,*) a(i)
end do

33

33

Using an implied do-loop to write out the array:

implicit none
INTEGER i, a(50)
do i=1,50
a(i) = i
end do
WRITE(*,*) (a(i),i=1,50)
end

produces the following output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

Note that there is not a line break after the number 30, the line just wrapped
because of the size of the window that it was originally written to.

In the previous case, the write statement was encountered 50 times, and so there
were 50 new lines. But with the implied-do, the effect is that the write statement is
only encountered once, thus all the values that are written are written without line
breaks, unless you put them in with a format statement.
34

34

17
This makes it easy to create nice looking output:

implicit none
INTEGER i, a(50) Notice that the first 1x in the
do i=1,50 format statement must go inside
a(i) = i the repeat parenthesis, because
end do there must be a blank at the
WRITE(*,10) (a(i),i=1,50) beginning of each line to control
10 FORMAT(10(1x,i2,1x)) line spacing properly.
end

Output: 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50

35

35

Implied-dos can be nested, just like regular do-loops.

In this example, assume that the output on the previous page is saved in a file
called “inputa.txt”. This program reads the values in, and prints them out in the
same form:

implicit none
INTEGER i,j, a(5,10)
OPEN(UNIT=10,FILE="inputa.txt")
READ(10,*) ((a(i,j),i=1,5),j=1,10)
WRITE(*,10) ((a(i,j),i=1,5),j=1,10)
10 FORMAT(10(1x,i2,1x))
end

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
Output: 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50

36

36

18
WHERE Statement

Form:
WHERE (logical-array-expr) array-var1 = array-expr1
or
WHERE (logical-array-expr)
array-var1 = array-expr1
.
.
.
array-varm = array-exprm
ELSEWHERE
array-varm+1 = array-exprm+1
.
.
.
array-varn = array-exprn
END WHERE
Where:
logical-array-expr is a logical-valued array expression and each array-vari has
the same size as the value of logical-array-expr; in the second form, the
ELSEWHERE part is optional.
Purpose:
Evaluates logical-arry-expr element by element. Whenever the expression is true,
assigns the value of the corresponding element of each array-expri to the corresponding
element of array-vari. All other elements are left unchanged in the first form.

38

38

Example
Now let’s use the where construct to re-write the random number program:

Program Output
implicit none 0.43 0
REAL x(10)
0.45 0
INTEGER jump(10),i
!You have(x) with random numbers
0.91 1
where (x > 0.5) 0.48 0
jump = 1 0.72 1
elsewhere 0.73 1
jump = 0 0.90 1
end where 0.47 0
do i=1,10
0.10 0
WRITE(*,10) x(i), jump(i)
10 FORMAT(1x,f5.2,1x,i1)
0.33 0
end do
end

Because the where contruct promises to operate on the whole array in a


consistent manner, the compiler can take advantage of CPU-dependant
accelerations such as vectorization and parallel processing (e.g. multiple CPUs).

39

39

19
Let’s examine this program more carefully:

implicit none x is a real array of 10 elements.


REAL x(10) jump is an integer array of 10 elements.
INTEGER jump(10),i
!You have(x) with random numbers The call to random_number fills the
array x with 10 random numbers,
where (x > 0.5)
uniformly distributed between 0 and 1.
jump = 1
elsewhere The where construct examines each
jump = 0 element in x.
end where
do i=1,10 When the ith x element is greater than
WRITE(*,10) x(i), jump(i) 0.5, the expression is true, and the ith
10 FORMAT(1x,f5.2,1x,i1) jump element is set to 1.
end do
When the ith x element is less than or
end equal to 0.5, the expression is false, and
the ith jump element is set to 0.

40

40

Fortran 90 Intrinsic Array-Processing


Subprograms
ALLOCATABLE(A) Returns true if memory has been allocated to
the allocatable array A and false otherwise.
DOT_PRODUCT(A,B) Returns the dot product of (numeric or logical)
arrays A and B
MAXVAL(A) Returns the maximum value in array A

MAXLOC(A) Returns a one-dimensional array containing


one element whose value is the position of the
first occurrence of the maximum value in A
MINVAL(A) Returns the minimum value in array A

MINLOC(A) Returns a one-dimensional array containing


one element whose value is the position of the
first occurrence of the minimum value in A
PRODUCT(A) Returns the product of the elements in A

SIZE(A) Returns the number of elements in A

SUM(A) Returns the sum of the elements in A

41

41

20
Fortran 90 Intrinsic Array-Processing
Subprograms
MAXVAL
• Returns the maximum value of all elements in an array, a
set of elements in an array, or elements in a specified
dimension of an array.
• Syntax
• result = MAXVAL (array [, dim] [, mask])
• array (Input) Must be an array of type integer or real.
• dim (Optional; input) Must be a scalar integer expression
with a value in the range 1 to n, where n is the rank of
array.
• mask (Optional; input) Must be a logical array that is
conformable with array.
42

42

Fortran 90 Intrinsic Array-Processing


Subprograms
• Results:
• The result is an array or a scalar of the same data type as
array.
– The result is scalar if dim is omitted or array has rank
one.

43

43

21
Fortran 90 Intrinsic Array-Processing
Subprograms
• Examples
1. The value of MAXVAL ((/2, 3, 4/)) is 4 because that is the
maximum value in the rank-one array.

2. MAXVAL (B, MASK=B .LT. 0.0) finds the maximum value of


the negative elements of B.
3. C is the array
[234]
[567]
– MAXVAL (C, DIM=1) has the value (5, 6, 7), 5 is the
maximum value in column 1; 6 is the maximum value in
column 2; and so forth.
– MAXVAL (C, DIM=2) has the value (4, 7). 4 is the maximum
value in row 1 and 7 is the maximum value in row 2.
44

44

Fortran 90 Intrinsic Array-Processing


Subprograms
• MINLOC
• Syntax
• result = MINLOC (array [, dim] [, mask] )
• array (Input) Must be an array of type integer or real.
• dim (Optional; input) Must be a scalar integer with a value in
the range 1 to n, where n is the rank of array. This
argument is a Fortran 95 feature.
• mask (Optional; input) Must be a logical array that is
conformable with array.

45

45

22
Fortran 90 Intrinsic Array-Processing
Subprograms
• Results:
• The result is an array of type default integer.
• The following rules apply if dim is omitted:
• The array result has rank one and a size equal to the rank of
array.
• If MINLOC(array) is specified, the elements in the array result
form the subscript of the location of the element with the
minimum value in array. The ith subscript returned lies in the
range 1 to ei, where ei is the extent of the ith dimension of array.
• If MINLOC(array, MASK=mask) is specified, the elements in the
array result form the subscript of the location of the element with
the minimum value corresponding to the condition specified by
mask.
46

46

Fortran 90 Intrinsic Array-Processing


Subprograms
• Examples
• The value of MINLOC ((/3, 1, 4, 1/)) is (2), which is the subscript of the
location of the first occurrence of the minimum value in the rank-one
array.
• A is the array
[ 4 0 -3 2 ]
[ 3 1 -2 6 ]
[ -1 -4 5 -5 ].
• MINLOC (A, MASK=A .GT. -5) has the value (3, 2) because these are the
subscripts of the location of the minimum value (-4) that is greater than
-5.
• MINLOC (A, DIM=1) has the value (3, 3, 1, 3). 3 is the subscript of the
location of the minimum value (-1) in column 1; 3 is the subscript of the
location of the minimum value (-4) in column 2; and so forth.
• MINLOC (A, DIM=2) has the value (3, 3, 4). 3 is the subscript of the
location of the minimum value (-3) in row 1; 3 is the subscript of the
location of the minimum value (-2) in row 2; and so forth.
47

47

23
Assignment
1) Write a program to read values from an input data
file and to sort them into ascending order.

2) Write a program to read in values from an input


data file and to calculate their mean, median, and
standard deviation.

3) Solve Laplace equation which presents 2-D


groundwater flow steady state equation

48

48

24

You might also like