1 / 26

Arrays

Arrays. Arrays asArguments. Example: Calculating the mean of a list. (Fortran allows both the array and its size to be passed to the subprogram). PROGRAM Mean_of_a_List_2 IMPLICIT NONE INTEGER, PARAMETER :: NumItems = 10 REAL, DIMENSION( NumItems ) :: Item

brent
Télécharger la présentation

Arrays

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

  2. Arrays asArguments • Example: • Calculating the mean of a list. • (Fortran allows both the array and its size to be passed to the subprogram).

  3. PROGRAM Mean_of_a_List_2 IMPLICIT NONE INTEGER, PARAMETER :: NumItems = 10 REAL, DIMENSION(NumItems) :: Item PRINT *, "Enter the", NumItems, " real numbers:" READ *, Item PRINT '(1X, "Mean of the ", I3, " Numbers is ", F6.2)', & NumItems, Mean(Item, NumItems) CONTAINS

  4. FUNCTION Mean(X, NumElements) INTEGER, INTENT(IN) :: NumElements REAL, DIMENSION(NumElements), INTENT(IN) :: X REAL :: Mean Mean = SUM(X) / REAL(NumElements) END FUNCTION Mean END PROGRAM Mean_of_a_List_2

  5. Vector Processing • Two dimensional vectors can be represented algebraically as ordered pairs • (a1, a2) of real numbers • a1, a2 are called components • In general, n-dimensional vectors can be represented algebraically by ordered n-tuples (a1, a2, … an).

  6. The Norm of a Vector • The norm of an n-dimensional vector • a=(a1, a2, … an) is given by|a| = a21+ a22,+…+ a2n

  7. Function to Calculate the Norm • FUNCTION Norm(A, N) REAL :: Norm REAL, INTENT(IN), DIMENSION(N) :: A INTEGER, INTENT (IN) :: NNorm = SQRT(SUM(A*A))END FUNCTION Norm

  8. Sum & Difference of Vectors • Example: • a=(10, 20) • b=(30, 40) • The SumA + B = (40, 60) • The differenceA - B = (-20, -20)

  9. Sorting • Sorting: Arranging the items in a list so that they are in either ascending or descending order. 25, 33, 1, 14, 27, 451, 14, 25, 27, 33, 45

  10. program sort implicit none integer:: item(10), i, j, min, pos, temp item = (/10, 2, 15, 9, 1, 0, 33, 29, 50, 17/) do i = 1, 9 min = item(i) pos = i do j = i, 10 if (item(j)< min)then min = item(j) pos = j end if end do temp = item(i) item(i)= min item(pos) = temp end do write(*, "(1x, i2)") item end program sort

  11. SUBROUTINE SelectionSort(Item) INTEGER, DIMENSION(:), INTENT(INOUT) :: Item INTEGER :: NumItems, SmallestItem, I INTEGER, DIMENSION(1) :: MINLOC_array NumItems = SIZE(Item) DO I = 1, NumItems - 1 SmallestItem = MINVAL(Item(I:NumItems)) MINLOC_array = MINLOC(Item(I:NumItems)) LocationSmallest = (I - 1) + MINLOC_array(1) Item(LocationSmallest) = Item(I) Item(I) = SmallestItem END DO END SUBROUTINE SelectionSort

  12. Search • Linear Search • Example:

  13. PROGRAM LinearSearch INTEGER, DIMENSION(10) :: A = (/5,10,15,20,25,30,35,40,45,50/) INTEGER :: N, I, POS LOGICAL :: FOUND = .FALSE. WRITE(*,'(1X, A)',ADVANCE="NO") "Search for :" READ *, N DO I=1,10 IF (A(I) == N) THEN POS = I FOUND = .TRUE. EXIT END IF END DO IF (FOUND) PRINT *, "N = ",N," is in position = ",POS END PROGRAM LinearSearch

  14. SUBROUTINE LinearSearch(Item, ItemSought, Found, Location) CHARACTER(*), DIMENSION(:), INTENT(IN) :: Item CHARACTER(*), INTENT(IN) :: ItemSought LOGICAL, INTENT(OUT) :: Found INTEGER, INTENT(OUT) :: Location INTEGER :: NumItems NumItems = SIZE(Item) Location = 1 Found = .FALSE. DO IF ((Location > NumItems) .OR. Found) RETURN IF (ItemSought == Item(Location)) THEN Found = .TRUE. ELSE Location = Location + 1 END IF END DO END SUBROUTINE LinearSearch

  15. Passing Arrays as Arguments SUBROUTINE task(Item) INTEGER, DIMENSION(:), INTENT(IN) :: Item or SUBROUTINE task (Item, NumElements)INTEGER, INTENT(IN) :: NumElements INTEGER, INTENT(IN), DIMENSION(NumElements) :: Item

  16. Binary Search • Binary search can be used to search for an item more efficiently than linear search. • Example:

  17. SUBROUTINE BinarySearch(Item, ItemSought, Found, Location) CHARACTER(*), DIMENSION(:), INTENT(IN) :: Item CHARACTER(*), INTENT(IN) :: ItemSought LOGICAL, INTENT(OUT) :: Found INTEGER, INTENT(OUT) :: Location INTEGER :: First, Last, Middle First = 1 Last = SIZE(Item) Found = .FALSE. DO IF ((First > Last) .OR. Found) RETURN Middle = (First + Last) / 2 IF (ItemSought < Item(Middle)) THEN Last = Middle - 1 ELSE IF (ItemSought > Item(Middle)) THEN First = Middle + 1 ELSE Found = .TRUE. Location = Middle END IF END DO END SUBROUTINE BinarySearch

  18. Multidimensional Arrays • A multidimensional array has more than one subscript. • There is no limit to the number of dimensions Fortran can have, but there is a limit on the total array size.

  19. Declaring Multidimensional Arrays • General Form: • type, DIMENSION(l1:u1, l2:u2, l3:u3,…ln:un) :: &list-of-array-namesexamples:REAL, DIMENSION(24, 365) :: TemperatureREAL, DIMENSION(1:24, 1:365) :: Temperature

  20. Multidimensional Arrays • INTEGER, DIMENSION (3, 5) :: Data Columns Data 1 2 3 4 5 5 10 15 20 25 1 Rows 30 35 40 45 50 2 55 60 65 70 75 3

  21. Multidimensional Arrays • INTEGER, DIMENSION (3, 5) :: DataData(2, 3) 3 5 10 15 20 25 30 35 40 45 50 2 55 60 65 70 75

  22. Reading MD Arrays • Using Nested DO statements • INTEGER :: data (10, 5)DO I = 1, 10 DO J = 1, 5READ *, data (I, J) END DOEND DO

  23. Reading MD Arrays • Using Implied DO StatementsINTEGER :: data (10, 5)READ *, ((data (I, J), J= 1, 5), I=1, 10)

  24. PROGRAM Table_of_Temperatures REAL, DIMENSION(:, :), ALLOCATABLE :: Temperature INTEGER :: NumTimes, NumLocs, Time, Location READ *, NumTimes, NumLocs ALLOCATE (Temperature(NumTimes, NumLocs)) PRINT *, "Enter the temperatures at the first location," PRINT *, "then those at the second location, and so on:" READ *, ((Temperature(Time, Location), & Location = 1, NumLocs), Time = 1, NumTimes) PRINT * PRINT '(1X, T13, "Location" / 1X, "Time", 10I6)', & (Location, Location = 1, NumLocs) DO Time = 1, NumTimes PRINT '(/1X, I3, 2X, 10F6.1/)', & Time, (Temperature(Time, Location), Location=1,NumLocs) END DO DEALLOCATE (Temperature) END PROGRAM Table_of_Temperatures

  25. Matrix Multiplication • Class Exercise

  26. program maxarrays integer, dimension (5)::a, b, c read *, a read *, b c = arrayMax(a, b) print *, c contains function arrayMax (a, b) integer, intent (in), dimension (:) :: a, b integer, dimension(size(a)) :: arrayMax arrayMax = MAX(a, b) end function arrayMax end program maxarrays Exercise 8.3, Prob. 18.

More Related