VIEWS: 32 PAGES: 36 CATEGORY: Computers & Internet POSTED ON: 3/26/2010 Public Domain
Midterm Review Programming in Fortran Yi Lin Feb 13, 2007 3/25/2010 Comp208 Computers in Engineering 1 What we have learned Units of Fortran programs Variable Expression Statement Control statements (IF-ELSE) IF IF-THEN-ELSE-ENDIF IF-THEN-ELSEIF-THEN-ELSE-ENDIF Repetition statements (DO LOOP) Count DO LOOP INFINITE DO LOOP Function and Subroutine 3/25/2010 Comp208 Computers in Engineering 2 Units of Fortran programs Smallest: Variables and constants Constants: the values are the same E.g., “hello”, 34, Variables: is a unique name which a FORTRAN program applies to a word of memory and uses to refer to it. Its values can be reassigned. E.g. a, b, Variable types INTEGER REAL LOGICAL .TRUE. .FALSE. CHARACTER 3/25/2010 Comp208 Computers in Engineering 3 Expression and Mixed mode calculation Composed of variables, constants and operators. For example: 3+½ 3 > 2 .AND. 4 >=3 “Prof. “ // ”Friedman” An expression has a value which is of a specific type (e.g., INTEGER, REAL, LOGICAL, CHARACTER) 3+1/2 has a value of 3.5 3 > 2 .AND. 4 >=3 has a value of .TRUE. “Prof. “ // “Friedman” has a value of “Prof. Friedman” 3/25/2010 Comp208 Computers in Engineering 4 Mixed Mode Expressions If one operand of an arithmetic operator is INTEGER and the other is REAL the INTEGER value is converted to REAL the operation is performed the result is REAL 1 + 2.5 3.5 1/2.0 0.5 2.0/8 0.25 -3**2.0 -9.0 1 + 5/2 3 (since 5/22) 4.0**(1/2) 1.0 (since ½ 0) 3/25/2010 Comp208 Computers in Engineering 5 Evaluate Complex Expression Arithmetic operators: precedence operators Precedence 2+3*4 = ? () 1 = 5 * 4 = 20 ** 2 Or *, / 3 = 2+12 = 14 +, - 4 3/3**4 = ? 3/25/2010 Comp208 Computers in Engineering 6 Evaluation Complex Expression Arithmetic operators: associativity operators associativity associativity resolves the order of operations when two operators of () Left to right the same precedence compete for three operands: ** Right to left 2**3**4 = 2**(3**4) = 2**81, 72/12/ 3 = (72/12)/3 = 6/3 = 2 *, / Left to right 30/5*3 = (30/5)*3 = 18 +, - Left to right if *,/ associativity is from right to left 30/5*3 = 30/(5*3) = 2 3/25/2010 Comp208 Computers in Engineering 7 Logical Expressions (.TRUE. .FALSE.) Relational operators lower precedence than arithmetic operator No associativity (illegal: 4 > 3 >2) <, <=, >, >=, ==, /= Logical operators lower precedence than Relational operators From left to right except .NOT. .NOT. High .AND. .OR. .EQV., .NEQV. Low 3/25/2010 Comp208 Computers in Engineering 8 Examples Suppose we have the declaration: INTEGER :: age=34, old=92, young=16 What is the value of the following expressions? age /= old age >= young age==56 .and. old/=92 age==56 .or. old/=92 age==56 .or. old/=92 .and. young==16 .not. age==56 .or. old/=92 3/25/2010 Comp208 Computers in Engineering 9 Control statements IF-THEN-ELSE-END IF Syntax: .TRUE. .FALSE. Log Exp IF (logical-exp) THEN first statement block, ELSE 1st Block 2nd block second statement block, END IF Stmt following END IF 3/25/2010 Comp208 Computers in Engineering 10 Control statements IF-THEN-END IF Syntax: .TRUE. .FALSE. Log Exp IF (logical-exp) THEN first statement block, END IF 1st Block Stmt following END IF 3/25/2010 Comp208 Computers in Engineering 11 Control statements Logical IF .TRUE. .FALSE. Log Exp IF (logical-exp) statement One statement Stmt following END IF 3/25/2010 Comp208 Computers in Engineering 12 Control statements IF-THEN-ELSEIF-THEN-ELSE- .FALSE. END IF Log Exp1 Syntax: .FALSE. IF (log-exp1) THEN .TRUE. Log Exp2 first statement block, ELSEIF (log-exp2)THEN .TRUE. second statement block, 2nd block …… 1st Block …… ELSE else block END IF Stmt following END IF 3/25/2010 Comp208 Computers in Engineering 13 Control statement: SELECT CASE The SELECT CASE construct provides an alternative to a series of repeated IF ... THEN ... ELSE IF statements. Syntax: SELECT CASE( expression ) CASE( value 1) block 1 ... CASE (value i) block I … [CASE DEFAULT block default] END SELECT 3/25/2010 Comp208 Computers in Engineering 14 SELECT CASE statement example INTEGER::month READ(*,*) month !input an integer from keyboard SELECT CASE(month) CASE (1) WRITE(*,*) “WINTER” CASE (2) WRITE(*,*) “WINTER” CASE (3) WRITE(*,*) “WINTER” CASE (4) WRITE(*,*) “Spring” CASE (5) WRITE(*,*) “Spring” CASE (6) WRITE(*,*) “Summer” CASE (7) WRITE(*,*) “Summer” CASE (8) WRITE(*,*) “Summer” CASE (9) WRITE(*,*) “FALL” CASE (10) WRITE(*,*) “FALL” CASE (11) WRITE(*,*) “WINTER” CASE (12) WRITE(*,*) “WINTER” CASE DEFAULT WRITE(*,*) “Not a month!” END SELECT 3/25/2010 Comp208 Computers in Engineering 15 SELECT CASE statement example Can be INTEGER, CHARACTER, LOGICAL INTEGER::month No REAL READ(*,*) month !input an integer from keyboard SELECT CASE(month) CASE (4,5) (value1, value2) WRITE(*,*) “Spring” CASE (6:8) (min:max) i.e., 6, 7, 8 WRITE(*,*) “Summer” CASE (9,10) WRITE(*,*) “FALL” CASE (11, 12, 1:3) THEN (value1, value2, min:max) WRITE(*,*) “WINTER” CASE DEFALUT WRITE(*,*) “Not a month!” END SELECT 3/25/2010 Comp208 Computers in Engineering 16 Repetition, DO statement Count loop Count=start uses a control clause to repeat a block of statements a predefined number of times. No Note that count variable should not be Right step modified within loop body. Yes Syntax: DO count = start, stop [,step] Yes Start exceeds stop block of statements END DO No Infinite loop Block of statements Use EXIT to get out. DO block of statements Count=count+step END DO Next stmt after END DO 3/25/2010 Comp208 Computers in Engineering 17 Count DO Loop examples Example 1: DO i=1, 10, 1 WRITE(*,*) i !write numbers 1, 2, …, 10 END DO Write(*,*) I ! I = 11 Example 2: DO i=1, 10 ! Default step = 1 WRITE(*,*) i !write numbers 1, 2, …, 10 END DO Example 3: DO i=1, 10, 2 ! i increased by 2 for each step WRITE(*,*) i !write numbers 1,3,5,7,9 END DO Write(*,*) I ! i= 11 3/25/2010 Comp208 Computers in Engineering 18 Count DO loop examples Example 4: DO j=10,2,-2 ! j decreased by 2 WRITE(*,*) j !write even numbers 10,8,6,4,2 END DO Example 5: DO i=3,3 write(*,*) I End do Example 5: i=1 DO WHILE(i<10) write(*,*) I i=i+1 END DO 3/25/2010 Comp208 Computers in Engineering 19 Loop within a loop Example Do i=1, 3 a=1 Do j=1,3 a = a+1 End do a = a+1 End Do Write(*,*) a 3/25/2010 Comp208 Computers in Engineering 20 Infinite DO loop example INTEGER::I=0 DO IF(I>10) EXIT ! Loop terminated at I==11 WRITE(*,*) I ! WRITE number 1 to 10 I=I+1 ! I increased by 1 at each step END DO Without IF(i>10) EXIT, the program will not be able to stop. 3/25/2010 Comp208 Computers in Engineering 21 Array An array is a collection of individual data elements, all of the same type. E.g., array Index: 1 2 3 4 5 6 7 8 51 6 34 61 75 4 53 5 element The subscript (or index) of an array element is the position of that element within the array, for example: the first element is 51 and has a subscript 1, the second element is 6 and has a subscript 2. 3/25/2010 Comp208 Computers in Engineering 22 Declare an array Syntax type, DIMENSION(bound ) :: name ! Fortran 90 only type :: name(bound) Where, bound = [lower:]upper lower: smallest index of the elements, by default=1 upper: largest index of the elements E.g., to declare the previous array example: INTEGER, DIMENSION(8)::a 0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 INTEGER, DIMENSION(1:8)::a INTEGER::a(8) 51 6 34 61 75 4 53 5 INTEGER::a(0:7) ! Then 51’s index=0, 6’s index=1, 5’s index=7 3/25/2010 Comp208 Computers in Engineering 23 Multi-dimensional array Consider the following array 51 6 34 61 75 4 53 5 This is one-dimensional array so it can only represent a vector. However, some data are more than one dimensional, e.g., matrix 51 61 53 6 75 5 Syntax: 34 4 10 TYPE, DIMENSION([1lb:][1ub], [2lb:][2ub])::name TYPE::name([1lb:][1ub], [2lb:][2ub]) 3/25/2010 Comp208 Computers in Engineering 24 Two dimensional array To declare an integer matrix with 3 rows and 4 columns j=3 They are equivalent INTEGER::a(1:3, 1:4) INTEGER::a(3,4) i=2 a(2,3) INTEGER, DIMENSION(1:3, 1:4)::a INTEGER, DIMENSION(3,4)::a a(i, j): to refer to an element at row i and column j, e.g., a(2, 3) 3/25/2010 Comp208 Computers in Engineering 25 Two dimensional array, example ! To set a matrix with 3 rows and 4 columns to zero PROGRAM test DO j=1,4 IMPLICIT NONE a(1, j)=0 INTEGER::a(3,4), i, j END DO DO i=1,3 DO j=1,4 0 0 0 0 DO j=1,4 a(2, j)=0 0 0 0 0 a(i, j)=0 END DO END DO 0 0 0 0 END DO DO j=1,4 END PROGRAM a(3, j)=0 END DO 3/25/2010 Comp208 Computers in Engineering 26 Function and Subroutine type FUNCTION function-name (arg1, arg2, ..., argn) IMPLICIT NONE [declarations] [statements] [other subprograms] END FUNCTION function-name SUBROUTINE subroutine-name (arg1, arg2, ..., argn) IMPLICIT NONE [declarations] [statements] [other subprograms] END SUBROUTINE subroutine-name 3/25/2010 Comp208 Computers in Engineering 27 Rules for Argument Association Rule 1: If an actual argument is an expression or a constant, it is evaluated and the result is saved into a temporary location. Then, the value in this temporary location is passed. INTEGER :: a = 10, b = 3, c = 37 WRITE(*,*) Minimum(18,c-a,a+b) When the function is invoked, new temporary variables we can call x, y and z are created. The value of x is initialized to 18, y to 27 and z to 13. The function returns 13. 3/25/2010 Comp208 Computers in Engineering 28 Rules for Argument Association Rule 2: If an actual argument is a variable, the corresponding formal argument is made to refer to the same memory cell. INTEGER :: a = 10, b = 3, c = 37 WRITE(*,*) Minimum(a,b,c) When the function is invoked, there are no new variables created. The parameter x refers to a, y to b and z to c. We say x is an alias for a. There are two names for the same memory cell. The function returns 3. 3/25/2010 Comp208 Computers in Engineering 29 Argument passing example REAL::x=1, y=2 WRITE(*,*) "x=", x, “y=", y ! X=1.0 y=2.0 CALL swap(x,y) a x b y SUBROUTINE swap( a, b ) REAL, INTENT(INOUT):: a, b 1.0 2.0 REAL:: temp temp = a temp 1.0 a=b a x b y b = temp END SUBROUTINE swap 2.0 1.0 WRITE(*,*) "x=", x, “y=", y ! x=2.0 y=1.0 3/25/2010 Comp208 Computers in Engineering 30 Example passing array as argument ! Input a list of real number and calculate their sum. PROGRAM Test IMPLICIT NONE INTEGER, PARAMETER :: MAX_SIZE = 1000 INTEGER, DIMENSION(1:MAX_SIZE) :: Data INTEGER::Sum INTEGER :: ActualSize INTEGER :: i READ(*,*) ActualSize READ(*,*) (Data(i), i=1, ActualSize) WRITE(*,*) "Sum = ", Sum(Data, ActualSize) END PROGRAM Test INTEGER FUNCTION Sum(x, n) IMPLICIT NONE INTEGER, INTENT(IN):: n INTEGER, DIMENSION(n), INTENT(IN) :: x INTEGER :: Total INTEGER :: i Total = 0.0 DO i = 1, n Total = Total + x(i) END DO Sum = Total END FUNCTION Sum 3/25/2010 Comp208 Computers in Engineering 31 Implied DO Loops The implied DO loop can simplify this greatly. INTEGER :: data(100) INTEGER :: n, i READ(*,*) n READ(*,*) (data(i), i=1, n) If the value of n is 15, this READ(*,*) statement is equivalent to INTEGER :: data(100) INTEGER :: n, i READ(*,*) data(1), data(2),. . ., data(15) What is the difference? The values read can appear on one or more lines since FORTRAN will automatically search for the next input on the current input line or go on to the next line if needed. 3/25/2010 Comp208 Computers in Engineering 32 FORMAT statement, F Example REAL::x=1.0, y=1100.1003 write(*, 900) x, y 900 format (F3.1, F9.4) (F3.1,F9.4): 1.01100.1003 (F3.1,F10.4): 1.0#1100.1003 (F3.1,F8.4): 1.0******** *: Width=8 is not wide enough to output y. 4 integer digits + 4 decimal digits + 1 for “.” = 9 digits 3/25/2010 Comp208 Computers in Engineering 33 FORMAT statement, I For integers only the field width is specified, so the syntax is Iw. Similarly, character strings can be specified as Aw but the field width is often dropped. INTEGER::a=1000 A5 I6 WRITE(*,100) “a=“, a 100 FORMAT(A5,I6) ###a=##1000 WRITE(*,200) “a=“,a A I4 200 FORMAT(A,I4) a=1000 WRITE(*,300) “a=“,a A I3 300 FORMAT(A,I3) a=*** 3/25/2010 Comp208 Computers in Engineering 34 FORMAT statement, READ Example INTEGER::a,b READ(*,100) a,b 100 FORMAT(2I3) ! eqv. To FORMAT(I3,I3) Correct inputs for (2I3), e.g., “##1##2” a=##1=1, b=##2=2 “1##2##” a=1##=1, b=2##=2 “#1##2#” a=#1#=1, b=#2#=2 3/25/2010 Comp208 Computers in Engineering 35 FILE input/output, Example ! Input 10 integers from keyboard and write them to file “inputData.txt” PROGRAM fileTest IMPLICIT NONE INTEGER::count, a OPEN(UNIT=10,FILE=“inputData.txt”) ! Open file “inputData.txt” DO count=1,10 WRITE(*,*) “Input an integer number from keyboard:” READ(*,*) a READ(10,100) “a=“, a ! Write to “inputData.txt” a=######51 END DO a=#######6 CLOSE(10); ! Close file “inputData.txt” … 100FORMAT(A2, I8) END PROGRAM inputData.txt 3/25/2010 Comp208 Computers in Engineering 36