You are on page 1of 27

Introduction to

Fortran 90 - Arrays
What are Arrays?
 There are many examples of problems where
the data that are being considered have a
tabular structure.
 Some of the special names given to these
structures include:
✓Linear list.
✓List.
✓Vector.
✓Array.
 The term used most often, and in the majority
of books on Fortran programming, is array

CP 260 Lectures - Fortran Arrays 2


Examples of Arrays Month Rainfall
January 3.1
February 2.0
 Table of data, March 2.4
◦ Monthly rainfall April 2.1
May 2.2
◦ Examination results June 2.2
July 1.8
August 2.2
September 2.7
October 2.9
November 3.1
December 3.1
Name Physics Maths Biology History English French
Fowler L. 50 47 28 89 30 46
Barron L.W 37 67 34 65 68 98
Warren J. 25 45 26 48 10 36
Mallory D. 89 56 33 45 30 65
Codd S. 68 78 CP 260 Lectures
38 - Fortran 76
Arrays 98 65 3
Examples of Arrays
◦ Telephone directory
◦ Book catalogue
Name Address Number
Adcroft A. 61 Connaught Road, Roath, Cardiff 223309
Beale K. 14 Airedale Road, Balham 745 9870
Blunt R.U. 81 Stanlake Road, Shepherds Bush 674 4546
Sims Tony 99 Andover Road,Twickenham 898 7330
Author(s) Title Publisher
Carroll L. Alice through the Looking Glass Penguin
Steinbeck J. Sweet Thursday Penguin
Wirth N. Algorithms plus data Structures = Prentice-Hall
programs
CP 260 Lectures - Fortran Arrays 4
Arrays in Fortran
 There are three key things to consider:
✓ The ability to refer to a set or group of
items by a single name.
✓ The ability to refer to individual items or
members of a set, i.e., look them up.
✓ The choice of a control structure that allows
easy manipulation of a set or array.

CP 260 Lectures - Fortran Arrays 5


Average of 10 numbers – Option 1
Program avg1
Implicit none
Real:: x1,x2,x3,x4,x5,x6,x7,x8,x9,x10
Print*,’Enter the ten numbers’
Read(*,*) x1,x2,x3,x4,x5,x6,x7,x8,x9,x10 Tedious to write
Average=(x1+x2+x3+x4+x5+x6+x7+x8+x9+x10)/10
Print*,’The average is: ‘, average
Print* ‘The numbers are: ‘
Print*,x1
Print*,x2
Print*,x3
Print*,x4
Print*,x5
Tedious to write
Print*,x6
Print*,x7
Print*,x8 Accepts 10 numbers –
Print*,x9 no less or more
Print*,x10
end
Rigid program
CP 260 Lectures - Fortran Arrays 6
Average of 10 numbers – Option 2
Program avg2
Implicit none
Real, dimension (10):: x Easier to write
Real:: average, sum
Integer:: I
Sum=0
Print*,’Enter the ten numbers’
Do i=1,10
Read(*,*) x(i)
Sum=sum+x(i) Easier to write
end do
Average=sum/10
Print*,’The average is: ‘, average
Print* ‘The numbers are: ‘
Print*,x
end
CP 260 Lectures - Fortran Arrays 7
Average of 10 numbers – Option 3
Program avg3
Implicit none
Real::x(10)
Real:: average, sum
Integer:: i
Sum=0
Print*,'Enter the ten numbers'
Do i=1,10
Read(*,*) x(i)
Sum=sum+x(i)
end do
Average=sum/10
Print*,'The numbers are: '
write(*,'(i4,f10.2)')(i,x(i),i=1,10)
print*
Print*,'The average is: ', average
end

CP 260 Lectures - Fortran Arrays 8


Average of n numbers – Option 4
Program avg4
Implicit none
Real::x(100)
Real:: average, sum
Integer:: i,n
Sum=0
Print*,’Enter total numbers to be used’
Read(*,*) n
Print*,'Enter the’, n, ’ numbers'
Do i=1,n
Read(*,*) x(i)
Sum=sum+x(i)
end do
Average=sum/n
Print*,'The numbers are: '
write(*,'(i4,f10.2)')(i,x(i),i=1,n)
print*
Print*,'The average is: ', average
end
CP 260 Lectures - Fortran Arrays 9
Arrays
 Much of numerical work requires
manipulation of arrays
◦ so Fortran provides lots of tools for arrays
 arrays can be declared by using:
◦ a type declaration statement
◦ DIMENSION
◦ ALLOCATABLE
◦ TARGET
◦ or POINTER or COMMON

CP 260 Lectures - Fortran Arrays 10


Vector & Matrix in Fortran
Declaration of a 5ⅹ 5 matrix ‘A’ in Fortran
Columns (n)

1 2 3 4 5
1 6 11 16 21
1 4 10 1 6 2
A= 2 7 12 17 22 Integer A(5,5)
Rows (m)

2 8 1.2 9 4 25 Integer A(1:5,1:5)


3 8 13 18 23
3 7.2 5 7 1 11
4 9 14 19 24
4 0 0.5 4 5 56
5 10 15 20 25
5 23 83 13 0 10
CP 260 Lectures - Fortran Arrays 11
Arrays declaration
 for example
REAL MyArr(-1:29, 15:36)
 defines a 2 dimensional array whose
◦ first index takes on values [-1, 29]
◦ second index takes on values [15, 36]
 the above is equivalent to using:
REAL MyArr
Dimension MyArr(-1:29, 15:36)

CP 260 Lectures - Fortran Arrays 12


Array Terminology
• an array declared like this
REAL :: X( 2, 5, -1:8 )
• has:
• rank of 3 ! Number of dimensions of an array
• extents of 2, 5 and 10 ! Number of elements along a dimension
• a shape of (/ 2, 5, 10 /) ! vector of extents
• a size of 100(=product of extents) ! Total number of elements
• Bounds ! The upper and lower limits of the index in each
dimension
• Conformable ! Two arrays are said to be conformable if they have
the same shape, (i.e. they have the same rank and the same
extent in each dimension)
• the shape of matrix A is a vector with rank(A) elements
CP 260 Lectures - Fortran Arrays 13
Array Terminology
 arrays declared like this
REAL :: X( 4:5, -1:8 )
REAL :: Y( 2, 10 )
X Y
rank ! # of dimensions 2 2
extents ! # in each dim 2,10 2,10
a shape ! vector of extents /2,10/ /2, 10/
Size ! # of values in array 20 20

 X and Y arrays are said to be conformable


 Since the shapes are the same we can do
X = Sqrt(Y + 1.0)
→ X(4,-1) = Sqrt(Y(1,1) + 1.0)
CP 260 Lectures - Fortran Arrays 14
Activity
 Given arrays declared as follows
REAL :: X( 4:5, -1:8 )
REAL :: Y( 2, 10 ), Z(10)

 Which of these expressions are valid?

X = Y;
Z = Y(:,5) – 3.0
Y = Z + 1.0;
Y(3,:) = Z
X(4,:) = Z;
X(5,:) = Z+Y(2,:)
CP 260 Lectures - Fortran Arrays 15
Arrays
 Fortran stores higher
dimensional arrays as a
contiguous sequence of
elements Fortran: elements stored in order -

 It is important to know [a11 a21 a31 a21 a22 a32 a13 a23 a33 ]

that 2-dimensional
arrays are stored by
column
◦ referred to as column-
major order C: elements stored in order -

◦ C uses row-major order [a11 a12 a13 a21 a22 a23 a31 a32 a33 ]

CP 260 Lectures - Fortran Arrays 16


Array Sections
 define an array with
4 rows and 5
columns
Real x(1:4,1:5)
 x(1,1) is in the upper
left corner

CP 260 Lectures - Fortran Arrays 17


Array Sections
 the subscript triplet is used as follows
Real x(10,10), y(10,10), z(3,5)
z = x(2:4,2:10:2) + y(1:5:2,1:9:2)
 the array sections of x and y are both the same
size as z
 note that the correspondence is by position in
the extent, not by subscript value
◦ the order of scalar operations isn’t specified in the
Fortran standard to the compiler can arrange
optimizations

CP 260 Lectures - Fortran Arrays 18


Array Constructor
 An array constructor can be used to
create and assign values to rank-one
arrays (and array constants)
 The array constructor is denoted by:
(/ ... /)
 The ellipsis is a list of values assigned to
the array elements and is created by
expressions or implied do loops

CP 260 Lectures - Fortran Arrays 19


Array Constructor
 use a list of numbers to give values to an
array
Integer i ! Loop variable
Real :: x(4)
x = (/ 3, 6, 9, 12 /)
write(*,’(a,4F6.1)’)’X = ‘, x

 gives the output


X = 3.0 6.0 9.0 12.0

CP 260 Lectures - Fortran Arrays 20


Array Constructor
 Use an implied DO loop to give values to
an array
Integer i ! Loop variable
Real :: x(4)
x = (/ (i, i=1, 8, 2) /)
write(*,’(x,4F6.1)’)‘X = ‘,x

 Gives the output


1.0 3.0 5.0 7.0

CP 260 Lectures - Fortran Arrays 21


Arrays
 The fact that the array elements are stored
contiguously is part of the Fortran standard:
 This is from ISO/IEC 1539 : 1991 (E) section
14.6.3.1, item (6)
◦ A non-pointer array of intrinsic type or sequence derived
type occupies a sequence of contiguous storage
sequences, one for each array element, in array element
order (6.2.2.2)
 In ISO/IEC 1539-1:2004(E) this is found in
section 16.4.3.1, item (7)

CP 260 Lectures - Fortran Arrays 22


Arrays
 Why do we care about the array
elements being in contiguous memory
locations?
◦ This is what allows us to count on
optimizations which access array elements in
column order
do j = 1, ny
do i = 1, nx
y(i, j) = Do_Something( y(i,j) )
end do
end do

CP 260 Lectures - Fortran Arrays 23


Array Expressions
 can write expressions containing arrays
without explicitly writing do loops
Real x(10,10), y(10,10), z(10,10)
z = -x + 1.0 ! z(i,j)= - x(i,j)+1.0
z = x * y ! z(i,j)= x(i,j)*y(i,j)
z = x / y ! z(i,j)= x(i,j)/y(i,j)
z = Sin( x ) ! z(i,j)= Sin(x(i,j) )
 The above requires arrays of the same
shape

CP 260 Lectures - Fortran Arrays 24


Array Functions
 Many of Fortran intrinsic functions take
arrays as arguments
 Classify the functions as
◦ elemental – operate on array elements
 get an array back of the same dimensions
 y = SIN(x) ! sine of each element of x
◦ transformational
 get a number or array of a different shape back
 z = SUM(A) ! sums elements of A

CP 260 Lectures - Fortran Arrays 25


Program twodra
Implicit none
Integer, dimension (2,3):: a
Integer:: row, col, count
!creates an array with 3 columns and 2 rows
!sets col 1 to 1 col2 to 2 and so on
Do row=1,2
Count = 0
Do col=1,3
Count = count+1
a(row,col)=count
End do
End do
Do row=1,2
Do col=1,3 Do row=1,2
Print*, a(row,col) write(*,’(3i4)’) a(row,col), col=1,3)
End do End do
End do
End program twodra
CP 260 Lectures - Fortran Arrays 26
Statistical application of Arrays
 Standard deviation is calculated as
σ𝑛 ത
𝑋−𝑋(𝑖) 2
Std= 𝑖=1
𝑛

This requires application of arrays

CP 260 Lectures - Fortran Arrays 27

You might also like