Midterm Review Programming in Fortran Yi Lin Oct 12, 2006 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering What we have learned Units of Fortran programs Variable Expression Statement Control statements (IF-ELSE) IF-THEN-ELSE-ENDIF IF-THEN-ELSEIF-THEN-ELSE-ENDIF Repetition statements (DO LOOP) Count DO LOOP INFINITE DO LOOP Function and Subroutine 11/24/2018 Comp208 Computers in Engineering
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 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering Expression 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” 11/24/2018 Comp208 Computers in Engineering
Arithmetic Expressions An arithmetic expression is formed using the operations: + (addition), - (subtraction) * (multiplication), / (division) ** (exponentiation) If the operands are integers, the result will be an integer value If the operands are real, the result will be a real value If the operands are of different types, the expression is called mixed mode. 11/24/2018 Comp208 Computers in Engineering
Evaluate Simple Expressions 1 + 3 --> 4 1.23 - 0.45 --> 0.78 6.5/1.25 --> 5.2 8.4/4.2 --> 2.0 rather than 2, since the result must be of REAL type. -5**2 --> -25 12/4 --> 3 13/4 --> 3 rather than 3.25. Since the operands are of INTEGER type, so is the result. The computer will truncate the mathematical result to make it an integer. 3/5 --> 0 rather than 0.6. 11/24/2018 Comp208 Computers in Engineering
Evaluate Complex Expression Arithmetic operators: precedence 2+3*4 = ? = 5 * 4 = 20 Or = 2+12 = 14 (2+3)*4 = 20 operators Precedence () 1 ** 2 *, / 3 +, - 4 11/24/2018 Comp208 Computers in Engineering
Evaluation Complex Expression Arithmetic operators: associativity () Left to right ** Right to left *, / +, - associativity resolves the order of operations when two operators of the same precedence compete for three operands: 2**3**4 = 2**(3**4) = 2**81, 72/12/ 3 = (72/12)/3 = 6/3 = 2 30/5*3 = (30/5)*3 = 18 if *,/ associativity is from right to left 30/5*3 = 30/(5*3) = 2 11/24/2018 Comp208 Computers in Engineering
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 1/2.0 2.0/8 -3**2.0 1 + 5/2 4.0**(1/2) 3.5 0.5 0.25 -9.0 3 (since 5/22) 1.0 (since ½ 0) 11/24/2018 Comp208 Computers in Engineering
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. .AND. .OR. .EQV., .NEQV. High Low 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering 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 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering Control statements IF-THEN-ELSE-END IF Syntax: IF (logical-exp) THEN first statement block, ELSE second statement block, END IF .TRUE. .FALSE. Log Exp 1st Block 2nd block Stmt following END IF 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering Control statements IF-THEN-END IF Syntax: IF (logical-exp) THEN first statement block, END IF .TRUE. .FALSE. Log Exp 1st Block Stmt following END IF 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering Control statements Logical IF Syntax: IF (logical-exp) one statement .TRUE. .FALSE. Log Exp One statement Stmt following END IF 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering Control statements .FALSE. IF-THEN-ELSEIF-THEN-ELSE-END IF Syntax: IF (log-exp1) THEN first statement block, ELSEIF (log-exp2)THEN second statement block, …… ELSE else block END IF Log Exp1 .FALSE. .TRUE. Log Exp2 .TRUE. 1st Block 2nd block …… Stmt following END IF 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering IF statement example Prints a season associated with a given month in terms of value. If the value of the integer month is not in the range 1-12, print “not a month!” 1, 2, 3: Winter 4, 5: Spring 6, 7, 8: Summer 9, 10: Fall 11, 12: Winter 11/24/2018 Comp208 Computers in Engineering
IF statement example (cont.) INTEGER::month READ(*,*) month !input an integer from keyboard IF (month ==4 .OR. month==5) THEN WRITE(*,*) “Spring” ELSEIF (month >=6 .AND. month <=8) THEN WRITE(*,*) “Summer” ELSEIF (month==9 .OR. Month==10) THEN WRITE(*,*) “FALL” ELSEIF (month==11 .OR. month==12 .OR. (month>=1 .AND. month<=3)) THEN WRITE(*,*) “WINTER” ELSE WRITE(*,*) “Not a month!” ENDIF 11/24/2018 Comp208 Computers in Engineering
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 11/24/2018 Comp208 Computers in Engineering
SELECT CASE statement example INTEGER::month READ(*,*) month !input an integer from keyboard SELECT CASE(month) CASE (1) WRITE(*,*) “WINTER” CASE (2) CASE (3) CASE (4) WRITE(*,*) “Spring” CASE (5) CASE (6) WRITE(*,*) “Summer” CASE (7) CASE (8) CASE (9) WRITE(*,*) “FALL” CASE (10) CASE (11) CASE (12) CASE DEFAULT WRITE(*,*) “Not a month!” END SELECT 11/24/2018 Comp208 Computers in Engineering
SELECT CASE statement example Can be INTEGER, CHARACTER, LOGICAL No REAL INTEGER::month READ(*,*) month !input an integer from keyboard SELECT CASE(month) CASE (4,5) WRITE(*,*) “Spring” CASE (6:8) WRITE(*,*) “Summer” CASE (9,10) WRITE(*,*) “FALL” CASE (11, 12, 1:3) THEN WRITE(*,*) “WINTER” CASE DEFALUT WRITE(*,*) “Not a month!” END SELECT (value1, value2) (min:max) i.e., 6, 7, 8 (value1, value2, min:max) 11/24/2018 Comp208 Computers in Engineering
Repetition, DO statement Count loop uses a control clause to repeat a block of statements a predefined number of times. Note that count variable should not be modified within loop body. Syntax: DO count = start, stop [,step] block of statements END DO Infinite loop Use EXIT to get out. DO 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering Count DO Loop examples Example 1: DO i=1, 10, 1 WRITE(*,*) i !write numbers 1, 2, …, 10 END DO Example 2: DO i=1, 10 ! Default step = 1 Example 3: DO i=1, 10, 2 ! i increased by 2 for each step WRITE(*,*) i !write numbers 1,3,5,7,9 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering 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: i=1 DO WHILE(i<10) write(*,*) I i=i+1 11/24/2018 Comp208 Computers in Engineering
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. 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering Array An array is a collection of individual data elements, all of the same type. E.g., 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. array Index: 1 2 3 4 5 6 7 8 51 34 61 75 53 element 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering 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 INTEGER, DIMENSION(1:8)::a INTEGER::a(8) INTEGER::a(0:7) ! Then 51’s index=0, 6’s index=1, 5’s index=7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 51 34 61 75 53 11/24/2018 Comp208 Computers in Engineering
Multi-dimensional array Consider the following array This is one-dimensional array so it can only represent a vector. However, some data are more than one dimensional, e.g., matrix Syntax: TYPE, DIMENSION([1lb:][1ub], [2lb:][2ub])::name TYPE::name([1lb:][1ub], [2lb:][2ub]) 51 6 34 61 75 4 53 5 51 61 53 6 75 5 34 4 10 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering Two dimensional array To declare an integer matrix with 3 rows and 4 columns They are equivalent INTEGER::a(1:3, 1:4) INTEGER::a(3,4) 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) j=3 i=2 a(2,3) 11/24/2018 Comp208 Computers in Engineering
Two dimensional array, example ! To set a matrix with 3 rows and 4 columns to zero PROGRAM test IMPLICIT NONE INTEGER::a(3,4), i, j DO i=1,3 DO j=1,4 a(i, j)=0 END DO END PROGRAM DO j=1,4 a(1, j)=0 END DO a(2, j)=0 a(3, j)=0 11/24/2018 Comp208 Computers in Engineering
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) END SUBROUTINE subroutine-name 11/24/2018 Comp208 Computers in Engineering
Where Do Function/Subroutine Definitions Go? External PROGRAM program-name IMPLICIT NONE INTERFACE [declaration of function/subroutine] END INTERFACE [declarations] [statements] END PROGRAM program-name [Function/subroutine definitions] INTERNAL PROGRAM program-name IMPLICIT NONE [declarations] [statements] CONTAINS [function/Subroutine definitions] END PROGRAM program-name 11/24/2018 Comp208 Computers in Engineering
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. 11/24/2018 Comp208 Computers in Engineering
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. 11/24/2018 Comp208 Computers in Engineering
Argument passing example REAL::x=1, y=2 WRITE(*,*) "x=", x, “y=", y ! X=1.0 y=2.0 CALL swap(x,y) 1.0 a x 2.0 b y SUBROUTINE swap( a, b ) REAL, INTENT(INOUT):: a, b REAL:: temp temp = a a = b b = temp END SUBROUTINE swap 1.0 temp 2.0 a x 1.0 b y WRITE(*,*) "x=", x, “y=", y ! x=2.0 y=1.0 11/24/2018 Comp208 Computers in Engineering
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 :: ActualSize INTEGER :: i READ(*,*) ActualSize READ(*,*) (Data(i), i=1, ActualSize) WRITE(*,*) "Sum = ", Sum(Data, ActualSize) CONTAINS INTEGER FUNCTION Sum(x, n) INTEGER, INTENT(IN):: n INTEGER, DIMENSION(n), INTENT(IN) :: x INTEGER :: Total Total = 0.0 DO i = 1, n Total = Total + x(i) END DO Sum = Total END FUNCTION Sum END PROGRAM Test 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering 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 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. 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering 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 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering 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 WRITE(*,100) “a=“, a 100 FORMAT(A5,I6) WRITE(*,200) “a=“,a FORMAT(A,I4) WRITE(*,300) “a=“,a 300 FORMAT(A,I3) ###a=##1000 A5 I6 a=1000 A I4 a=*** A I3 11/24/2018 Comp208 Computers in Engineering
Comp208 Computers in Engineering FORMAT statement, READ Example INTEGER::a,b READ(*,100) a,b 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 11/24/2018 Comp208 Computers in Engineering
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” END DO CLOSE(10); ! Close file “inputData.txt” FORMAT(A2, I8) END PROGRAM a=######51 a=#######6 … inputData.txt 11/24/2018 Comp208 Computers in Engineering