Presentation on theme: "Introduction to arrays"— Presentation transcript:
1Introduction to arrays Chapter 6Introduction to arrays
2What 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, 100a(i) = SQRT(a(i))END DORather thana1 = SQRT(a1)a2= SQRT(a2)…a100 = SQRT(a100)a ( 3 )a ( 1 )a ( 2 )a ( 4 )a ( 5 )Computer MemoryArray a
3Arrays 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.
5Declaring Arrays Define type and size Array constant REAL, DIMENSION(16) :: voltageINTEGER, DIMENSION(5, 6):: matrix, valuesCHARACTER(len=20), DIMENSION(50) :: last_nameFind rank, extent, shape and size of above arrays.Array constant(/ 3, 4, 5, 1, 3/)
6Array exampleDeclares 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) :: studentIDWRITE(*,*) ‘ ID of first student: ’, studentID(1)WRITE(*,*) ‘ ID of 25th student: ’, studentID(25)
7Array exampleMake 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) :: occupiedINTEGER, DIMENSION (7,15) :: personsWRITE(*,*) ‘Is room 9 in floor 6 occupied ? ’, occupid(6,9)WRITE(*,*) ‘persons in room 2 at floor 7 are: ’, persons(7,2)
8Using 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) :: studentIDstudentID(5)=999999IF (studentID(5)==999999) studentID(4)=888888studentID(6) = MAX(studentID(4), studentID(5))WRITE(*,*) ‘ ID of sixth student: ’, studentID(6)
9Initialization of array elements Un-initialized arrayINTEGER, DIMENSION(10) :: jWRITE (*,*) ‘ j(1) = ’, j(1)Initialization with assignment statementREAL, DIMENSION(10) :: array1DO I = 1, 10array1(i) = REAL(i)END DOInitialization with assignment using array constructorarray1 = (/1., 2., 3., 4., 5., 6., 7., 8., 9., 10./)Possible to assign one value to all array elements.array1 = 0.
10Example-1 PROGRAM squares IMPLICIT NONE INTEGER :: i INTEGER, DIMENSION(10) :: number, squareDO i=1, 10number(i)=isquare(i)=number(i)**2END DOWRITE(*,*) number(i), ' ', square(i)END PROGRAM
11Initialization of array elements Named constants in array declarationINTEGER, PARAMETER :: max_size=1000INTEGER, DIMENSION(max_size) :: array1REAL, DIMENSION(2*max_size) :: array2Do i = 1, max_sizearray2(i) = array1(i) * 2.0 / 3.0END DO
12Initialization of array elements Initializing with READ statementINTEGER, DIMENSION(5) :: array1Do i=1,5READ(*,*) array1(i)END DOInitializing 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 sizeDeclaring arrays using implied loopsINTEGER, DIMENSION(100) :: array1 = ( / ( i , i=1,100) / )General form of implied loop (arg1, arg2, …, index = istart, iend, incr)
13Example-2 PROGRAM square_roots IMPLICIT NONE INTEGER :: i INTEGER, DIMENSION(10) :: value = (/ (i, i = 1, 10) /)INTEGER, DIMENSION(10) :: s_rootDO i=1, 10s_root(i) = SQRT(value(i))END DOWRITE(*,*) values(i), ' ', s_root(i)END PROGRAM
15Whole array operations REAL, DIMENSION(4):: a = (/ 1, 2, 3, 4 /)REAL, DIMENSION(4):: b = (/ 2, 2, 1, 0 /)REAL, DIMENSION(4):: cDO i = 1, 4c(i) = a(i) + b(i)END DOWRITE(*,*) c(i)2134+=abcc = a + bWRITE(*,*) ‘ c = ’, cOutput :c =
16Out of bound subscripts REAL, DIMENSION(5) :: arrayarray should have 5 values numbered 1 to 5Any 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.
17Example-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=0DO i=1, 6total = total + (2*x(i)+a(i))END DOWRITE (*,*) 'Total = ', totalEND PROGRAM_____________________________________________What does this program do?
18Changing the Subscript Range of an Array REAL, DIMENSION(5):: arrElements arr(1), arr(2), arr(3), arr(4), arr(5)Change rangeREAL, 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
19Out of bounds INTEGER, DIMESION(5):: arr Do i=1,5 WRITE(*,*) arr(i) END DOINTEGER, DIMESION(-2:2):: arrDo i=-2,2
22Vector SubscriptThe 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
27Two-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 dimensionb is the index in the second dimensionDeclaring 2-D arrayREAL, DIMENSION(3,6) :: arr1REAL, DIMENSION(-1:9, 0:19) :: arr2INTEGER, DIMENSION(10, 0:19) :: arr3
302-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)
322-D array example-1Write 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.
332-D array example-2Write 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.
34Matrix OperationsWrite 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