# Introduction to arrays

## Presentation on theme: "Introduction to arrays"— Presentation transcript:

Introduction to arrays
Chapter 6 Introduction to arrays

What is an Array? Group of variables or constants, all of the same type, referred to by a single name. Array values occupy consecutive memory locations in the computer. Single value in an array called array element and referred to by the array name and a subscript (of type integer) that is pointing to its location in the group. Arrays allow a powerful computation that apply the same algorithm to many different items with a simple DO loop. Example: Find square root of 100 different real numbers. DO i= 1, 100 a(i) = SQRT(a(i)) END DO Rather than a1 = SQRT(a1) a2= SQRT(a2) a100 = SQRT(a100) a ( 3 ) a ( 1 ) a ( 2 ) a ( 4 ) a ( 5 ) Computer Memory Array a

Arrays Arrays can be multi-dimensions.
Number of subscripts called rank. Extent of array in one dimension is the number of values in that given dimension of the array. Shape of the array is the combination of its rank and the extent in each dimension. Size of array is the total number of elements in all dimensions.

Arrays One dimension array Vector(i) Rank: Extent: Shape: Rank, extent
Size: Two dimension array Vector(i , j) Dimension - 2 Dimension - 1 Rank: Extent: Shape: Rank, extent Size: 1 15 2 5 , 4 20 Dimension - 1 Dimension - 1 Dimension - 2 Dimension - 3 Three dimension array Vector(i, j, k) Rank: Extent: Shape: Rank, extent Size: 3 5, 4, 3 60

Declaring Arrays Define type and size Array constant
REAL, DIMENSION(16) :: voltage INTEGER, DIMENSION(5, 6):: matrix, values CHARACTER(len=20), DIMENSION(50) :: last_name Find rank, extent, shape and size of above arrays. Array constant (/ 3, 4, 5, 1, 3/)

Array example Declares an array that stores the student ID of all students in the class. (43 students) Make a WRITE statement that makes the program display the first student in the class. Make a WRITE statement that makes the program display the student number 25 in the list. INTEGER, DIMENSION(43) :: studentID WRITE(*,*) ‘ ID of first student: ’, studentID(1) WRITE(*,*) ‘ ID of 25th student: ’, studentID(25)

Array example Make two arrays that represent a building that consists of 7 floors. Each floor has 15 rooms. First array values indicates if the room is occupied or not. (True or False) The second array indicates how many persons in the room in case it is occupied. (Integer) Please, write to the customer if room number 9 in the 6th floor is occupied or not. Please, write how many persons in room 2 in the 7th floor. LOGICAL, DIMENSION (7,15) :: occupied INTEGER, DIMENSION (7,15) :: persons WRITE(*,*) ‘Is room 9 in floor 6 occupied ? ’, occupid(6,9) WRITE(*,*) ‘persons in room 2 at floor 7 are: ’, persons(7,2)

Using array elements in FORTRAN
Each element in the array is treated as any regular FORTRAN variable. FORTRAN statements can access its values, assign values and make operations on them. Back to student ID example: INTEGER, DIMENSION(43) :: studentID studentID(5)=999999 IF (studentID(5)==999999) studentID(4)=888888 studentID(6) = MAX(studentID(4), studentID(5)) WRITE(*,*) ‘ ID of sixth student: ’, studentID(6)

Initialization of array elements
Un-initialized array INTEGER, DIMENSION(10) :: j WRITE (*,*) ‘ j(1) = ’, j(1) Initialization with assignment statement REAL, DIMENSION(10) :: array1 DO I = 1, 10 array1(i) = REAL(i) END DO Initialization with assignment using array constructor array1 = (/1., 2., 3., 4., 5., 6., 7., 8., 9., 10./) Possible to assign one value to all array elements. array1 = 0.

Example-1 PROGRAM squares IMPLICIT NONE INTEGER :: i
INTEGER, DIMENSION(10) :: number, square DO i=1, 10 number(i)=i square(i)=number(i)**2 END DO WRITE(*,*) number(i), ' ', square(i) END PROGRAM

Initialization of array elements
Named constants in array declaration INTEGER, PARAMETER :: max_size=1000 INTEGER, DIMENSION(max_size) :: array1 REAL, DIMENSION(2*max_size) :: array2 Do i = 1, max_size array2(i) = array1(i) * 2.0 / 3.0 END DO

Initialization of array elements
Initializing with READ statement INTEGER, DIMENSION(5) :: array1 Do i=1,5 READ(*,*) array1(i) END DO Initializing arrays in declaration (OK for small arrays) INTEGER, DIMENSION(5) :: array1 =(/1,2,3,4,5/) Number of values must be equal to the array size Declaring arrays using implied loops INTEGER, DIMENSION(100) :: array1 = ( / ( i , i=1,100) / ) General form of implied loop (arg1, arg2, …, index = istart, iend, incr)

Example-2 PROGRAM square_roots IMPLICIT NONE INTEGER :: i
INTEGER, DIMENSION(10) :: value = (/ (i, i = 1, 10) /) INTEGER, DIMENSION(10) :: s_root DO i=1, 10 s_root(i) = SQRT(value(i)) END DO WRITE(*,*) values(i), ' ', s_root(i) END PROGRAM

More Implied loops (arg1, arg2, …, index = istart, iend, incr)
INTEGER, DIMENSION(25) :: array = (\ ((0, i=1, 4), 5*j, j=1,5) \) (arg1, arg2, …, index = istart, iend, incr) array = 0, 0, 0, 0, 5, 0, 0, 0, 0, 10, 0, 0, 0, 0, 15, 0, 0, 0, 0, 20, 0, 0, 0, 0, 25 INTEGER, DIMENSION(25) :: array = (\ ((j, 2*j, 3*j, j=1,3) \) array = 1, 2, 3, 2, 4, 6, 3, 6, 9

Whole array operations
REAL, DIMENSION(4):: a = (/ 1, 2, 3, 4 /) REAL, DIMENSION(4):: b = (/ 2, 2, 1, 0 /) REAL, DIMENSION(4):: c DO i = 1, 4 c(i) = a(i) + b(i) END DO WRITE(*,*) c(i) 2 1 3 4 + = a b c c = a + b WRITE(*,*) ‘ c = ’, c Output : c =

Out of bound subscripts
REAL, DIMENSION(5) :: array array should have 5 values numbered 1 to 5 Any other subscript out of these bounds (1, 5) will result in an out of bounds error either detected by compiler (if compiler bounds check is turned on) or at run time.

Example-3 PROGRAM summation IMPLICIT NONE INTEGER :: i
REAL, DIMENSION(6) :: x=(/ 1., 2., 3., 4., 5., 6./) REAL, DIMENSION(6) :: a=(/ .1, .1, .1, .2, .2, .2/) REAL :: total=0 DO i=1, 6 total = total + (2*x(i)+a(i)) END DO WRITE (*,*) 'Total = ', total END PROGRAM _____________________________________________ What does this program do?

Changing the Subscript Range of an Array
REAL, DIMENSION(5):: arr Elements arr(1), arr(2), arr(3), arr(4), arr(5) Change range REAL, DIMESION(-2:2) Elements arr(-2), arr(-1), arr(0), arr(1), arr(2) What is he sixze of the following array? REAL, DIMENSION(-1:19) Size = upper – lower +1 = = 21

Out of bounds INTEGER, DIMESION(5):: arr Do i=1,5 WRITE(*,*) arr(i)
END DO INTEGER, DIMESION(-2:2):: arr Do i=-2,2

arr(subscript1 : subscript2: increment)
Array subset arr(subscript1 : subscript2: increment) INTEGER, DIMENSION(10):: a=(/1., -2., 3., -4., 5., -6., 7., -8., 9., -10./) Find the following subsets: arr(:) arr(3:7) arr(3:7:2) arr(3:7:7) arr(:6) arr(5:) arr(::2)

Example INTEGER, DIMENSION(3):: x=(/2, 5, 8/)
INTEGER, DIMENSION(5):: y=(/1, 3, 2, 4, 7/) INTEGER, DIMENSION(2):: z z = x(1:2) + y(3:4) WRITE(*,*) z z = 4, 9

Vector Subscript The subset in previous slides used subscript triplets array(a:b:c) Vector subscript is using a vector or array as index values of another array. INTEGER, DIMENSION(5):: vec=(/1,6,4,1,9/) REAL, DIMENSION(10):: a=(/1, -2, 3, -4, 5, -6, 7, -8, 9, -10/) WRITE(*,*) a(vec) Output: a(1), a(6), a(4), a(1), a(9) 1, -6, -4, 1, 9

Subset Assignment INTEGER, DIMENSION(5):: vec=(/1,6,4,1,9/)
REAL, DIMENSION(10):: a=(/1, -2, 3, -4, 5, -6, 7, -8, 9, -10/) REAL, DIMENSION(10):: b=(/1, -2, 3, -4, 5, -6, 7, -8, 9, -10/) a(2:8:2)=(/1, -1, 1, -1/) b(vec)=(/1,-1,1, -1, 1/) WRITE(*,*) ‘a = ’, a WRITE (*,*) ‘b = ’, b Output : a = 1, 1, 3, -1, 5, 1, 7, -1, 9, -10 b = -1, -2, 3, 1, 5, -1, 7, -8, 1, -10

Simple Output Format INTEGER :: a=10 REAL :: b=5.1 WRITE(*,*) a, b
100 FORMAT(I2, F10.8) Output: 100 FORMAT(I2, 1X, F10.8) 100 FORMAT(I2, 3X, F3.1) Output: 100 FORMAT(' a= ',I2, 3X, 'b= ', F3.1) Output: a= 10 b= 5.1

Input/output of array elements
REAL, DIMENSION(5) :: a= (/0.5,-1,0.1,2., 10.4/) WRITE(*,*) (a(i), i=1, 5) Output: Do i=1,5 WRITE(*,*) a(i) END DO Output: WRITE(*,100) a(1), a(2), a(3), a(4), a(5) 100 FORMAT (1X, 'a =', 5F7.2) Output: a = WRITE(*,100) (a(i), i=1, 5) 100 FORMAT (F10.2) 0.50 -1.00 0.10 2.00 10.40 WRITE(*,100) (a(i), i=1, 5) 100 FORMAT (5F10.2) Output:

Input/Output with implied loops
WRITE(unit, format)(arg1, ar2,.., index=istart, iend, incr) READ(unit, format)(arg1, ar2,.., index=istart, iend, incr) WRITE(*, *) (i, 2*I, 3*I, i=1,3) Output: WRITE(*,100) (a(i), i=1, 5) 100 (1X, ‘a= ’, 5F7.2) WRITE(*,200) ((i,j, j=1,3), i=1, 2) 200 FORMAT(1X, I5, 1X, I5) Output: 1 1 1 2 1 3 2 1 2 2 2 3

Two-Dimensional Array
Rank-2 array require 2 subscripts as an address to retrieve one value. array(a,b) a is the index in the first dimension b is the index in the second dimension Declaring 2-D array REAL, DIMENSION(3,6) :: arr1 REAL, DIMENSION(-1:9, 0:19) :: arr2 INTEGER, DIMENSION(10, 0:19) :: arr3

Initializing 2-D arrays
Using DO loops INTEGER, DIMENSION(4,3):: arr DO i=1, 4 DO j=1, 3 arr(i, j)= j END DO Using Constructor arr=(/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /) Memory arrangement in computer (/ arr(1,1), arr(2,1), arr(3,1), arr(4,1), arr(1,2), arr(2,2), arr(3,2), arr(4,2), arr(1,3), …, arr(4,3) /) Is array shape correct?? NO that is 1-D array arr=RESHAPE( (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /), (/4, 3/)) 2 3

Initializing 2-D arrays
Initializing in Declaration INTEGER, DIMENSION(4,3):: arr=RESHAPE ( (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /), (/4,3/)) Initializing using READ READ(*,*) arr READ(*,*) ((arr(i,j), j=1,3), i=1,4)

2-D array subsets arr(:, 1) arr(1, :) arr(1:3, 1:4:2) arr(1:4:2, :)
list = (/ 1, 2, 4/) arr(:, list) How to access the third column? How to access the fourth row? How to access the second and fourth columns together? arr(:,3) arr(4,:) arr(:,2:4:2)

2-D array output 2 3 1 2 3 WRITE (*,*) arr 1 1 1 1 2 2 2 2 3 3 3 3
2 3 WRITE (*,*) arr DO j=1, 3 DO i=1, 4 WRITE (*,*) arr(i, j) END DO WRITE (*,*) ((arr(i,j), i=1,4), j=1,3) WRITE (*,*) (arr(i, j), j=1,3) 1 2 3 1 2 3

2-D array example-1 Write a program that initializes a 3X4 matrix as shown below. The program should make search for minimum and maximum values in the 2-D array. Repeat the above program but let the user now enter the values of the matrix via the keyboard.

2-D array example-2 Write a program that initializes a 3X4 matrix as shown below. The program should count how many positive numbers, negative numbers and zeros in the matrix. Repeat the above program but let the user now enter the values of the matrix via the keyboard.

Matrix Operations Write a program that accepts two matrices from the user and store them in two 2-D arrays. Then, the program prompt the user to select the operation he is seeking by entering 1 for matrix addition, 2 for matrix subtraction, and 3 for matrix multiplication. Addition and subtraction are done element by element for two matrices that are having the same size. The multiplication is done for two matrixes where the number of columns in the first matrix is equal to the number of rows in the second one. C = A + B (MXN) = (MXN) + (MXN) C = A - B (MXN) = (MXN) - (MXN) C = A X B (MXN) = (MXL) + (LXN) Multiplication can be done as follows