Arrays

One Dimensional Arrays

Scalar Variables

•     all types of variables seen so far are scalar

–   each variable holds a single data item

–   character variables contain many characters but a single string of characters

•     number of variables to be declared would be very large if number of data items is large

•     many times, we have large amount of data of a  similar nature

•     preferable to group similar data together

Examples

•     names of students in this course

–   each name is a character string of length 20

–   500 students in the course

–   require 500 variables to be declared

•     marks of students

–   500 integers to be declared

•     preferable to declare one variable which can hold 500 different values

Array

•     arrays group many data items in one variable

•     abstractly, an array a is a finite sequence of variables a1,……, an   called its elements

•     ith element is denoted by a(i)

•     all variables a(i) have the same type

•     type of array is the type of its elements

•     size of array is the total number of elements

•     type and size (a constant) of array is declared in a declaration statement

 

 

Array Declarations

integer, dimension(500) :: marks

 

•     declares marks to be a sequence of 500 integer variables marks(1) to marks(500)

•     an element of the array accessed by its index, also called subscript

•     index may be any integer expression

•     marks(i) represents different variables depending on value of i

character(len=20),dimension(0:50) :: name

•     declares name to be a sequence ( or array) of 51 character variables name(0) to name(50)

•     array index can have lower and upper bound

–   lower bound assumed 1 if not given

–   any index value within the bounds is valid

•     name(5)(6:15) denotes a substring of the 6th element of array name

Using Arrays

integer, dimension(500) :: marks

integer :: i, n ! number of students

read *, n

do i = 1, n

print *, “type marks of”, i, “th student”

read *, marks(i) ! separate read for each student

end do

! all the marks are read into array marks

!if  n > 500, marks(n) is undefined

! an error occurs, should be checked and reported

! find mean and variance of marks

sum = 0 ; sum_square = 0

do i = 1, n

sum = sum + marks(i)

sum_square = sum_square + marks(i)**2

end do

mean = real(sum)/real(n)

variance = real(sum_square)/real(n) – mean**2

! simpler ways of doing this without using array

! use arrays only when necessary        

Input Output of Arrays

•      complete arrays maybe read or printed

read *, a

print *, a

–   values appear on a single line

•     implied do loops may also be used

read *, (a(i), i = 1,n)

–   short notation for do loop

–   values of a(1) to a(n) read on one line

–   more flexible

 

Array Assignment

•     individual elements of an array can be assigned values, some may be undefined

•     an array can be assigned to another provided both arrays have the same type and size

–   index bounds may be different

•     an expression of same type can be assigned to entire array – all elements get same value

•     array constants defined using array constructors may be used

integer, dimension(50) :: a

integer, dimension(51:100) :: b

! a and b have the same type and size

a(1) = 5

a(10) = a(1)*a(1)

read *, b            ! read 50 integer values b(51)..b(100)

a = b               ! b(50+i) is assigned to a(i), 1<=i<=50

! all elements of b must have a value assigned

print *, b                 ! prints 50 integer values in a line

a = b(51) ! assigns  b(51) to all elements of array a

Array Initialization

•     array constructor

•     defines an array constant

–   used for array initialization

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

•     initializes array a to 0,1,2,3,4,5,6,7,8,9

•     implied do loop can also be used

a = (/(2*i, i=1,10)/) ! i must be declared before use

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

! a is  1,1,3,3,5,5,7,7,9,9

Array Sections

•     like substrings of strings, it is possible to form  sections of a given array

•     section specified by a triplet i: j: k

–   i is the lower bound on index (default value array lower bound)

–   j is the upper bound (default value array upper bound)

–   k is the increment ( default value 1)

•     i, j, k must be integer expressions

integer, dimension(50) :: a

•     a(1:10:2) is an array containing the elements a(1) a(3) a(5) a(7) a(9)  in this order

•     a(10:40) contains elements a(10) to a(40)

•     a(5 : : 10) contains a(5) a(15) …… a(45)

•     an array and its section share storage

•     array section can be used in array operations

•     a(5:1) is a null section

read *, a(: : 2)           ! reads variables with odd index

a(2: : 2) = a( : : 2)

! assigns a(2i) the value of a(2i-1).  is it always correct?

temp = a(1)

a(:49) = a(2:)

a(50) = temp                     ! cyclically shifts to left by 1

! at least one : must be present in a section

! a(1:1) is not the same as a(1)

a(1:1) = a(2) ! assigns a scalar to an array

! has same effect as a(1) = a(2)

Operations on Arrays

•     operations on an array depend on its type

•     arithmetic operations for integer, real arrays

•     operations performed element wise

–   result assigned to array of same size

•     arrays must be of the same size

–   bounds may be different

–   mixing of real and integer arrays is possible

•     scalar operations are also allowed

Arithmetic Operations

integer, dimension(50) :: a, c

integer, dimension(51:100) :: b

read *, a, b                     ! reads 100 values

c = 2*a + b + 5          ! c(i) = 2*a(i) + b(50+i) + 5

•     operation is applied to corresponding elements  in “parallel”

•     similar to component wise vector addition

•     other arithmetic operations (except **) applied similarly

•     scalar operation performed on each element

Relational Operators

•     arrays of the same type and size can be compared if their elements can be

•     result is an array of logical type of same size

•     can also be compared to scalars

integer, dimension(50) :: a,b

logical, dimension(50) :: c

c = (a < b) .and. ( a > a(1))

! c(i) is .true. iff a(i) < b(i) and a(i) > a(1), 1<=i <=50

! similarly for other relational operators

Masking

•     array operations can be done with a mask

•     mask is a logical array of the same size

•     operations performed only for those elements for which corresponding mask is true

where(a < 0) a = -a

! changes sign of all negative elements

•     mask and arrays used in the operations must have same size

where(a < b) a = b ! computes max(a,b)

Intrinsic Functions

•     many intrinsic functions  available for arrays

•     intrinsic functions applicable to integers and reals can be applied to integer or real arrays

–   function applied to all elements separately

•     size(a)

–   number of elements in array a

•     maxval(a) and minval(a)

–   maximum and minimum value in integer or real array a

•     sum(a), product(a)

–   sum or product of elements of integer or real array a

•     dot_product(a,b)

–   dot product of two vectors of same size

•     using array operations and intrinsic functions makes program more readable

•     it can be more efficient also as operations can be done in “parallel”

 

Mean and Variance

print *, “type number of students”

read *, n

print *, “type marks of students”

! a is assumed to be an array with lower bound = 1

! assume size(a) >= n

read *, a(:n)

! a(:n) is the section of the array actually used

mean = sum(a(:n))/real(n)

variance = sum(a(:n)*a(:n))/real(n)-mean**2

Summary

•     arrays group variables under a single name

•     individual variables, called array elements, accessed using array name and index

•     type of array is the type of its elements

•     array declared using dimension attribute

•     declaration specifies index values and type

•     arrays assigned values using array operations

•     array operations must use arrays of same size

•     sections of arrays can be used in operations

•     array constants defined by array constructors

•     implied do loops used for input, output

•     mask can be used for array operations

•     intrinsic functions for operations on arrays

•     array operations can make a program clearer as well as more efficient