Midterm Review Programming in Fortran

Download Report

Transcript Midterm Review Programming in Fortran

Midterm Review
Programming in Fortran
Yi Lin
Oct 12, 2006
7/20/2015
Comp208 Computers in Engineering
1
What we have learned

Units of Fortran programs




Control statements (IF-ELSE)



IF-THEN-ELSE-ENDIF
IF-THEN-ELSEIF-THEN-ELSE-ENDIF
Repetition statements (DO LOOP)



Variable
Expression
Statement
Count DO LOOP
INFINITE DO LOOP
Function and Subroutine
7/20/2015
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




7/20/2015
INTEGER
REAL
LOGICAL
.TRUE. .FALSE.
CHARACTER
Comp208 Computers in Engineering
3
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)



7/20/2015
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
4
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.
7/20/2015
Comp208 Computers in Engineering
5
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.
7/20/2015
Comp208 Computers in Engineering
6
Evaluate Complex Expression
Arithmetic operators: precedence
operators
Precedence
()
1
**
2
*, /
3
+, -
4
2+3*4 = ?
= 5 * 4 = 20
Or
= 2+12 = 14
(2+3)*4 = 20
7/20/2015
Comp208 Computers in Engineering
7
Evaluation Complex Expression
Arithmetic operators: associativity
operators
associativity
()
Left to right
**
Right to left
*, /
+, -
7/20/2015
Left to right
Left to right
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
8
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/22)
4.0**(1/2)  1.0 (since ½  0)

7/20/2015
Comp208 Computers in Engineering
9
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.
7/20/2015
==, /=
High
Low
Comp208 Computers in Engineering
10
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
7/20/2015
Comp208 Computers in Engineering
11
Control statements
IF-THEN-ELSE-END IF
Syntax:

.TRUE.
Log Exp
.FALSE.
IF (logical-exp) THEN
first statement block,
ELSE
second statement block,
1st Block
2nd block
END IF
Stmt following
END IF
7/20/2015
Comp208 Computers in Engineering
12
Control statements
IF-THEN-END IF
Syntax:

.TRUE.
Log Exp
.FALSE.
IF (logical-exp) THEN
first statement block,
END IF
1st Block
Stmt following
END IF
7/20/2015
Comp208 Computers in Engineering
13
Control statements
Logical IF
Syntax:

.TRUE.
Log Exp
.FALSE.
IF (logical-exp)
one statement
One statement
Stmt following
END IF
7/20/2015
Comp208 Computers in Engineering
14
Control statements
.FALSE.
IF-THEN-ELSEIF-THEN-ELSELog Exp1
END IF
Syntax:
.FALSE.
.TRUE.
IF (log-exp1) THEN
Log Exp2
first statement block,

.TRUE.
ELSEIF (log-exp2)THEN
second statement block,
……
1st Block
2nd block
……
ELSE
else block
END IF
7/20/2015
Stmt following
END IF
Comp208 Computers in Engineering
15
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!”





7/20/2015
1, 2, 3: Winter
4, 5: Spring
6, 7, 8: Summer
9, 10: Fall
11, 12: Winter
Comp208 Computers in Engineering
16
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
7/20/2015
Comp208 Computers in Engineering
17
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
7/20/2015
Comp208 Computers in Engineering
18
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
7/20/2015
Comp208 Computers in Engineering
19
SELECT CASE statement example
Can be INTEGER, CHARACTER, LOGICAL
No REAL
INTEGER::month
READ(*,*) month !input an integer from keyboard
SELECT CASE(month)
(value1, value2)
CASE (4,5)
WRITE(*,*) “Spring”
(min:max) i.e., 6, 7, 8
CASE (6:8)
WRITE(*,*) “Summer”
CASE (9,10)
WRITE(*,*) “FALL”
(value1, value2,
CASE (11, 12, 1:3) THEN
WRITE(*,*) “WINTER”
CASE DEFALUT
WRITE(*,*) “Not a month!”
END SELECT
7/20/2015
Comp208 Computers in Engineering
min:max)
20
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
7/20/2015
Comp208 Computers in Engineering
21
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
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
7/20/2015
Comp208 Computers in Engineering
22
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
END DO
7/20/2015
Comp208 Computers in Engineering
23
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.
7/20/2015
Comp208 Computers in Engineering
24
Array

An array is a collection of individual data elements,
all of the same type. E.g.,
Index: 1
2
51 6
3
4
5
6
34 61 75 4
7
8
array
53 5
element

The subscript (or index) of an array element is the
position of that element within the array, for example:


7/20/2015
the first element is 51 and has a subscript 1,
the second element is 6 and has a subscript 2.
Comp208 Computers in Engineering
25
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 21 32 43 54 65 76 87
1
INTEGER, DIMENSION(1:8)::a
51 6 34 61 75 4 53 5
INTEGER::a(8)
INTEGER::a(0:7) ! Then 51’s index=0, 6’s index=1, 5’s index=7
7/20/2015
Comp208 Computers in Engineering
26
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
Syntax:
61
53
6
75
5
34
4
10
TYPE, DIMENSION([1lb:][1ub], [2lb:][2ub])::name
TYPE::name([1lb:][1ub], [2lb:][2ub])
7/20/2015
Comp208 Computers in Engineering
27
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)
INTEGER, DIMENSION(1:3, 1:4)::a
INTEGER, DIMENSION(3,4)::a

i=2
a(2,3)
a(i, j): to refer to an element at row i and column
j, e.g., a(2, 3)
7/20/2015
Comp208 Computers in Engineering
28
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 DO
END PROGRAM
7/20/2015
DO j=1,4
a(1, j)=0
END DO
DO j=1,4
a(2, j)=0
END DO
0 0 0 0
0 0 0 0
0 0 0 0
DO j=1,4
a(3, j)=0
END DO
Comp208 Computers in Engineering
29
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
7/20/2015
Comp208 Computers in Engineering
30
Where Do Function/Subroutine
Definitions Go?


INTERNAL
PROGRAM
program-name
IMPLICIT NONE
[declarations]
[statements]
CONTAINS
[function/Subroutine
definitions]
END PROGRAM program-name
External
PROGRAM program-name
IMPLICIT NONE
INTERFACE
[declaration of
function/subroutine]
END INTERFACE
[declarations]
[statements]
END PROGRAM program-name
[Function/subroutine
definitions]
7/20/2015
Comp208 Computers in Engineering
31
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.
7/20/2015
Comp208 Computers in Engineering
32
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.
7/20/2015
Comp208 Computers in Engineering
33
Argument passing example
REAL::x=1, y=2
WRITE(*,*) "x=", x, “y=", y
CALL swap(x,y)
! X=1.0 y=2.0
a
SUBROUTINE swap( a, b )
WRITE(*,*) "x=", x, “y=", y
7/20/2015
b
1.0
REAL, INTENT(INOUT):: a, b
REAL:: temp
temp = a
a=b
b = temp
END SUBROUTINE swap
x
temp
a
y
2.0
1.0
x
2.0
b
y
1.0
! x=2.0 y=1.0
Comp208 Computers in Engineering
34
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)
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
END PROGRAM Test
7/20/2015
Comp208 Computers in Engineering
35
Implied DO Loops
The implied DO loop can simplify this greatly.
INTEGER ::
INTEGER ::
READ(*,*)
READ(*,*)
data(100)
n, i
n
(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.
7/20/2015
Comp208 Computers in Engineering
36
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):
(F3.1,F10.4):
(F3.1,F8.4):


7/20/2015
1.01100.1003
1.0#1100.1003
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
37
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
###a=##1000
100 FORMAT(A5,I6)
WRITE(*,200) “a=“,a
A I4
200
FORMAT(A,I4)
a=1000
WRITE(*,300) “a=“,a
A I3
300 FORMAT(A,I3)

a=***
7/20/2015
Comp208 Computers in Engineering
38
FORMAT statement, READ

Example
100
INTEGER::a,b
READ(*,100) a,b
FORMAT(2I3) ! eqv. To FORMAT(I3,I3)

Correct inputs for (2I3), e.g.,



7/20/2015
“##1##2”
“1##2##”
“#1##2#”
a=##1=1, b=##2=2
a=1##=1, b=2##=2
a=#1#=1, b=#2#=2
Comp208 Computers in Engineering
39
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
…
100
7/20/2015
inputData.txt
Comp208 Computers in Engineering
40