360 likes | 376 Views
This review covers units of Fortran programs, variables, expressions, statements, control statements, functions, subroutines, and more.
E N D
Midterm ReviewProgramming in Fortran Yi Lin Feb 13, 2007 Comp208 Computers in Engineering
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 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 Comp208 Computers in Engineering
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” 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) Comp208 Computers in Engineering
Evaluate Complex ExpressionArithmetic operators: precedence 2+3*4 = ? = 5 * 4 = 20 Or = 2+12 = 14 3/3**4 = ? Comp208 Computers in Engineering
Evaluation Complex ExpressionArithmetic operators: associativity 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 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 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 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 .FALSE. .TRUE. Log Exp 1st Block 2nd block Stmt following END IF Comp208 Computers in Engineering
Control statements • IF-THEN-END IF Syntax: IF (logical-exp) THEN first statement block, END IF .FALSE. .TRUE. Log Exp 1st Block Stmt following END IF Comp208 Computers in Engineering
Control statements • Logical IF IF (logical-exp) statement .FALSE. .TRUE. Log Exp One statement Stmt following END IF Comp208 Computers in Engineering
Control statements .FALSE. Log Exp1 • 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 .FALSE. .TRUE. Log Exp2 .TRUE. 1st Block 2nd block …… Stmt following END IF 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 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) 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 Comp208 Computers in Engineering
Can be INTEGER, CHARACTER, LOGICAL No REAL (min:max) i.e., 6, 7, 8 (value1, value2) (value1, value2, min:max) SELECT CASE statement example 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 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 block of statements END DO Count=start No Right step Yes Start exceeds stop Yes No Block of statements Count=count+step Next stmt after END DO Comp208 Computers in Engineering
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 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: DO i=3,3 write(*,*) I End do Example 5: i=1 DO WHILE(i<10) write(*,*) I i=i+1 END DO Comp208 Computers in Engineering
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 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. 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: element 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 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]) Comp208 Computers in Engineering
j=3 a(2,3) i=2 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) Comp208 Computers in Engineering
! 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 DO END PROGRAM DO j=1,4 a(1, j)=0 END DO DO j=1,4 a(2, j)=0 END DO DO j=1,4 a(3, j)=0 END DO Two dimensional array, example 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) IMPLICIT NONE [declarations] [statements] [other subprograms] END SUBROUTINE subroutine-name 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. 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. Comp208 Computers in Engineering
a x b y 1.0 2.0 1.0 temp a x b y 2.0 1.0 Argument passing example REAL::x=1, y=2 WRITE(*,*) "x=", x, “y=", y ! X=1.0 y=2.0 CALL swap(x,y) SUBROUTINE swap( a, b ) REAL, INTENT(INOUT):: a, b REAL:: temp temp = a a = b b = temp END SUBROUTINE swap WRITE(*,*) "x=", x, “y=", y ! x=2.0 y=1.0 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::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 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 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. 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 Comp208 Computers in Engineering
A5 I6 ###a=##1000 A I4 a=1000 A I3 a=*** 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) 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 Comp208 Computers in Engineering
a=######51 a=#######6 … inputData.txt 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 Comp208 Computers in Engineering