Week 7
Week 7
Introduction
Introduction
In scientific and engineering computing, it is very common to
need to manipulate ordered sets of values, such as vectors and
matrices.
There is a common requirement in many applications to repeat
the same sequence of operations on successive sets of data.
1
A
Array Concept
In all the programs we have used one name to refer to one
location in the computers memory.
It is possible to refer a complete set of data obtained for example
from repeating statements, etc.
2
3
A31
A20
2
Array declarations
type, dimension(subscript bounds) ::
list_of_array_names
type, dimension(n:m) :: variable_name_list
Array declarations
Up to 7 dimensions
Number of permissible subscripts:
rank
Number of elements in a particular
dimension:
extent
Total number of elements of an array:
size
Shape of an array is determined by its rank
and the extent of each dimension
Examples
integer, dimension ( 10 ) : : arr
arr ( 1 )
arr ( 5 )
= 27
arr ( 7 )
= 12
arr ( 10 ) = 22
Array constructors
(/ value_1, value_2, /)
Initialization
EXAMPLE :
integer, dimension ( 5 ) : :
read *, value
read *, value(3)
value
Examples
real, dimension(5) :: p, q
integer, dimension(4) :: r
print *, p, q(3), r
read *, p(3), r(1), q
print *, p, q (3), q (4), r
print *,
q (2)
! displays the value in the 2nd location of the array q
print *,
real, dimension(20) :: a, b, c
.
.
a = b*c
do i = 1, 20
a(i) = b(i)*c(i)
end do
Example
integer, dimension ( 4 ) : :
a, b
integer, dimension ( 0 : 3 ) : :
integer, dimension ( 6 : 9 ) : :
a = (/ 1, 2, 3, 4 /)
b = (/ 5, 6, 7, 8 /)
c = (/ -1, 3, -5, 7 /)
will result
d = 2 * abs ( c ) + 1 !
a = (/ 6, 8, 10, 12 /)
will result
d = (/ 3, 7, 11, 15 /)
d(6) d(7) d(8) d(9)
Sub-arrays
consecutive
Sub-arrays
Subscript triplet:
subscript_1 : subscript_2 : stride
Simpler forms:
subscript_1 : subscript_2
subscript_1 :
subscript_1 : : stride
: subscript_2
: subscript_2 : stride
: : stride
:
Example
real, dimension ( 10 ) : :
arr
arr ( 1 : 10 )
! rank-one array containing all the elements of arr.
arr ( : )
! rank-one array containing all the elements of arr.
arr ( 3 : 5 )
! rank-one array containing the elements arr (3), arr(4), arr(5).
arr ( : 9 )
! rank-one array containing the elements arr (1), arr(2),., arr(9).
arr ( : : 4 )
! rank-one array containing the elements arr (1), arr(5),arr(9).
Example
integer, dimension ( 10 ) : :
integer, dimension ( 5 ) : :
b, i
integer : :
a = (/ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 /)
i = (/ 6, 5, 3, 9, 1 /)
b = a ( i ) ! will result
a ( 2 : 10 : 2 ) ! will result
a ( 1 : 10 : 2 ) = (/ j ** 2, ( j = 1, 5 ) /)
! will result
a = ( 1, 4, 9, 16, 25 /)
: :
i = 1, 3, 1
integer, dimension ( 3 ) : :
.
b = a(4:6)
! b (1 ) = a (4)
! b (2 ) = a (5)
! b (3 ) = a (6)
.
a(1:3) =0
a(1::2)=1
a(1::2)=a(2::2) + 1
! a(1) = a(2) +1
! a(3) = a(4) +1
! a(5) = a(6) +1
! etc.
result (maximum_array)
! dummy arguments
! do i = 1, size (array_2)
! maximum_array ( i ) = max ( array_1 ( i ), array_2 ( i ) )
! end do
end function
max_array
7.8
7.10