Arrays - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Arrays

Description:

Last = Middle - 1. ELSE IF (ItemSought Item(Middle)) THEN. First = Middle 1. ELSE ... list-of-array-names. examples: REAL, DIMENSION(24, 365) :: Temperature ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 27
Provided by: joaqui3
Category:
Tags: arrays | last | meaning | names

less

Transcript and Presenter's Notes

Title: Arrays


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 bya a21
    a22, a2n

7
Function to Calculate the Norm
  • FUNCTION Norm(A, N) REAL Norm REAL,
    INTENT(IN), DIMENSION(N) A INTEGER, INTENT
    (IN) N Norm SQRT(SUM(AA))END FUNCTION
    Norm

8
Sum Difference of Vectors
  • Example
  • a(10, 20)
  • b(30, 40)
  • The Sum A B (40, 60)
  • The difference A - 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, 45 1, 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)lt 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(INumItems)) MINLOC_array
MINLOC(Item(INumItems)) 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 I1,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 gt
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 gt Last) .OR. Found) RETURN Middle
(First Last) / 2 IF (ItemSought lt
Item(Middle)) THEN Last Middle - 1
ELSE IF (ItemSought gt 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(l1u1, l2u2, l3u3,lnun)
    list-of-array-namesexamplesREAL,
    DIMENSION(24, 365) TemperatureREAL,
    DIMENSION(124, 1365) 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, 5 READ , data (I, J) END
    DOEND DO

23
Reading MD Arrays
  • Using Implied DO StatementsINTEGER data (10,
    5)READ , ((data (I, J), J 1, 5), I1, 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),
Location1,NumLocs) END DO DEALLOCATE
(Temperature) END PROGRAM Table_of_Temperatures
25
Matrix Multiplication
  • Class Exercise

26
Exercise 8.3, Prob. 18.
  • 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
Write a Comment
User Comments (0)
About PowerShow.com