ARRAYS COMPUTER PROGRAMMING By Zerihun Alemayehu AAiT.CED Rm. E119B
BASIC RULES AND NOTATION An array is a group of variables or constants, all of the same type, that are referred to by a single name. Array elements are indexed or subscripted, just like x 1, x 2,..., x n in mathematics. Array Elements Array subscript
BASIC RULES AND NOTATION Array Declaration REAL, DlMENSION (10):: X Declares X to be an array (or list) with 10 real elements, denoted by X(1), X(2),...,X(10). The number of elements in an array is called its size (10 in this case). Each element of an array is a scalar (singlevalued).
BASIC RULES AND NOTATION REAL, DIMENSION (0:100): : A declares A to have 101 elements, from A(0) to A(100). The upper bound must be specified; if the lower bound is missing it defaults to 1. REAL, DIMENSION (2, 3) : : A A is a two-dimensional array. The number of elements along a dimension is called the extent in that dimension. A has an extent of 2 in the first dimension, and an extent of 3 in the second dimension(and a size of 6).
ONE AND TWO DIMENSIONAL ARRAY Array1 (row) Array2 (row, col)
BASIC RULES AND NOTATION Fortran allows up to seven dimensions The number of dimensions of an array is its rank, and the sequence of extents is its shape. The shape of A is (2, 3), or (2 x 3) in matrix notation. A scalar is regarded as having a rank of zero. An array with a rank of one is called a vector An array with a rank of 2 or greater is called a matrix
BASIC RULES AND NOTATION REAL, DIMENSION (5): : A, B(2,3), C(10)! Only A is (1:5) INTEGER :: I (10), K (4, 4), L (5)! All different DIMNESION key word can be omitted Array constants (/ 1, 2, 5, 6, 9 /)! In FORTRAN 95 [ 1, 2, 5, 6, 9 ]! In FORTRAN 2003
BASIC RULES AND NOTATION An array subscript can be used as the control variable in a DO loop to generate array elements. DO I = 1, 5 X(I) = 2 * I END DO Integer, dimension(5), parameter:: i=6! Assigns a constant value of 6 to all the five elements of i READ*, X! An entire array may be read X = 1! An entire array assigned a scalar value
BASIC RULES AND NOTATION a = i = 100 y i i= 1 This expression represents the sum of all the elements stored in the subscripted variable y and the statement stores the result in a. REAL,DIMENSION(100):: Y READ*, Y A=0.0 DO I = 1,100 A = A + Y(I) END DO
ARRAY STORAGE The array element ordering: conceptualise a linear sequence of the array elements with the first index changing first. REAL, DIMENSION(3, 5) :: a
ARRAY ASSIGNMENT Whole array assignment REAL, DIMENSION(100) :: a, b, c REAL :: d(10,10) = 0.0 b = 2*a+4 a = c = b*a c = d! Illegal, the two arrays are not conformable
ARRAY ASSIGNMENT Array section assignment alpha beta alpha REAL, DIMENSION(10) :: alpha, beta alpha = beta = alpha + alpha(1:10:2) = beta(1:5) * 1 2 3 4 5 6 4.0 4.0 4.0 4.0 4.0 4.0 8.0 8.0 8.0 7 8 9 10 4.0 4.0 4.0 4.0 8.0 8.0
INITIALIZING ARRAY Constructors array = (/ list /) INTEGER :: a(6)=(/ 1, 2, 3, 6, 7, 8 /) variable expression(s) REAL :: b(2)=(/ SIN(x), COS(x) /) array expression(s) INTEGER :: c(5)=(/ 0, a(1:3), 4 /)
INITIALIZING ARRAY Implied DO loops (value list, variable = start, end [, step]) REAL :: d(100)=(/ REAL(i), i=1,100 /)!1 up to 100 INTEGER :: A (6) = (/ (I, I = 1, 2), J = 1, 3) /) the same as (/ 1, 2, 1, 2, 1, 2 /)
INITIALIZING ARRAY RESHAPE used for the initialization or assignment of multidimensional arrays RESHAPE (list, shape [,PAD] [,ORDER]) INTEGER, DIMENSION(2,3) :: a a=reshape((/i,i=0,5/),(/3,2/)) RESHAPE (SOURCE = (/1, 2, 3, 4, 5, 6/), SHAPE = (/2, 3/)) -- -- forms the (2 x 3) matrix 1 3 5 2 4 6
INITIALIZING ARRAY DATA statement DATA variable / list /... INTEGER :: a(4), b(2,2), c(3,3) DATA a/4,3,2,1/ DATA a/4*0/ DATA b(1,:)/0,0/ DATA b(2,:)/1,1/ DATA ((C(I,J), J= 1, 3), I=1, 3) /3*0, 3*1, 3*2/
WHERE STATEMENT used when the value of an element depends on the outcome of some condition. The WHERE statement allows a single array assignment only if a logical condition is true. WHERE (condition) statement INTEGER :: a(2,3,4) WHERE(a< 0) a=0 WHERE(a*3>10) a=999
WHERE STATEMENT The WHERE construct allows array assignment(s) only if a logical condition is true, and alternative array assignement(s) if false. WHERE (condition) block1 [ELSEWHERE block2] ENDWHERE INTEGER :: b(8,8) READ*, b WHERE (b<=0) b=0 ELSEWHERE b=1/b ENDWHERE
WHERE EXAMPLE Write a WHERE statement which replicates every nonzero element of an array beta by its reciprocal and every zero element by 1. INTEGER :: beta(8,8) READ*, b WHERE (beta == 0) beta=1 ELSEWHERE beta=1/beta ENDWHERE
ARRAYS: EXPERIMENTAL WORK Write a program that calculates dot product of two vectors a and b which is defined by. z = a b i = = = i Give values of a and b at the beginning of the program. And the result onto screen. 3 1 a i b i
ARRAYS: EXPERIMENTAL WORK Real, Dimension (3) :: a,b Real:: z=0.0 Read*, a Read*, b Do I =1,3 z=z+a(i)*(b(i) End do Print*, The dot product =, z
ALLOCATABLE (DYNAMIC) ARRAYS REAL, DIMENSION(:), ALLOCATABLE: : X Although its rank is specified, it has no size until it appears in an ALLOCATE statement, such as: ALLOCATE(X (N)) DEALLOCATE(X)!freeing up the memory used.
ARRAY EXAMPLE Write a program which first reads the number of people sitting an exam. It should then read their marks (or scores) in to an array and print the highest and lowest marks, followed by the average mark for the class.
SOLUTION Declare a dynamic array Read the no. of students, n Allocate the size of the array n Read all the marks Use do loop to find the max and min Calculate the sum Calculate the average Print the result
SOLUTION REAL, DIMENSION (:) :: mark INTEGER :: n PRINT*, Give me the no. of students READ*, n ALLOCATE (mark (n)) PRINT*, Give me the marks READ*, mark max_grade = MAXVAL (mark) min_grade = MINVAL (mark) avg = SUM(mark)/n DEALLOCATE (mark) PRINT *, Maximum =, max, Minimum =, min, average=, avg