chapter 6 introduction to arrays. what is an array? group of variables or constants, all of the same...
TRANSCRIPT
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, 100a(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.
Rank:
Extent:
Shape: Rank, extent
Size:
1 , 1 1 , 41 , 31 , 2
2 , 1 2 , 42 , 32 , 2
3 , 1 3 , 43 , 33 , 2
4 , 1 4 , 44 , 34 , 2
5 , 1 5 , 45 , 35 , 2
Two dimension array
Vector(i , j)
Dimension - 2
Dim
ensi
on -
1
Arrays1
2
3
4
12
13
14
15
One dimension array
Vector(i)
Rank:
Extent:
Shape: Rank, extent
Size:
1
15
15
2
5 , 4
20
Dim
ensi
on -
1
1, 1 , 3 1, 4, 31, 3, 31, 2, 3
2, 1, 1 2, 4, 32, 3, 1 2, 2, 1
3, 1 , 1 3, 4, 33, 3, 13, 2, 1
4, 1, 1 4, 4, 34, 3, 14 , 2, 1
5, 1, 1 5, 4, 35, 3, 15, 2, 1
1, 1 , 2 1, 4, 21, 3, 21, 2, 2
2, 1, 1 2, 4, 22, 3, 1 2, 2, 1
3, 1 , 1 3, 4, 23, 3, 13, 2, 1
4, 1, 1 4, 4, 24, 3, 14 , 2, 1
5, 1, 1 5, 4, 25, 3, 15, 2, 1
1, 1 , 1 1, 4, 11, 3, 11, 2, 1
2, 1, 1 2, 4, 12, 3, 1 2, 2, 1
3, 1 , 1 3, 4, 13, 3, 13, 2, 1
4, 1, 1 4, 4, 14, 3, 14 , 2, 1
5, 1, 1 5, 4, 15, 3, 15, 2, 1
Dim
ensi
on -
1
Dimension - 2
Dimen
sion -
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
REAL, DIMENSION(16) :: voltageINTEGER, DIMENSION(5, 6):: matrix, valuesCHARACTER(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 arrayINTEGER, DIMENSION(10) :: j
WRITE (*,*) ‘ j(1) = ’, j(1)
• Initialization with assignment statementREAL, DIMENSION(10) :: array1
DO I = 1, 10
array1(i) = REAL(i)
END DO
• Initialization with assignment using array constructorREAL, DIMENSION(10) :: array1
array1 = (/1., 2., 3., 4., 5., 6., 7., 8., 9., 10./)
• Possible to assign one value to all array elements.REAL, DIMENSION(10) :: array1
array1 = 0.
Example-1PROGRAM squaresIMPLICIT NONE
INTEGER :: iINTEGER, DIMENSION(10) :: number, square
DO i=1, 10number(i)=isquare(i)=number(i)**2END DO
DO i=1, 10WRITE(*,*) number(i), ' ', square(i)END DO
END PROGRAM
Initialization of array elements
• Named constants in array declarationINTEGER, 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
• Initializing with READ statementINTEGER, 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 loopsINTEGER, DIMENSION(100) :: array1 = ( / ( i , i=1,100) / )
General form of implied loop (arg1, arg2, …, index = istart, iend, incr)
Initialization of array elements
Example-2PROGRAM square_rootsIMPLICIT NONE
INTEGER :: iINTEGER, DIMENSION(10) :: value = (/ (i, i = 1, 10) /)INTEGER, DIMENSION(10) :: s_root
DO i=1, 10s_root(i) = SQRT(value(i))END DO
DO i=1, 10WRITE(*,*) values(i), ' ', s_root(i)END DO
END PROGRAM
More Implied loops
• 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
• DO i = 1, 4• WRITE(*,*) c(i)• END DO
c = a + b
WRITE(*,*) ‘ c = ’, c
Output :c = 3.0 4.0 4.0 4.0
2210
1234
3444
+ =
a b 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 summationIMPLICIT NONEINTEGER :: iREAL, 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, 6total = 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):: arrElements 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 = 19 - -1 +1 = 21
Out of boundsINTEGER, DIMESION(5):: arrDo i=1,5WRITE(*,*) arr(i)END DO
INTEGER, DIMESION(-2:2):: arrDo i=1,5WRITE(*,*) arr(i)END DO
INTEGER, DIMESION(-2:2):: arrDo i=-2,2WRITE(*,*) arr(i)END DO
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 FormatINTEGER :: a=10REAL :: b=5.1
WRITE(*,*) a, bOutput: 10 5.10000000
WRITE(*,100) a, b100 FORMAT(I2, F10.8)Output: 105.10000000
WRITE(*,100) a, b100 FORMAT(I2, 1X, F10.8)Output: 10 5.10000000
WRITE(*,100) a, b100 FORMAT(I2, 3X, F3.1)Output: 10 5.1
WRITE(*,100) a, b100 FORMAT(' a= ',I2, 3X, 'b= ', F3.1)Output: a= 10 b= 5.1
Input/output of array elementsREAL, DIMENSION(5) :: a= (/0.5,-1,0.1,2., 10.4/)
WRITE(*,*) (a(i), i=1, 5)Output: 0.500000000 -1.00000000 0.100000001 2.00000000 10.40000000
Do i=1,5WRITE(*,*) a(i)END DOOutput: 0.500000000 -1.00000000 0.100000000 2.00000000 10.40000000
WRITE(*,100) a(1), a(2), a(3), a(4), a(5)100 FORMAT (1X, 'a =', 5F7.2)Output: a = 0.50 -1.00 0.10 2.00 10.40
WRITE(*,100) (a(i), i=1, 5)100 FORMAT (F10.2)Output: 0.50 -1.00 0.10 2.00 10.40
WRITE(*,100) (a(i), i=1, 5)100 FORMAT (5F10.2)Output: 0.50 -1.00 0.10 2.00 10.40
Input/Output with implied loopsWRITE(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: 1 2 3 2 4 6 3 6 9
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 arrayREAL, DIMENSION(3,6) :: arr1
REAL, DIMENSION(-1:9, 0:19) :: arr2
INTEGER, DIMENSION(10, 0:19) :: arr3
Initializing 2-D arrays
• Using DO loopsINTEGER, DIMENSION(4,3):: arrDO i=1, 4
DO j=1, 3arr(i, j)= j
END DOEND DO
• Using Constructorarr=(/ 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 arrayarr=RESHAPE( (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /), (/4, 3/))
1 2 3
1 2 3
1 2 3
1 2 3
• Initializing in DeclarationINTEGER, DIMENSION(4,3):: arr=RESHAPE ( (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /), (/4,3/))
• Initializing using READREAD(*,*) arr
READ(*,*) ((arr(i,j), j=1,3), i=1,4)
Initializing 2-D arrays
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?
1 2 3 4
5 6 7 8
1 1 2 2
3 3 4 4
arr(:,3)
arr(4,:)
arr(:,2:4:2)
2-D array outputWRITE (*,*) arr1 1 1 1 2 2 2 2 3 3 3 3
DO j=1, 3 DO i=1, 4 WRITE (*,*) arr(i, j) END DOEND DO
DO i=1, 4 DO j=1, 3 WRITE (*,*) arr(i, j) END DOEND DO
WRITE (*,*) ((arr(i,j), i=1,4), j=1,3)1 1 1 1 2 2 2 2 3 3 3 3
DO i=1, 4 WRITE (*,*) (arr(i, j), j=1,3)END DO
1 2 3
1 2 3
1 2 3
1 2 3
1
1
1
1
2
2
2
2
3
3
3
3
1
2
3
1
2
3
1
2
3
1
2
3
1 2 3
1 2 3
1 2 3
1 2 3
2-D array example-1
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.
2. Repeat the above program but let the user now enter the values of the matrix via the keyboard.
1.5 2. -3.1 4.0
5. 9.6 7.7 -8.
1.1 0.1 2.1 -2.
2-D array example-2
1. 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.
2. Repeat the above program but let the user now enter the values of the matrix via the keyboard.
1.5 2. -3.1 4.0
5. 9.6 7.7 -8.
1.1 0.1 2.1 -2.
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
l
jjkijik baC
1
*