1 / 38

Review of Arrays in Programming

Arrays are sequential access data structures that allow storing and accessing multiple data items of the same type directly. Learn about passing arrays, common mistakes, printing and reading arrays, array constants, dimensioning arrays, array expressions, subarrays, vector subscripts, and the WHERE statement.

pineiro
Download Presentation

Review of Arrays in Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays II

  2. Review of arrays • Arrays are sequential access data structures • They allow us to store many data items of the same type and access them directly • You must always include the array name and index number to reference an element in an array

  3. Passing arrays • When an array is passed into a subprogram you use only the name in the actual and formal arguments. • CALL READIT(x,n) • Whenever the array name is used the whole array is specified.

  4. Array elements • Whenever a particular array element is referred to you must specify the array name and the index number of the element. • READ*, x(5) • or, READ*, x(i) where i is an integer variable used to indicate the index value of the array element

  5. Common mistakes • Here are two common mistakes • 1. Passing a particular element when you intent to pass the entire array • CALL READIN(x(i), n) • 2. Referring to the entire array when you only want one element • READ*, x WRONG! Should be x WRONG! Should be x(i)

  6. Printing and Reading Arrays • There are many ways to reference array elements for printing and reading. • The default method • One per line • Multiple items per line

  7. Default printing • Whenever you refer to the name of an array without specifying a particular element you get the whole array. • PRINT*, x • This will print all elements in x on one line, wrapped around if necessary.

  8. Default reading • If x is an array of n items then, • READ*, x • will attempt to read x items from the data file. • You should use a formatted read if the items are in columns or have a specific arrangement on the file (like columns)

  9. Reading and printing one per line • DO i = 1, n • READ*, x(i) • PRINT*, x(i) • ENDDO

  10. Multiple items method • DO i = 1, 10 • READ(*,’(5(1x,15))’) (x(j), j = 1, 5) • PRINT(*,’(5(1x,15))’) (x(j), j = 1, 5) • ENDDO Implied DO loop

  11. Array constants • A new feature in f90 is the array constant. These are initialization strings that may be directly assigned to an array. • Examples • x = (/ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 /) • x = (/ (2*i, i=1,10) /)

  12. Why use them? • The purpose of array constants is to avoid having to do use an explicit DO loop: • DO i=1,20 • x(i) = 2*i • ENDDO

  13. Special case • When you wish to assign the same value to all the items in an array you can do it directly. • Example: • a = 0 • This works because ‘a’ refers to the entire array instead of a single element.

  14. Dimensioning array a Example: INTEGER, DIMENSION(8) :: a a = 0 same as a = (/ 0,0,0,0,0,0,0,0 /) same as DO i = 1, 8 a(i) = 0 ENDDO a(1) a(2) a(3) a(4) 0 0 0 0 a(5) a(6) a(7) a(8) 0 0 0 0

  15. Dimensioning arrays • By default, array element indices always start at 1

  16. Dimensioning array a a(1) a(2) a(3) a(4) 4 By default, array indices always start at 1. Example: INTEGER, DIMENSION(8) :: a a = (/ 4, 2, 8, 7, 6, 1, 9, 5 /) 2 8 7 a(5) a(6) a(7) a(8) 6 1 9 5

  17. Dimensioning array a a(0) a(1) a(2) a(3) 4 Sometimes it is not appropriate to start at 1, if so, you can set both dimensions when the array is declared. Example: INTEGER, DIMENSION(0:7) :: a a = (/ 4, 2, 8, 7, 6, 1, 9, 5 /) 2 8 7 a(4) a(5) a(6) a(7) 6 1 9 5

  18. Dimensioning array a a(-3) a(-2) a(-1) a(0) 4 Example: INTEGER, DIMENSION(-3:4) :: a a = (/ 4, 2, 8, 7, 6, 1, 9, 5 /) 2 8 7 a(1) a(2) a(3) a(4) 6 1 9 5

  19. Array expressions • If you have two arrays of the same type and size you can use the standard relational and arithmetic operators on them.

  20. Arithmetic operators with arrays a b a Example: a = a - b + 2 a(1) a(2) a(3) a(4) 4 a(1) a(2) a(3) a(4) 6 a(1) a(2) a(3) a(4) 0 2 1 3 8 9 1 7 5 4

  21. Subarrays • A parent array is one from which data portions are copied. • A subarray is a portion of a parent array. • Subarrays are declared by specifying the lower bound, upper bound and element increment (stride) in relation to the parent array.

  22. Subarray example 1 a b 4 4 a is the parent array b is a subarray containing the first 5 elements of a b = a(1:5) a(1) a(2) a(3) a(4) b(1) b(2) b(3) b(4) b(5) 2 2 8 8 7 7 a(5) a(6) a(7) a(8) 6 6 1 9 5

  23. Subarray example 2 a b 4 b(1) b(2) b(3) b(4) 4 a is the parent array b is a subarray containing the odd numbered elements of a b = a(1:5:2) a(1) a(2) a(3) a(4) 2 8 8 6 7 9 a(5) a(6) a(7) a(8) 6 1 9 5

  24. Vector subscripts Arrays are often called vectors. Here is an example that uses an array (called a vector array) to serve as a map for the definition of parent array elements to be included in a subarray. a b i 4 b(1) b(2) b(3) b(4) 0 a(1) a(2) a(3) a(4) i(1) i(2) i(3) i(4) 1 2 0 4 8 0 5 7 0 6 a(5) a(6) a(7) a(8) 6 1 9 5

  25. Vector subscripts a i 4 i(1) i(2) i(3) i(4) 1 b = a(i) a(1) a(2) a(3) a(4) 2 4 8 5 7 6 b a(5) a(6) a(7) a(8) 6 b(1) b(2) b(3) b(4) 4 1 7 9 6 5 1

  26. The WHERE statement • FORTRAN offers many specialized operations for dealing with arrays that are not found in other programming languages. • One important one is the WHERE statement

  27. WHERE Example INTEGER, DIMENSION(8) :: a a = (/4,2,8,7,6,1,9,5 /) INTEGER, DIMENSION(8) :: b b = 0 WHERE (a > 3) b = 1 ELSEWHERE b = 0 END WHERE INTEGER, DIMENSION(8) :: a a = (/4,2,8,7,6,1,9,5 /) INTEGER, DIMENSION(8) :: b b = 0 DO i = 1,8 IF (a(i) > 3) THEN b(i) = 1 ELSE b(i) = 0 ENDIF ENDDO

  28. WHERE example a b 4 1 INTEGER, DIMENSION(8) :: a a = (/ 4,2,8,7,6,1,9,5 /) INTEGER, DIMENSION(8) :: b b = 0 WHERE (a > 3) b = 1 ELSEWHERE b = 0 END WHERE a(1) a(2) a(3) a(4) b(1) b(2) b(3) b(4) 2 0 8 1 7 1 a(5) a(6) a(7) a(8) b(5) b(6) b(7) b(8) 6 1 1 0 9 1 5 1

  29. Array subprograms • FORTRAN has a wide array of subprograms available that deal with arrays • Allocated(A) - logical T/F • Size(A) - # of elements in A • Maxval(A) - maximum value • Minval(A) - minimum value

  30. Maxval THIS instead of THIS max = MAXVAL(a) max = a(1) DO i = 2, n IF (a(i) > max) THEN max = a(i) ENDIF ENDDO Returns the largest value in an array.

  31. Minval THIS instead of THIS min = MINVAL(a) min = a(1) DO i = 2, n IF (a(i) < min) THEN min = a(i) ENDIF ENDDO Returns the smallest value in an array.

  32. Maxloc THIS instead of THIS maxpos = MAXLOC(a) maxpos = 1 max = a(1) DO i = 2, n IF (a(i) > max) THEN max = a(i) maxpos = i ENDIF ENDDO Returns the location of the largest value in an array.

  33. Minloc THIS instead of THIS maxpos = MINLOC(a) minpos = 1 min = a(1) DO i = 2, n IF (a(i) < max) THEN min = a(i) minpos = i ENDIF ENDDO Returns the location of the smallest value in an array.

  34. Product THIS instead of THIS prod = PRODUCT(a) prod = 1 DO i = 1, n prod = prod * a(i) ENDDO Returns the product of all the items in the array.

  35. Sum THIS instead of THIS total = SUM(a) total = 0 DO i = 1, n total = total + a(i) ENDDO Returns the sum of all the items in the array.

  36. Dot_Product THIS instead of THIS dp = DOT_PRODUCT(a,b) dp = 0 DO i = 1, n dp = dp + (a(i) * b(i)) ENDDO Returns the dot product of all the items in the array.

  37. Other functions • Check the list in Appendix D for a complete list of FORTRAN subroutines and examples of how to use them. • Example: SIZE, returns the declared size of an array (see next slide)

  38. Automatic arrays • An automatic array is one that is automatically dimensioned to be the same size as another. SUBROUTINE Swap(A, B) REAL, DIMENSION(:), INTENT(INOUT):: a,b REAL, DIMENSION( SIZE(a) ) :: temp temp = a a = b b = temp END SUBROUTINE Swap

More Related