Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm:...
-
Upload
myrtle-cobb -
Category
Documents
-
view
217 -
download
2
Transcript of Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm:...
![Page 1: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/1.jpg)
Introduction to Fortran 90/95 by Stephen J. Chapman
Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40%
Office hours: (PH224) 1. Tues. 11:10~12:00 14:40~15:30 2. Fri. 13:40~15:30
![Page 2: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/2.jpg)
Ch. 1 Introduction To Computers And
The Fortran Language Sec. 1.1 The Computer
Fig 1-1
Main Memory
Secondary Memory
C P U
(Central processing unit)
Input devices
Output devices
![Page 3: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/3.jpg)
Sec 1.2 Data Representation in a Computer
bit : Each switch represents one binary digit.
ON 1
OFF 0
byte : a group of 8 bits
e.g.,
825 MB hard disk. ( 1MB = 106 byte)
Sec 1.2.1 The Binary Number System
The base 10 number system:
1 2 3 10 = 1 x 102 + 2 x 101 + 3 x 100 = 123.
102 101 100
![Page 4: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/4.jpg)
The base 2 number system:
1 0 1 2 = 1 x 22 + 0 x 21 + 1 x 20 = 5
1 1 1 2 = 1 x 22 + 1 x 21 + 1 x 20 = 7
22 21 20
3 bits can represent 8 possible values :
0 ~ 7 (or 0002 ~ 1112 )
![Page 5: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/5.jpg)
In general, n bits 2n possible values.
e.g.,
8 bits ( = 1 byte) 28 = 256. (-128 ~ +127)
16 bits ( = 2 byte) 216 = 65,536. (-32,768 ~ +32,767)
Sec 1.2.2 Types of Data Stored in Memory• Character data : (western language, < 256, use 1 byte)
A ~ Z (26)
a ~ z (26)
0 ~ 9 (10)
Miscellaneous symbols: ( ) { } ! …
Special letters or symbols: à ë …
![Page 6: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/6.jpg)
Coding systems: (see App. A, 8-bit codes) • ASCII (American Standard Code for Information Interchange)• EBCDIC (Extended Binary Coded Decimal Interchange Code)
*The unicode coding system uses 2 bytes for each character. (for any language)
• Integer data: (negative, zero, positive) For an n-bit integer, Smallest Integer value = - 2n-1
Largest Integer value = 2n-1 - 1 e.g., a 4-byte (= 32-bit) integer, the smallest = -2,147,483,648 ( = - 232-1) the largest = 2,147,483,647 ( = 232-1-1)
*Overflow condition: An integer > the largest or < the smallest.
![Page 7: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/7.jpg)
• Real data: (or floating-point data) The base 10 system: 299,800,000 = 2.998 x 108 (scientific notation)
mantissa exponent
The base 2 system: e.g., a 4-byte real number = 24-bit mantissa + 8-bit exponent
value = mantissa x 2exponent
Precision: The number of significant digits that can be preserved in a number. e.g., 24-bit mantissa ± 223 (~ seven significant digits)
Range: The diff. between the largest and the smallest numbers. e.g., 8-bit exponent 2-128 ~ 2127 (range ~ 10-38 to 1038)
![Page 8: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/8.jpg)
Sec 1.3 Computer Languages• Machine language: The actual language that a computer recognizes
and executes.
• High-level languages: Basic, C, Fortran, …
*The History of the Fortran Language
Fortran = Formula translation
Fortran 66 Fortran 77 Fortran 90 Fortran 95
(1966) (1977) (1991) (1997)
![Page 9: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/9.jpg)
Sec. 2.3 The Structure of a Fortran Statement
A Fortran program = a series of statements
• Executable statements: e.g., additions, subtractions, …• Non-executable statements: providing information.
Free-source form: Fortran statements may be entered anywhere on a line, up to 132 characters long.
e.g., 100 output = input1 + input2 ! Sum the inputs or 100 output = input1 & ! Sum the inputs + input2
![Page 10: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/10.jpg)
Sec. 2.4 The Structure of a Fortran Program
Fig 2-1 (A simple Fortran program)
PROGRAM my_first_program
! Purpose: …
! Declare the variables
INTEGER :: i, j, k !All variable are integers
! Get the variables WRITE (*,*) " Enter the numbers to multiply:" READ (*,*) i, j k = i * j ! Write out the result WRITE (*,*) 'Result = ', k STOP END PROGRAM
Declarationsection
Executionsection
Terminationsection
![Page 11: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/11.jpg)
Sec. 2.4.4 Program Style
Text book : • Capitalizing Fortran keywords ( e.g., READ, WRITE)• Using lowercase for variables, parameters
Sec. 2.4.5 Compiling, Linking, and Executing the Fortran Program
Fig 2-2
Fortran program
Object file
Executable program
(Compile) (Link)
![Page 12: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/12.jpg)
Sec. 2.5 Constants and Variables
Valid variable names: time distance z123456789 I_want_to_go_home(up to 31 chracters, and the 1st character in a name must always be alphabetic)
Invalid variable names: this_is _a_very_long_variable_name 3_days A$ ($ is an illegal character) my-help (“-” is an illegal character)
![Page 13: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/13.jpg)
Five intrinsic types of Fortran constants and variables:1. INTEGER2. REAL3. COMPLEX4. LOGICAL5. CHARACTER
(numeric)
(logical)(character)
Sec. 2.5.1 Integer Constant and Variables
Integer constants: (no decimal point) e.g., 0 -999 +17 1,000,000 (X) -100. (X)
![Page 14: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/14.jpg)
Integer variables: 16-bit integers 32-bit integers(diff. kinds of integers, Ch. 7)
Sec. 2.5.2 Real Constants and Variables Real constants: (with a decimal point) e.g., 10. -999.9 1.0E-3 (= 1.0 x 10-3 or 0.001) 123.45E20 0.12E+1 1,000,000. (X) 111E3 (X) -12.0E1.5 (X)
![Page 15: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/15.jpg)
Real variables: 32-bit real numbers 64-bit real numbers(diff. kinds of real numbers, Ch. 7)
Sec. 2.5.3 Character Constants and Variables Character constants: [enclosed in single (‘) or double (“) quotes)] e.g., ‘This is a test!’ “This is a test!” ‘ ‘ (a single blank) ‘{^}’ ‘3.141593’ (not a number) This is a test! (X) ‘This is a test!” (X)
A character variable contains a value of the character data type.
![Page 16: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/16.jpg)
Sec. 2.5.4 Logical Constants and Variables Character constants: e.g., .TRUE. .FALSE.
TRUE (X) .FALSE (X)
A logical variable contains a value of the logical data type.
Sec. 2.5.5 Default and Explicit Variable Typing Default typing: Any variable names beginning with the letters I, J, K, L, M, or N are assumed to be of type INTEGER. e.g., incr (integer data type) big (real data type)
![Page 17: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/17.jpg)
Explicit typing: The type of a variable is explicitly defined in the declaration section. e.g., PROGRAM example INTEGER :: day, month, year REAL :: second LOGICAL :: test1, test2 CHARACTER :: initial
(Executable statements)
*No default names for the character data type!
![Page 18: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/18.jpg)
Sec. 2.5.6 Keeping Constants Consistent in a Program Using the PARAMETER attribute : type, PARAMETER :: name=value e.g., REAL, PARAMETER :: pi=3.14159 CHARACTER, PARAMETER :: error=‘unknown’
Sec. 2.6 Assignment Statements and Arithmetic Calculations Assignment statement: variable_name = expression e.g., I = I + 1 ( I + 1 I )
![Page 19: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/19.jpg)
Arithmetic operators:• binary operators: a + b (a + b, addition) a – b (a – b, subtraction) a * b (a x b, multiplication) a / b (a/b, division) a ** b (ab, exponentiation)• unary operators: + a - b
Rules:
1. No two operators may occur side by side. e.g., a*-b (X) a*(-b) a**-2 (X) a**(-2)
![Page 20: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/20.jpg)
2. Implied multiplication is illegal. e.g., x (y + z) (X) x*(y + z)
3. Parentheses may be used to group terms whenever desired e.g.,
2**((8+2) / 5)
Sec. 2.6.1 Integer Arithmetic e.g., 3/4 = 0, 6/4 = 1 7/4 = 1, 9/4 = 2
Sec. 2.6.2 Real Arithmetic (or floating-point arithmetic) e.g., 3./4. = 0.75, 6./4. = 1.50 7./4. = 1.75, 9./4. = 2.25
![Page 21: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/21.jpg)
Sec. 2.6.3 Hierarchy (order) of Operators e.g., x = 0.5 * a * t **2 is equal to x = 0.5 * a * (t **2) ? or x = (0.5 * a * t ) **2 ?
Order: 1. Parentheses, from inward to outward.2. Exponentials, from right to left.3. Multiplications and divisions, from left to right.4. Additions and subtractions, from left to right.
![Page 22: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/22.jpg)
Example 2-1 a = 3. b = 2. c=5. d=4. e = 10. f = 2. g= 3.(1) output = a * b + c * d + e / f **g(2) output = a * (b + c) * d + (e / f) **g(3) output = a * (b + c) * (d + e) / f **g
Sol. (1) output = 3. * 2. + 5. * 4. + 10. / 2. ** 3. = 6. + 20. + 1.25 = 27.25 (2) output = 3. * (2. + 5.) * 4. + (10. / 2.) ** 3. = 84. + 125. = 209. (3) output = 3. * (2. + 5.) * (4. + 10.) / 2. ** 3. = 3. * 7. * 14. / 8. = 294. / 8. = 36.75
![Page 23: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/23.jpg)
Example 2-2 a = 3. b = 2. c=3. (1) output = a ** (b ** c)(2) output = (a ** b) ** c(3) output = a ** b ** c
Sol. (1) output = 3. ** (2. ** 3.) = 3. ** 8. = 6561. (2) output = (3. ** 2.) ** 3. = 9. ** 3. = 729. (3) output = 3. ** 2. ** 3. = 3. ** 8. = 6561.
![Page 24: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/24.jpg)
Sec. 2.6.4 Mixed-Mode Arithmetic In the case of an operation between a real number and an integer,the integer is converted by the computer into a real number. e.g., 3. / 2 = 1.5 1 + 1/4 = 1 1. + 1/4 = 1. 1 + 1./4 = 1.25
Automatic type conversion:
e.g., nres = 1.25 + 9/4 ave = (5 + 2) / 2 = 1.25 + 2 = 7/2 = 3.25 = 3. = 3
(a integer variable) (a real variable)
![Page 25: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/25.jpg)
Logarithm
• Base 10: If 10x = N, then x = ? log N = x e.g., N = 100 log 100 = log (102) = 2
N = 3 log 3 = 0.47712…
• Base e (=2.71828…): (Natural logarithm)
If ex = N, then x = ? ln N = x e.g., N = e2 ln (e2) = 2
N = 3 ln 3 = 1.09861…
* If N < 0 ( log N ) or ( ln N ) is undefined !
![Page 26: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/26.jpg)
Sec. 2.6.5 Mixed-Mode Arithmetic and Exponentiation
If result and y are real, and n is an integer,
result = y ** n = y * y * y…*y (real arithmetic, not mixed-mode)
But if result, y and x are real,
result = y ** x = ?
![Page 27: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/27.jpg)
use yx = e x ln y ( e ∵ x ln y = e ln (yx) = yx )
e.g., (4.0) ** 1.5 = 8.
(8.0)**(1./3)=2.
(-2.0) ** 2 = 4. [ (-2.0) * (-2.0) = 4.]∵
(-2.0) ** 2.0 [X, ln (-2.0) is undefined!]∵
![Page 28: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/28.jpg)
Sec. 2.7 Assignment Statements and Logical Calculations Assignment statements: logical variable name = logical expression
Logical operators: • relational operators• combinational operators
Sec. 2.7.1 Relational Operators
a1 op a2
a1, a2: arithmetic expressions, variables, constants, or character strings.op: the relational logical operators. (see Table 2-3)
![Page 29: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/29.jpg)
Table 2-3
operation meaning
= = equal to / = not equal to > greater than > = greater than or equal to < less than < = less than or equal toe.g., operation result 3 < 4 .TRUE. 3 < = 4 .TRUE. 3 = = 4 .FALSE. ‘A’ < ‘B’ .TRUE. (in ASCII, A 65, B 66, p.493) 7+3 < 2+11 .TRUE.
![Page 30: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/30.jpg)
Sec. 2.7.2 Combinational Logic Operators
l1 .op. l2 and .NOT. l1 (.NOT. is a unary operator)
l1, l2: logical expressions, variables, or constants.op: the binary operators. (see Table 2-4)
Table 2-4
operation meaning
.AND. logical AND .OR. logical OR .EQV. logical equivalence .NEQV. logical non-equivalence .NOT. logical NOT
![Page 31: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/31.jpg)
The order of operations:
1. Arithmetic operators.
2. All relational operators, from left to right.
3. All .NOT. operators.
4. All .AND. operators, from left to right.
5. All .OR. operators, from left to right.
6. All .EQV. And .NEQV. operators, from left to right.
![Page 32: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/32.jpg)
Example 2-3
L1 = .TRUE., L2 = .TRUE., L3 = .FALSE. (a) .NOT. L1 .FALSE.
(b) L1 .OR. L3 .TRUE.
(c) L1 .AND. L3 .FALSE.
(d) L2 .NEQV. L3 .TRUE.
(e) L1 .AND. L2 .OR. L3 .TRUE.
(f) L1 .OR. L2 .AND. L3 .TRUE.
(g) .NOT. (L1 .EQV. L2) .FALSE.
![Page 33: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/33.jpg)
Sec. 2.7.3 The Significance of Logical Variables and Expressions
Most of the major branching and looping structures of Fortran are controlled by logical values.
Sec. 2.8 Assignment Statements and Character Variables
character variables name = character expression
Character operators:1. substring specifications2. concatenation
![Page 34: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/34.jpg)
Sec. 2.8.1 Substring SpecificationsE.g., str1 = ‘123456’ str1(2:4) contains the string ‘234’.
Example 2-4 PROGRAM substring CHARACTER (len=8) :: a,b,c a = ‘ABCDEFGHIJ’ b = ‘12345678’ c = a(5:7) b(7:8) = a(2:6) WRITE(*,*) 'a=', a WRITE(*,*) 'b=', b WRITE(*,*) 'c=', c END PROGRAM
a = ? b = ? c = ? (Try it out!)
![Page 35: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/35.jpg)
Solu: a = ‘ABCDEFGH’ ( len = 8)∵
∵ b(7:8) = a(2:6) = ‘BC’
b = ‘123456BC’
c = a(5:7) = ‘EFG’ = ‘EFG□□□□□‘ ( len = 8)∵
(Cont.)
![Page 36: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/36.jpg)
Sec. 2.8.2 The Concatenation Operator
E.g., PROGRAM concate CHARACTER (len=10) :: a CHARACTER (len=8) :: b,c a = ‘ABCDEFGHIJ’ b = ‘12345678’ c = a(1:3) // b(4:5) // a(6:8) WRITE(*,*)’c=‘,c END PROGRAM
c = ? (Try it out: c =‘ABC45FGH’)
![Page 37: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/37.jpg)
Sec. 2.8.3 Relational Operators with Character Data
E.g., ‘123’ = = ‘123’ (true)
‘123’ = = ‘1234’ (false)
‘A’ < ‘B’ (true, ASCII, A 65, B 66)∵
‘a’ < ‘A’ (false, a 97)∵
‘AAAAAB’ > ‘AAAAAA’ (true)
‘AB’ > ‘AAAA’ (true)
‘AAAAA’ > ‘AAAA’ (true)
![Page 38: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/38.jpg)
Sec. 2.9 Intrinsic Functions• Intrinsic functions are the most common functions built directly into the Fortran language. ( see Table 2-6 and App. B)• External functions are supplied by the user. (see Ch. 6)
e.g., y = sin(3.141593) INT(2.9995) = 2 (truncates the real number) y = sin(x)
y = sin(pi*x) NINT(2.9995) = 3 (rounds the real number) y = sin(SQRT(x))
![Page 39: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/39.jpg)
Generic functions: (can use more than one type of input data) e.g., If x is a real number, ABS(x) is real. If x is an integer, ABS(x) is integer.
Specific functions: (can use only one specific type of input data) e.g., IABS(i)
(integer only)
*See Appendix B for a complete list of all intrinsic functions.
![Page 40: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/40.jpg)
Sec. 2.10 List-directed (or free-format) Input and Output Statements
• The list-directed input statement: READ (*,*) input_list
I/O unit format
• The list-directed output statement: WRITE (*,*) output_list I/O unit format
![Page 41: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/41.jpg)
e.g., PROGRAM input_example INTEGER :: i, j REAL :: a CHARACTER (len=12) :: chars READ(*,*) i, j, a, chars WRITE(*,*) i, j, a, chars END PROGRAM
Input: 1, 2, 3., ‘This one.’ (or 1 2 3. ‘This one.’)
Output: 1 2 3.00000 This one.
(Try it out!)
![Page 42: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/42.jpg)
Sec. 2.11 Initialization of Variables
E.g., PROGRAM init INTEGER :: i WRITE(*,*) I END PROGRAM
Output: i = ??? (uninitialized variable)
Run-time error! (depends on machines)
(Try it out!)
![Page 43: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/43.jpg)
Three ways to initialize variables:
1. Assignment statements: e.g., PROGRAM init_1 INTEGER :: i i = 1 WRITE(*,*) i END PROGRAM
2. READ statements: e.g., PROGRAM init_2 INTEGER :: i READ(*,*) i WRITE(*,*) i END PROGRAM
![Page 44: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/44.jpg)
3. Type declaration Statements: type :: var1 = value1, [var2 = value2, …]
e.g., REAL :: time = 0.0, distance = 5128. INTEGER :: loop = 10 LOGICAL :: done = .FALSE. CARACTER (len=12) :: string = ‘characters’
or
PROGRAM init_3 INTEGER :: i = 1 WRITE(*,*) i END PROGRAM
![Page 45: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/45.jpg)
Sec. 2.12 The IMPLICIT NONE Statement
When the IMPLICIT NONE statement is included in a program, any variable that does not appear in an explicit type declaration statement is considered an error. e.g., PROGRAM test_1 REAL :: time time = 10.0 WRITE(*,*) ‘Time=‘, tmie END PROGRAM
Output:
Run-time error! (depends on machines)
![Page 46: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/46.jpg)
+ IMPLICIT NONE,
PROGRAM test_1 IMPLICIT NONE REAL :: time time = 10.0 WRITE(*,*) ‘Time=‘, tmie END PROGRAM
Output:
Compile-time error! (depends on machines)
![Page 47: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/47.jpg)
Sec. 2.13 Program Examples
Example 2-5 (Temperature conversion)
T (0F) = (9/5) T(0C) + 32
Fig. 2-6 PROGRAM temp IMPLICIT NONE REAL :: temp_c, temp_f WRITE(*,*) ’Enter T in degrees C:’ READ(*,*) temp_c temp_f = (9./5.) * temp_c + 32. WRITE(*,*) temp_c,’ degrees C =‘, temp_f, & ‘degrees F’ END PROGRAM
(Try it out!)
![Page 48: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/48.jpg)
Example (extra)
Write a program for converting a 4 bits integer into a base 10 number, e.g.,
1 0 1 1 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 11
![Page 49: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/49.jpg)
Ch. 3 Control Structures and Program Design
Ch. 2: Sequential programs (simple and fixed order)
Here: Complex programs (using two control statements)
(1) branches
(2) loops
![Page 50: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/50.jpg)
Sec. 3.1 Introduction to Top-down Design Techniques
Start
State the problem
Design thealgorithm
Convert algorithm into Fortran statements
Test the program
Finished !
Fig. 3-1 (a formal program design process)
![Page 51: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/51.jpg)
Sec. 3.2 Pseudocode and Flowcharts
(1) Pseudocode : a mixture of Fortran and English
(2) Flowcharts : graphical symbolsl
e.g.,
(1) The pseudocode for Example 2-5: Prompt user to enter temp. in degree Farenheit Read temp. in degree Farenheit temp_k in Kelvins (5./9.)*(temp_f-32.)+273.15 Write temp. in Kelvins
![Page 52: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/52.jpg)
(2) The flowcharts for Example 2-5:
Start
Tell user toenter temp_f
Get temp_f
Calculate temp_k Write temp_k Stop
(an oval for start or stop)
(a parallelogram for I/O)
(a rectangle for computation)
![Page 53: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/53.jpg)
Sec. 3.3 Control Constructs: Branches
• IF Statement
• SELECT CASE
Branches are Fortran statements that permit us to select and execute specific sections of code (called blocks) while skipping other sections of code.
![Page 54: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/54.jpg)
Sec. 3.3.1 The Block IF Construct
This construct specifies that a block of code will be executed if and only if a certain logical expression is true.
IF (logical_expr) THEN Statement 1 Statement 2 . . .END IF
a block
![Page 55: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/55.jpg)
Fig . 3-5 (Flowchart for a simple block IF construct)
logical_expr
Statement 1Statement 2 . .
.TRUE.
.FALSE.
(a diamond for choice)
![Page 56: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/56.jpg)
Example:
ax2 + bx + c = 0,
x = -b ± ( b2 – 4ac )1/2
2a
If b2 – 4ac = 0
b2 – 4ac > 0
b2 – 4ac < 0
two distinct real roots
two complex roots
a single repeated root
![Page 57: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/57.jpg)
Fig . 3-6 (Flowchart)
.TRUE.
.FALSE.
Problem: Tell the user if the eq. has complex roots.
b2 – 4ac < 0
WRITE ‘two complex roots’
![Page 58: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/58.jpg)
Fortran: IF ( (b**2 – 4.*a*c) < 0. ) THEN WRITE(*,*) ‘Two complex roots!’ END IF
Sec. 3.3.2 The ELSE and ELSE IF Clauses
For many different options to consider,
IF + ELSE IF (one or more) + an ELSE
![Page 59: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/59.jpg)
IF (logical_expr_1) THEN Statement 1 Statement 2 . .ELSE IF (logical_expr_2) THEN Statement 1 Statement 2 . .ELSE Statement 1 Statement 2 . .END IF
Block 1
Block 2
Block 3
![Page 60: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/60.jpg)
Fig . 3-7 (flowchart)
.TRUE.
.FALSE.logical_expr_1
Block 1
logical_expr_2
Block 2
.TRUE.
.FALSE.
Block 3
![Page 61: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/61.jpg)
Fig . 3-8 (flowchart)
.TRUE.
.FALSE.b2 - 4ac < 0
WRITE ‘two complex roots’
.TRUE.
.FALSE.
Example: Tell the user whether the eq. has two complex roots, two identical real roots, or two distinct real roots.
b2 - 4ac = 0
WRITE ‘two identical real roots’
WRITE ‘two distinct real roots’
![Page 62: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/62.jpg)
Fortran: IF ( (b**2 – 4.*a*c) < 0. ) THEN WRITE(*,*) ‘two complex roots’ ELSE IF ( (b**2 – 4.*a*c) == 0. ) THEN WRITE(*,*) ‘two identical real roots’ ELSE WRITE(*,*) ‘two distinct real roots’ END IF
Write a complete Fortran program for a quadraticequation ax2 + bx + c = 0.
Input: a, b, c (e.g., 1., 5., 6. or 1., 4., 4. or 1., 2., 5.)
Output: ‘distinct real’ or ‘identical real’ or ‘complex roots’
(Try it out!)
![Page 63: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/63.jpg)
PROGRAM abcIMPLICIT NONEREAL :: a, b, c
WRITE(*,*)'Enter the coeffs. a, b, and c:‘READ(*,*) a, b, c
IF ( (b**2-4.*a*c) < 0. ) THEN WRITE(*,*) 'two complex root‘ELSE IF ( (b**2-4.*a*c) == 0. ) THEN WRITE(*,*) 'two identical real roots‘ELSE WRITE(*,*) 'two distinct real roots‘END IF
END PROGRAM
![Page 64: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/64.jpg)
Sec. 3.3.3 Examples Using Block IF Constructs
Example 3-1 The Quadratic Equation: (ax2 + bx + c =0) Write a program to solve for the roots of a quadratic equation, regardless of type.
Input: a, b, c
Output: rootsrealrepeated realcomplex
![Page 65: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/65.jpg)
PROGRAM rootIMPLICIT NONEREAL :: a, b, c, d, re, im, x1, x2
WRITE(*,*)'Enter the coeffs. a, b, and c:‘READ(*,*) a, b, cd = b**2 – 4.*a*cIF ( d < 0. ) THEN WRITE(*,*) 'two complex root:‘ re = (-b)/(2.*a) im = sqrt(abs(d))/(2.*a) WRITE(*,*) ’x1=‘, re, ‘+ i’, im WRITE(*,*) ’x2=‘, re, ‘- i’, im ELSE IF ( d == 0. ) THEN WRITE(*,*) 'two identical real roots:‘ x1 = (-b) / (2.*a) WRITE(*,*) ’x1=x2=‘, x1
![Page 66: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/66.jpg)
ELSE WRITE(*,*) 'two distinct real roots:‘ x1 = (-b + sqrt(d)) / (2.*a) x2 = (-b – sqrt(d)) / (2.*a) WRITE(*,*) ’x1=‘, x1 WRITE(*,*) ‘x2=‘, x2END IFEND PROGRAM
Test: (Try it out!)
x2 + 5x + 6 = 0, x1,2 = -2, -3
x2 + 4x + 4 = 0, x1,2 = -2
x2 + 2x + 5 = 0, x1,2 = -1 ± i 2
![Page 67: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/67.jpg)
Example 3-2 Evaluation a Function of Two Variables:
f(x,y) =
x + y, x 0 ≧ and y 0≧x + y2, x 0 ≧ and y < 0x2 + y, x < 0 and y 0≧x2 + y2, x < 0 and y < 0
Input: x, y
Output: f
![Page 68: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/68.jpg)
Fig . 3-11 (flowchart)
.TRUE.
.FALSE.x ≧ 0 &y 0≧
f = x + y
x 0 ≧ &y < 0
f = x2 + y
.TRUE.
.FALSE.
f = x2 + y2f = x + y2
x < 0 &y 0≧
.TRUE.
.FALSE.
WRITE f
Stop
Start
READ x, y
![Page 69: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/69.jpg)
PROGRAM funxyIMPLICIT NONEREAL :: x, y, f
WRITE(*,*)'Enter x and y:‘READ(*,*) x, yIF ((x >= 0.) .AND. (y >= 0. )) THEN f = x + yELSE IF ((x >= 0.) .AND. (y < 0. )) THEN f = x + y**2ELSE IF ((x < 0.) .AND. (y >= 0. )) THEN f = x**2 + yELSE f = x**2 + y**2END IFWRITE(*,*) ‘f = ‘, fEND PROGRAM
![Page 70: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/70.jpg)
Test: (Try it out!)
x y f
2. 3. 5. 2. -3. 11.-2. 3. 7.-2. -3. 13.
![Page 71: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/71.jpg)
[name:] IF (logical_expr_1) THEN Statement 1 Statement 2 . .ELSE IF (logical_expr_2) THEN [name] Statement 1 Statement 2 . .ELSE [name] Statement 1 Statement 2 . .END IF [name]
Block 1
Block 2
Block 3
Sec. 3.3.4 Named Block IF Constructs
optional
optional
![Page 72: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/72.jpg)
Sec. 3.3.5 Notes Concerning the Use of Logical IF Constructs
Nested IF Constructs:
outer: IF ( x > 0. ) THEN . . inner: IF ( y < 0. ) THEN . . END IF inner . .END IF outer
![Page 73: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/73.jpg)
Example 3-3 Assigning Letter Grades:
95 < GRADE A86 < GRADE < 95 B76 < GRADE < 86 C66 < GRADE < 76 D 0 < GRADE < 66 F
Input: grade
Output: ‘The grade is A.’ or ‘The grade is B.’ or ‘The grade is C.’ or ‘The grade is D.’ or ‘The grade is F.’
![Page 74: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/74.jpg)
Method (a): IF + ELSE IF
IF (grade > 95.) THEN WRITE(*,*) ‘The grade is A.’ELSE IF (grade > 86.) THEN WRITE(*,*) ‘The grade is B.’ELSE IF (grade > 76.) THEN WRITE(*,*) ‘The grade is C.’ELSE IF (grade > 66.) THEN WRITE(*,*) ‘The grade is D.’ELSE WRITE(*,*) ‘The grade is F.’END IF
![Page 75: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/75.jpg)
Method (b): nested IF
if1: IF (grade > 95.) THEN WRITE(*,*) ‘The grade is A.’ELSE if2: IF (grade > 86.) THEN WRITE(*,*) ‘The grade is B.’ ELSE if3: IF (grade > 76.) THEN WRITE(*,*) ‘The grade is C.’ ELSE if4: IF (grade > 66.) THEN WRITE(*,*) ‘The grade is D.’ ELSE WRITE(*,*) ‘The grade is F.’ END IF if4 END IF if3 END IF if2END IF if1
![Page 76: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/76.jpg)
Sec. 3.3.6 The Logical IF Statement
IF (logical_expr) Statement
e.g., IF ( (x >= 0.) .AND. (y >= 0.) ) f = x + y
![Page 77: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/77.jpg)
[name:] SELECT CASE (case_expr)CASE (case_selector_1) [name] Statement 1 Statement 2 . . .CASE (case_selector_2) [name] Statement 1 Statement 2 . . .CASE DEFAULT [name] Statement 1 Statement 2 . . .END SELECT [name]
Block 1
Block 2
Block 3
Sec. 3.3.7 The CASE Construct
optional
optional
optional
![Page 78: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/78.jpg)
case_expr: an integer, character, or logical expression.
The case_selector can take one of four forms:
1. case_value Execute block if case_value == case_expr2. low_value: Execute block if low_value <= case_expr3. : high_value: Execute block if case_expr <= high_value4. low value: high_value Execute block if low_value <= case_expr <= high_value
![Page 79: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/79.jpg)
Fig . 3-13 (flowchart for a CASE construct)
In range
Not in range
case_sel_1
Block 1
case_sel_2
Block n
In range
NotIn range
Default BlockBlock 2
case_sel_n
In range
Not in range
![Page 80: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/80.jpg)
e.g., (modified)
REAL :: temp_c. . .temp: SELECT CASE (temp_c)CASE (: -1.0) WRITE (*,*) ‘ It’s below freezing today!’CASE (0.0) WRITE (*,*) ‘ It’s exactly at the freezing point!’CASE (1.0:20.0) WRITE (*,*) ‘ It’s cool today!’CASE (21.0:33.0) WRITE (*,*) ‘ It’s warm today!’CASE (34.0:) WRITE (*,*) ‘ It’s hot today!’END SELECT temp
![Page 81: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/81.jpg)
PROGRAM selectcIMPLICIT NONEINTEGER :: temp_cWRITE(*,*) “Enter today’s temp. in degree C:”READ(*,*) temp_ctemp: SELECT CASE (temp_c)CASE (: -1) WRITE (*,*) “It’s below freezing today!”CASE (0) WRITE (*,*) “It’s exactly at the freezing point!”CASE (1:20) WRITE (*,*) “It’s cool today!”CASE (21:33) WRITE (*,*) “It’s warm today!”CASE (34:) WRITE (*,*) “It’s hot today!”END SELECT tempEND PROGRAM
![Page 82: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/82.jpg)
Problem: Determine whether an integer between 1 and 10 is even or odd. (Try it out!)
PROGRAM selectvINTEGER :: valueWRITE(*,*) 'Enter an inter between 1-10:'READ(*,*) valueSELECT CASE (value) CASE (1,3,5,7,9) WRITE(*,*) 'The value is odd.' CASE (2,4,6,8,10) WRITE(*,*) 'The value is even.' CASE (11:) WRITE(*,*) 'The value is too high' CASE DEFAULT WRITE(*,*) 'The value is negative or zero.'END SELECTEND PROGRAM
![Page 83: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/83.jpg)
Sec. 3.4 Control Constructs: Loops
• while loops• iterative (or counting) loops
Sec. 3.4.1 The While Loop
DO . . . IF (logical_expr) EXIT . . .END DO
a code block
![Page 84: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/84.jpg)
Fig . 3-14 (Flowchart for a while loop)
.TRUE.
.FALSE.
logical_expr
Statement 1 . . .
Statement 1 . . .
![Page 85: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/85.jpg)
Example 3-4 Statiscal Analysis:
Average: x_ave =
Σxii=1
N
N
Standard deviation:
S = N Σxi
2 – (i=1 i=1
N N
Σxi )2
N (N-1)
1/2
Input: x (i.e., xi , i = 1, 2, …, N) 0 ≧
Output: x_ave and S
![Page 86: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/86.jpg)
Fig . 3-15 (Flowchart for Example 3-4)
.TRUE.
.FALSE.
x < 0
READ x
n = n + 1sum_x = sum_x + xsum_x2=sum_x2 + x2
Start
1
1
Calculate x_ave, s
Stop
WRITE x_ave, s, n
Initial values:n = 0sum_x = 0Sum_x2 = 0
![Page 87: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/87.jpg)
Fig . 3-16 PROGRAM stats_1IMPLICIT NONEINTEGER :: n = 0REAL :: x, x_ave, s , sum_x = 0., sum_x2 = 0.
DO WRITE(*,*) ‘Enter the value x:’ READ (*,*) x IF ( x < 0. ) EXIT n = n + 1 sum_x = sum_x + x sum_x2 = sum_x2 + x**2END DOx_ave = sum_x / ns = sqrt (( n*sum_x2 – sum_x**2) / (n*(n-1)))WRITE(*,*) ‘n = ‘ , n, ‘ x_ave = ‘, x_ave, & ‘ s = ‘, sEND PROGRAM
![Page 88: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/88.jpg)
Test:
Input: 3. 4. 5. -1.
Output: n = 3 x_ave = 4.00000 s = 1.00000
![Page 89: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/89.jpg)
Sec. 3.4.2 The Iterative Counting Loop
DO index = istart, iend, incr Statement 1 . . . Statement nEND DO
e.g.,
(1) Do i = 1, 10 Statement 1 . . . Statement nEND DO
( incr = 1 by default)
(2) Do i = 1, 10, 2 Statement 1 . . . Statement nEND DO
( i = 1, 3, 5, 7, 9 )
![Page 90: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/90.jpg)
Fig. 3-18 (Flowchart for a Do loop construct)
index*incr ≦iend * incr
index =istart
incr
Statement 1Statement 2 . . .
.TRUE..FALSE.
![Page 91: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/91.jpg)
Example 3-5 The Factorial Function:
N ! = N × (N-1) × (N-2) … × 3 × 2 × 1, N > 0.0 ! = 1
e.g.,
4 ! = 4 × 3 × 2 × 1 = 24 5 ! = 5 × 4 × 3 × 2 × 1 = 120
Fortran Code: n_factorial = 1DO i = 1, n n_factorial = n_factorial * iEND DO
![Page 92: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/92.jpg)
Problem: Write a complete Fortran program for the factorial function.
Input: n ( n > = 0 )
N ! = N × (N-1) × (N-2) … × 3 × 2 × 1, N > 0.
0 ! = 1
Output: n!
![Page 93: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/93.jpg)
PROGRAM factorialIMPLICIT NONEINTEGER :: i, n, n_fact
WRITE(*,*) ’Enter an integer n ( > = 0 ):’READ(*,*) nn_fact = 1IF ( n > 0 ) THEN DO i = 1, n n_fact = n_fact * i END DOEND IF
WRITE(*,*) n, ‘! = ‘, n_factEND PROGRAM
![Page 94: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/94.jpg)
Fig . 3-20
.TRUE. .FALSE.n < 2
READ n
sum_x = sum_x + xsum_x2 = sum_x2+x2
Start
1
1
Calculate x_ave, s
Stop
WRITE x_ave, s, n
Initial values:sum_x = 0sum_x2 = 0
Example 3-7 Statistical Analysis: (modified)
WRITE‘At least 2 values!’
i=1
i=i+1i n≦ ?
READ x
.FALSE. .TRUE.
![Page 95: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/95.jpg)
Fig . 3-21PROGRAM stats_3IMPLICIT NONEINTEGER :: i, nREAL :: x, x_ave, s , sum_x = 0., sum_x2 = 0.
WRITE(*,*) ‘Enter the number of points n:’READ(*,*) nIF ( n < 2 ) THEN WRITE(*,*) ‘ At least 2 values!’ELSE DO i = 1, n WRITE(*,*) ‘Enter the value x:’ READ (*,*) x sum_x = sum_x + x sum_x2 = sum_x2 + x**2 END DO
![Page 96: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/96.jpg)
x_ave = sum_x / ns = sqrt (( n*sum_x2 – sum_x**2) / (n*(n-1)))WRITE(*,*) ‘n = ‘ , n, ‘ x_ave = ‘, x_ave, & ‘ s = ‘, sEND IFEND PROGRAM
Test:
Input: 3 3. 4. 5.
Output: n = 3 x_ave = 4.00000 s = 1.00000
![Page 97: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/97.jpg)
Sec. 3.4.3 The CYCLE and EXIT Statements
E.g., PROGRAM test_cycleINTEGER :: iDO i = 1, 5 IF ( i == 3 ) CYCLE WRITE(*,*) iEND DOWRITE(*,*) ‘End of loop!’END PROGRAM
Output: 1 2 4 5 End of loop!
![Page 98: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/98.jpg)
PROGRAM test_exitINTEGER :: IDO i = 1, 5 IF ( i == 3 ) EXIT WRITE(*,*) iEND DOWRITE(*,*) ‘End of loop!’END PROGRAM
Output: 1 2 End of loop!
![Page 99: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/99.jpg)
[name:] DO . . . IF (logical_expr) CYCLE [name] . . . IF (logical_expr) EXIT [name] . . . END DO [name]
Sec. 3.4.4 Named Loops
While loop:
optional
![Page 100: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/100.jpg)
[name:] DO index = istart, iend, incr . . . IF (logical_expr) CYCLE [name] . . . END DO [name]
Counting loop:
optional
![Page 101: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/101.jpg)
Sec. 3.4.5 Nesting Loops and Block IF Construct
e.g.,PROGRAM nested_loopsINTEGER :: i, j, productDO i = 1, 3 DO j = 1, 3 product = i * j WRITE(*,*) i, ‘*’, j, ‘=‘, product END DOEND DOEND PROGRAM
![Page 102: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/102.jpg)
Output:
1 * 1 = 11 * 2 = 21 * 3 = 32 * 1 = 22 * 2 = 42 * 3 = 63 * 1 = 33 * 2 = 63 * 3 = 9
![Page 103: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/103.jpg)
PROGRAM test_cycle_1INTEGER :: i, j, productDO i = 1, 3 DO j = 1, 3 IF ( j == 2 ) CYCLE product = i * j WRITE(*,*) i, ‘*’, j, ‘=‘, product END DOEND DOEND PROGRAM
Output: 1 * 1 = 11 * 3 = 32 * 1 = 22 * 3 = 63 * 1 = 33 * 3 = 9
![Page 104: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/104.jpg)
PROGRAM test_cycle_2INTEGER :: i, j, productouter: DO i = 1, 3 inner: DO j = 1, 3 IF ( j == 2 ) CYCLE outer product = i * j WRITE(*,*) i, ‘*’, j, ‘=‘, product END DO innerEND DO outerEND PROGRAM
Output:1 * 1 = 12 * 1 = 23 * 1 = 3
![Page 105: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/105.jpg)
Nesting loops within IF constructs and vice versa:
e.g.,outer: IF ( a < b ) THEN . . . inner: DO i = 1, 3 . . . ELSE . . . END DO innerEND IF outer
illegal!
![Page 106: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/106.jpg)
outer: IF ( a < b ) THEN . . . inner: DO i = 1, 3 . . . END DO inner . . . ELSE . . . END IF outer
legal:
![Page 107: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/107.jpg)
Ch. 4 Basic I/O Concepts
Sec. 4.1 FORMATS and FORMETED WRITE STATEMENTS
READ (*,*) Not always convenient!
WRITE (*,*) Not always pretty!
e.g,. PROGRAM free_formatINTEGER :: i = 21REAL :: result = 3.141593WRITE(*,100) i, result100 FORMAT (‘□The□result□for□iteration□’, & I3, ‘□is□’, F7.3)END PROGRAM
Output: □The□result□for□iteration□□21□is□□□3.142
∵ I3 ∵ F7.3
![Page 108: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/108.jpg)
The following three WRITE statements are equivalent:
• WRITE (*, 100) i, result 100 FORMAT (I6, F10.2)
• CHARACTER ( len=20 ) :: string string = ‘(I6, F10.2)’ WRITE(*,string) i, result
• WRITE(*, ‘(I6,F10.2)’) i, result
Output: □□□□21□□□□□□3.14
I6 F10.2
![Page 109: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/109.jpg)
PROGRAM formatINTEGER :: i = 21REAL :: result = 3.141593CHARACTER ( len=20 ) :: string
WRITE (*, 100) i, result100 FORMAT (I6, F10.2)
string = '(I6, F10.2)'WRITE(*,string) i, result
WRITE(*, '(I6,F10.2)') i, result
END PROGRAM
Output: □□□□21□□□□□□3.14
![Page 110: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/110.jpg)
Sec. 4.2 Output Devices
Line printers, laser printers, and terminals.
Sec. 4.3 Format Descriptors
Table 4-2 (Symbols used with format descriptors)
Symbol meaning
c column number d # of digits to the right of the decimal point m min. # of digit r repeat count w field width
![Page 111: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/111.jpg)
Sec. 4.3.1 Integer Output – The I Descriptor
rIw or rIw.m
e.g., INTEGER :: index = -12, junk = 4, number = -12345 WRITE(*,200) index, index+12, junk, number WRITE(*,210) index, index+12, junk, number WRITE(*,220) index, index+12, junk, number 200 FORMAT ( 2I5, I6, I10) 210 FORMAT (2I5.0, I6, I10.8) 220 FORMAT (2I5.3, I6, I5)
Output: □□-12□□□□0□□□□□4□□□□-12345 □□-12□□□□□□□□□□4□-00012345 □-012□□000□□□□4*****
(Not in scale!)
I5 I5 I6 I10
![Page 112: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/112.jpg)
PROGRAM iformatIMPLICIT NONEINTEGER :: index = -12, junk = 4, number = -12345
WRITE(*,200) index, index+12, junk, numberWRITE(*,210) index, index+12, junk, numberWRITE(*,220) index, index+12, junk, number200 FORMAT ( 2I5, I6, I10)210 FORMAT (2I5.0, I6, I10.8)220FORMAT (2I5.3, I6, I5)
END PROGRAM
Output: -12 0 4 -12345 -12 4 -00012345 -012 000 4*****
(Not in scale!)
![Page 113: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/113.jpg)
Sec. 4.3.2 Real Output – The F Descriptor
rFw.d
e.g., REAL :: a = -12.3, b = .123, c = 123.456 WRITE(*,200) a, b, c WRITE(*,210) a, b, c 200 FORMAT (2F6.3, F8.3) 210 FORMAT (3F10.2)
Output: ******□0.123□123.456 □□□□-12.30□□□□□□0.12□□□□123.46
(Not in scale!)
F6.3 F6.3 F8.3
F10.2 F10.2F10.2
![Page 114: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/114.jpg)
PROGRAM rformatIMPLICIT NONEREAL :: a = -12.3, b = .123, c = 123.456
WRITE(*,200) a, b, cWRITE(*,210) a, b, c200 FORMAT (2F6.3, F8.3)210 FORMAT (3F10.2)
END PROGRAM
Output: ****** 0.123 123.456 -12.30 0.12 123.46
(Not in scale!)
![Page 115: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/115.jpg)
Sec. 4.3.3 Real Output – The E Descriptor
(Expomential notation)Scientific notation: 6.02 × 1023
Exponential notation: 0.602 × 1024(E Descriptor)
0.602 E+24
rFw.d ( w d + ≧ 7 )
e.g., REAL :: a = 1.2346E6, b = 0.001, c = -77.7E10, d = -77.7E10WRITE (*,200) a, b, c, d200 FORMAT (2E14.4, E13.6, E11.6)
□□□□0.1235E+07□□□□0.1000E-02-0.777000E+12***********
E14.4 E11.6E13.6E14.4
Output:
( ‘E+**’ , ‘0.’, and ‘-’)∵
( 11 < 6 + 7)∵
![Page 116: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/116.jpg)
PROGRAM routputIMPLICIT NONEREAL :: a = 1.2346E6, b = 0.001, c = -77.7E10, d = -77.7E10
WRITE (*, 200) a, b, c, d200 FORMAT (2E14.4, E13.6, E11.6)
END PROGRAM
![Page 117: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/117.jpg)
Sec. 4.3.4 True Scientific Notation – The ES Descriptor
rESw.d ( w d + ≧ 7 )
e.g.,
REAL :: a = 1.2346E6, b = 0.001, c = -77.7E10WRITE (*,200) a, b, c200 FORMAT (2ES14.4, ES12.6)
□□□□1.2346E+06□□□□1.0000E-03************
ES14.4 ES12.6 ES14.4
Output:
![Page 118: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/118.jpg)
PROGRAM esformatIMPLICIT NONE
REAL :: a = 1.2346E6, b = 0.001, c = -77.7E10WRITE (*, 200) a, b, c200 FORMAT (2ES14.4, ES12.6)
END PROGRAM
![Page 119: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/119.jpg)
Sec. 4.3.5 Logical Output – The L Descriptor
rLw
e.g.,
LOGICAL :: output = .TRUE., debug = .FALSE.WRITE (*, 200) output, debug200 FORMAT (2L5)
□□□□T□□□□F
L5 L5
Output:
![Page 120: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/120.jpg)
PROGRAM loutputIMPLICIT NONELOGICAL :: output = .TRUE., debug = .FALSE.
WRITE (*, 200) output, debug200 FORMAT (2L5)
END PROGRAM
![Page 121: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/121.jpg)
Sec. 4.3.6 Character Output – The A Descriptor
rAw or rA
e.g.,
CHARACTER (len = 17) :: string = ‘This□is□a□string.’WRITE (*, 10) stringWRITE (*, 11) stringWRITE (*, 12) string10 FORMAT (A)11 FORMAT (A20)12 FORMAT (A6)
Output:
(i.e., the width is the same as the # of characters being displayed.)
17 characters
This□is□a□string.□□□This□is□a□string.This□i
![Page 122: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/122.jpg)
PROGRAM aoutputIMPLICIT NONECHARACTER (len = 17) :: string = 'This is a string.‘
WRITE (*, 10) stringWRITE (*, 11) stringWRITE (*, 12) string10 FORMAT (A)11 FORMAT (A20)12 FORMAT (A6)
END PROGRAM
![Page 123: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/123.jpg)
Sec. 4.3.7 Horizontal Position – The X and T Descriptors
X descriptor: nX
(the # of blanks to insert)
T descriptor:
Tc
(the column # to go to)
![Page 124: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/124.jpg)
e.g,.
CHARACTER (len = 10) :: first_name = ‘James□’CHARACTER :: initial = ‘R’CHARACTER (len = 16) :: last_name = ‘Johnson□’CAHRACTER (len = 9) :: class = ‘COSC□2301’INTEGER :: grade = 92WRITE(*,100) first_name, initial, last_name, grade, class100 FORMAT (A10, 1X, A1, 1X, A10, 4X, I3, T51, A9)
Output:
James□□□□□□R□Johnson□□□□□□□□92 . . . COSC□2301
A10 A10 4X I3 A91X A1 1X
(51th column, T51)∵
![Page 125: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/125.jpg)
orCHARACTER (len = 10) :: first_name = ‘James□’CHARACTER :: initial = ‘R’CHARACTER (len = 16) :: last_name = ‘Johnson□’CHARACTER (len = 9) :: class = ‘COSC□2301’INTEGER :: grade = 92WRITE(*,100) first_name, initial, last_name, class, grade100 FORMAT (A10, T13, A1, T15, A10, T51, A9, T29, I3)
Output:
James□□□□□□□R□Johnson□□□□□□□□92 . . . COSC□2301
A10 A10 I3 A9 A1
(13th column, T13)∵
(15th column, T15)∵ (29th column, T29)∵
(51th column, T51)∵
![Page 126: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/126.jpg)
orCHARACTER (len = 10) :: first_name = ‘James□’CHARACTER :: initial = ‘R’CHARACTER (len = 16) :: last_name = ‘Johnson□’CAHRACTER (len = 9) :: class = ‘COSC□2301’INTEGER :: grade = 92WRITE(*,100) first_name, initial, last_name, class, grade100 FORMAT (A10, T13, A1, T15, A10, T17, A9, T29, I3)
Output:
James□□□□□□□R□JoCOSC□2301□□□□92
A10 A10 I3 A1
(13th column, T13)∵
(15th column, T15)∵
(29th column, T29)∵
(17th column, T17)∵
![Page 127: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/127.jpg)
PROGRAM tformatCHARACTER (len = 10) :: first_name = 'James 'CHARACTER :: initial = 'R'CHARACTER (len = 16) :: last_name = 'Johnson 'CHARACTER (len = 9) :: class ='COSC 2301'INTEGER :: grade = 92
WRITE(*,100) first_name, initial, last_name, grade, classWRITE(*,110) first_name, initial, last_name, class, gradeWRITE(*,120) first_name, initial, last_name, class, grade100 FORMAT (A10, 1X, A1, 1X, A10, 4X, I3, T51, A9) 110 FORMAT (A10, T13, A1, T15, A10, T51, A9, T29, I3) 120 FORMAT (A10, T13, A1, T15, A10, T17, A9, T29, I3)
END PROGRAM
![Page 128: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/128.jpg)
Sec. 4.3.8 Repeating Groups of Format Descriptors
E.g.,
320 FORMAT (1X, I6, I6, F10.2, F10.2, I6, F10.2, F10.2)321 FORMAT (1X, I6, 2(I6, 2F10.2))
320 FORMAT (1X, I6, F10.2, A, F10.2, A, I6, F10.2, A, F10.2, A)321 FORMAT (1X, 2(I6, 2(F10.2, A)))
![Page 129: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/129.jpg)
Sec. 4.3.9 Changing Output Lines – The Slash ( / ) Descriptor e.g.,
WRITE (*, 100) index, time, depth, amplitude, phase100 FORMAT (T20, ‘Results for Test Number ‘, I3, ///, & 1X, ‘Time = ‘, F7.0/, & 1X, ‘Depth = ‘, F7.1, ‘ meters’, / , & 1X, ‘Amplitude = ‘, F8.2/, & 1X, ‘Phase = ‘, F7.1)
Output:
Results for Test Number . . .
Time = . . .Depth = . . . Amplitude = . . .Phase = . . .
(skip 2 lines)
![Page 130: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/130.jpg)
Sec. 4.3.10 How Format Statements Are Used during WRITES
Example 4-1 Generating a Table of Information
Output:
Table of Square Roots, Squares, and Cubes
Number Square Root Square Cube====== ========== ====== ==== 1 1.000000 1 1 2 1.414214 4 8 . . . . . . . . . . . . 9 3.000000 81 729 10 3.162278 100 1000
![Page 131: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/131.jpg)
PROGRAM tableIMPLICIT NONEINTEGER :: i, square, cubeREAL :: square_root
WRITE(*, 100)100 FORMAT(T4, ‘Table of Square Roots, Squares, and Cubes’/ )WRITE(*, 110)110 FORMAT(T4, ‘Number’, T13, ‘Square Root’, T29, ‘Square’, T39, ‘Cube’)WRITE(*, 120)120 FORMAT(T4, ‘======‘, T13, ‘===========‘, T29, & ‘======‘, T39, ‘====‘)DO i = 1, 10 square_root = SQRT(REAL(i)) square = i**2 cube = i**3 WRITE(*, 130) i, square_root, square, cube 130 FORMAT(T4, I4, T13, F10.6, T27, I6, T37, I6)END DOEND PROGRAM
![Page 132: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/132.jpg)
Sec. 4.4 Formatted READ Statements
e.g., READ (*,100) increment100 FORMAT (6X, I6)
(skip the 1st six column)(col. 7~12: an integer)
Sec. 4.4.1 Integer Input – The I Descriptor
rIw
e.g.,READ(*, 100) a, b, c100 FORMAT(3I5)
Input:
□□□15□□15□□15□□
I5 I5 I5a = 15b = 15c = 15
![Page 133: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/133.jpg)
PROGRAM iinputIMPLICIT NONEINTEGER :: a, b, c
READ(*, 100) a, b, c100 FORMAT(3I5)WRITE(*,*) 'a= ', aWRITE(*,*) 'b= ', bWRITE(*,*) 'c= ', c
END PROGRAM
![Page 134: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/134.jpg)
Sec. 4.4.2 Real Input – The F Descriptor
rFw.d
e.g.,
READ (*, ‘(3F10.4)’ ) a, b, c
Input:
1.5□□□□□□□□0.15E+01□□□15.0E-01
F10.4 F10.4 F10.4
a = 1.5b = 1.5c = 1.5
![Page 135: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/135.jpg)
PROGRAM rinputIMPLICIT NONEREAL :: a, b, c
READ (*, '(3F10.4)' ) a, b, cWRITE(*,*) 'a= ', aWRITE(*,*) 'b= ', bWRITE(*,*) 'c= ', c
END PROGRAM
![Page 136: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/136.jpg)
If a number without a decimal point appears in the field, then a decimal point is assumed to be in the position specified by the d term of the format descriptor.
e.g.,READ (*, ‘(3F10.4)’ ) a, b, c
Input:
□□□□□□□□15□□□150□□□□□□15000□□□
F10.4 F10.4 F10.4
a = 0.0015b = 0.0150c = 1.5000
*The E and ES format descriptors are identical to the F descriptor.
![Page 137: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/137.jpg)
Sec. 4.4.3 Logical Input – The L Descriptor
rLw
e.g.,READ (*, ‘(3L5)’ ) a, b, c
Input:
□□□□T□□□□F□□□□T
L5 L5 L5
a = Tb = Fc = T
(or T□□□□F□□□□T□□□□ )
![Page 138: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/138.jpg)
PROGRAM linputIMPLICIT NONELOGICAL :: a, b, c
READ (*, '(3L5)' ) a, b, cWRITE(*,*) 'a= ', aWRITE(*,*) 'b= ', bWRITE(*,*) 'c= ', c
END PROGRAM
![Page 139: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/139.jpg)
Sec. 4.4.4 Character Input – The A Descriptor
rA or rAw
e.g., CHARACTER (len=10) :: string_1, string_2CHARACTER (len=5) :: string_3CHARACTER (len=15) :: string_4, string_5READ (*, ‘(A)’ ) string_1READ (*, ‘(A10)’ ) string_2READ (*, ‘(A10)’ ) string_3READ (*, ‘(A10)’ ) string_4READ (*, ‘(A)’ ) string_5
Input:
ABCDEFGHIJKLMNOABCDEFGHIJKLMNOABCDEFGHIJKLMNOABCDEFGHIJKLMNOABCDEFGHIJKLMNO
String_1 = ‘ABCDEFGHIJ’String_2 = ‘ABCDEFGHIJ’String_3 = ‘FGHIJ’String_4 = ‘ABCDEFGHIJ □□□□□’String_5 = ‘ABCDEFGHIJKLMNO’
![Page 140: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/140.jpg)
PROGRAM ainputIMPLICIT NONECHARACTER (len=10) :: string_1, string_2CHARACTER (len=5) :: string_3CHARACTER (len=15) :: string_4, string_5
READ (*, '(A)' ) string_1READ (*, '(A10)' ) string_2READ (*, '(A10)' ) string_3READ (*, '(A10)' ) string_4READ (*, '(A)' ) string_5WRITE(*,*)string_1WRITE(*,*)string_2WRITE(*,*)string_3WRITE(*,*)string_4WRITE(*,*)string_5
END PROGRAM
![Page 141: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/141.jpg)
Sec. 4.4.5 Horizontal Positioning – The X and T Descriptor
e.g., CHARACTER (len=6) :: stringINTEGER :: iREAD (*, ‘(I6, T1, A6)’ ) i, string
Input: 123456
i = 123456string = ‘123456’
![Page 142: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/142.jpg)
PROGRAM hpositionIMPLICIT NONECHARACTER (len=6) :: stringINTEGER :: i
READ (*, '(I6, T1, A6)' ) i, stringWRITE(*,'(I6)') iWRITE(*,'(A)') string
END PROGRAM
![Page 143: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/143.jpg)
Sec. 4.4.6 Vertical Positioning – The Slash (/) Descriptor
e.g.,REAL :: a, b, c, dREAD (*, 300) a, b, c, d300 FORMAT (2F10.2, //, 2F10.2)
Input:
□□□□□□□1.0□□□□□□□2.0□□□□□□□3.0□□□□□□□4.0□□□□□□□5.0□□□□□□□6.0□□□□□□□7.0□□□□□□□8.0□□□□□□□9.0
a = 1.0b = 2.0c = 7.0d = 8.0
F10.2 F10.2 F10.2
![Page 144: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/144.jpg)
PROGRAM vpositionIMPLICIT NONEREAL :: a, b, c, d
READ (*, 300) a, b, c, d300 FORMAT (2F10.2, //, 2F10.2)WRITE(*,*)'a= ',aWRITE(*,*)'b= ',bWRITE(*,*)'c= ',cWRITE(*,*)'d= ',d
END PROGRAM
![Page 145: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/145.jpg)
Sec. 4.5 An Introduction to Files and File Processing
• i/o unit number:
e.g., READ (8, 100)
Typically, (vary from processor to processor)
READ (5,*) = READ (*,*)WRITE (6,*) = WRITE (*,*)
• I/O statement: (see Table 4-3)
OPEN, CLOSE, READ, WRITE, REWIND, and BACKSPACE.
![Page 146: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/146.jpg)
Sec. 4.5.1 The OPEN Statement
OPEN ( open_list )
The five most important items from the list:
1. (the i/o unit number) UNIT = int_expr
2. (the file name of the file to be opened) FILE = char_expr
3. (the status of the file) STATUS = char_expr (‘OLD’, ‘NEW’, ‘REPLACE’, ‘SCRATCH’, or ‘UNKNOWN’)
![Page 147: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/147.jpg)
4. (whether a file is to be opened for reading only, for writing only, or for both reading and writing) ACTION = char_expr (‘READ’, ‘WRITE’, or ‘READWRITE’)
5. (the status of the open operation) IOSTAT = int_var (If the OPEN statement is successful, a zero will be returned)
![Page 148: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/148.jpg)
Example 1 (Opening a file for input)
INTEGER :: ierrorOPEN ( UNIT = 8, FILE = ‘INPUT.DAT’, & STATUS = ‘OLD’, ACTION = ‘READ’, & IOSTAT = ierror)
INTEGER :: ierrorOPEN ( UNIT = 25, FILE = ‘OUTPUT.DAT, & STATUS = ‘NEW’, ACTION = ‘WRITE’, & IOSTAT = ierror)
Example 2 (Opening a file for output)
Example 3 (Opening a scratch file)
OPEN ( UNIT = 12, STATUS = ‘SCRATCH’, & IOSTAT = ierror)
![Page 149: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/149.jpg)
Sec. 4.5.2 The CLOSE Statement
CLOSE ( close_list )
e.g.,
CLOSE( UNIT = 8 )or
CLOSE( 8 )
Sec. 4.5.3 READS and WRITES to Disk Files
e.g.,OPEN ( UNIT = 8, FILE = ‘INPUT.DAT’, & STATUS = ‘OLD’, IOSTAT = ierror)READ (8,*) x, y, z
![Page 150: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/150.jpg)
OPEN ( UNIT = 9, FILE = OUTPUT.DAT’, & STATUS = ‘REPLACE’, IOSTAT = ierror)READ (9,100) x, y, z100 FORMAT(‘ X= ‘, F10.2, ‘ Y = ‘, F10.2, ‘Z= ‘, F10.2)
Sec. 4.5.4 The IOSTAT = clause in the READ Statement
IOSTAT = int_var
e.g.,READ (8,*, IOSTAT = ierror)
If the READ statement is successful, ierror = 0.
If the READ statement is fail, ierror > 0 (format error) = -1 (end of file) = -2 (end of record)
![Page 151: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/151.jpg)
Example 4-3 Reading Data from a File
1. State the problem. Write a program that can read in an unknown number of real values from a user-specified input data file and detect the end of the data file.
2. Define the input and output. input: (1) the filename of the data file (2) the data in that file output: the values in the data file
3. Describe the algorithm
4. Turn the algorithm into Fortran statements.
![Page 152: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/152.jpg)
.TRUE.
.FALSE.
ierror1 = 0 ?
READ filename
Start1
nvals = nvals +1
Stop
WRITE nvals, value
Initial values:nvals = 0
Fig 4-8 (Flowchart)
WRITE ‘Error
opening file’
READ value
.FALSE.
.TRUE.
OPEN filename
1
2
ierror2 = 0 ?
ierror2 > 0 ?
WRITE ‘Error reading line’
WRITE ‘End of file’
.FALSE.
2
.TRUE.
![Page 153: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/153.jpg)
PROGRAM readIMPLICIT NONECAHRACTER (len = 20) :: filenameINTEGER :: nvals = 0INTEGER :: ierror1, ierror2REAL :: value
WRITE(*,*) ‘Please enter input file name:’READ (*,*) filenameOPEN (UNIT = 3, FILE = filename, STATUS = ‘OLD’, & ACTION = ‘READ’, IOSTAT = ierror1)
openif: IF(ierror1 ==0) THEN readloop: DO READ(3,*), IOSTAT = ierror2) value IF (ierror2 /= 0) EXIT nvals = nvals + 1 WRITE(*, 1010) nvals, value 1010 FORMAT (‘Line ‘, I6, ‘:value=‘, F10.4) END DO readloop
![Page 154: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/154.jpg)
readif: IF (ierror2 > 0) THEN WRITE(*, 1020) nvals + 1 1020 FORMAT (‘Error reading line’, I6) ELSE WRITE(*, 1030) nvals 1030 FORMAT (‘End of file. There are ‘, & I6, ‘ values in the file.’) END IF readifELSE openif WRITE(*, 1040) ierror1 1040 FORMAT (‘Error opening file: IOSTST=‘, I6)END IF openifCLOSE(3)END PROGRAM
![Page 155: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/155.jpg)
Test the program:
(1) The valid input file READ1.TXT: (e.g., use Notepad)
-17.030.0011.012000.-0.012
Output: Please enter input file name:READ1.TXTLine 1: value = -17.0000Line 2: value = 30.0000 Line 3: value = 1.0000 Line 4: value = 12000.0000 Line 5: value = -0.0120 End of file. There are 5 values in the file.
![Page 156: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/156.jpg)
(2) The invalid input file READ2.TXT:
-17.030.001ABCDEF12000.-0.012
Output:
Please enter input file name:READ2.TXTLine 1: value = -17.0000Line 2: value = 30.0000Error reading Line 3
![Page 157: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/157.jpg)
(3) A file does not exist:
Output:Please enter input file name:JUNK.DATError opening file: IOSTAT = 128
(depends on machine)
![Page 158: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/158.jpg)
Sec. 4.5.4 File Positioning
BACKSPACE ( UNIT = a unit # )
REWIND ( UNIT = a unit #)
and
Example 4-4 Using File-Positioning Commands:
(1) Write a program that accepts a series of nonnegative real values and stores them in a scratch file.
(2) Ask the user for a record number to display.
(2) Rewind the file, get that value, and display it.
![Page 159: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/159.jpg)
PROGRAM scratchIMPLICIT NONEINTEGER, PARAMETER :: unit = 8REAL :: dataINTEGER :: icount = 0, irec, j
OPEN (UNIT = unit, STATUS = ‘SCRATCH’)WRITE(*, 100)100 FORMAT (1X, ‘Enter positive or zero input values.’, / , & 1X, ‘A negative value terminates input.’)DO WRITE(*, 110) icount + 1 110 FORMAT (1X, ‘Enter sample ‘, I4, ‘:’) READ (*,*) data IF ( data < 0. ) EXIT icount = icount + 1 WRITE(unit, 120) data 120 FORMAT (1X, ES16.6)END DO
![Page 160: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/160.jpg)
WRITE(*, 130) icount130 FORMAT (1X, ‘Which record do you want to see ( 1 to’, I4, ‘)? ’)READ (*,*) irecIF ( ( irec >= 0) .AND. (irec <= icount) )THEN REWIND (UNIT = unit) DO j = 1, irec READ( unit, *) data END DO WRITE(*, 140) irec, data 140 FORMAT (1X, ‘ The value of record ‘, I4, ‘is’, ES14.5)ELSE WRITE(*, 150) irec 150 FORMAT (1X, ‘ Illegal record number entered: ‘, I8)END IFEND PROGRAM
![Page 161: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/161.jpg)
Output:Enter positive or zero input values.A negative input value terminates input.Enter sample 1:234.Enter sample 2:12.34Enter sample 3:0.Enter sample 4:16.Enter sample 5:11.235Enter sample 6:2.Enter sample 7:-1.Which recore do you want to see ( 1 to 6)?5The value of record 5 is 1.12350E+01
![Page 162: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/162.jpg)
Example 4-5 The linear fit problem:
Given a set of measurements (xi , yi), i = 1, . . . , N:
(x2, y2)
(x4, y4)
(x3 , y3)
(x1 , y1)
The best fit: y = mx + b
x
y
m = ?b = ?
![Page 163: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/163.jpg)
The method of least squares:
m =
(Σxi yi ) - (Σxi ) y i=1
N
where
y = Σyii=1 i=1
N N
Σxi
N
b = y – m x
i=1
N
(Σxi2 ) - (Σxi ) x
N
i=1i=1
N
and x = N
![Page 164: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/164.jpg)
PROGRAM least_squares_fitIMPLICIT NONEINTEGER, PARAMETER :: unit = 18CHARACTER (len = 24) :: filenameINTEGER :: ierror, n = 0REAL :: m, bREAL :: sum_x = 0., sum_x2 = 0., sum_xy = 0., sum_y = 0.REAL :: x, y, x_bar, y_bar
WRITE(*, 1000)1000 FORMAT (1X, ‘Enter the file name: ‘)READ (*, ‘(A)’) filenameOPEN (UNIT = unit, FILE = filename, STATUS = ‘OLD’, & ACTION = ‘READ’, IOSTAT = ierror)
errorcheck: IF (ierror > 0) THEN WRITE(*, 1020) filename 1020 FORMAT (1X, ‘ERROR: File ‘, A, ‘ does not exist! ’)ELSE
![Page 165: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/165.jpg)
DO READ (unit, *, IOSTAT = ierror) x, y IF ( ierror /= 0 ) EXIT n = n + 1 sum_x = sum_x + x sum_y = sum_y + y sum_x2 = sum_x2 + x ** 2 sum_xy = sum_xy + x * y END DO x_bar = sum_x / n y_bar = sum_y / n m = (sum_xy – sum_x * y_bar) / (sum_x2-sum_x * x_bar) b = y_bar – m * x_bar WRITE(*, 1030) m, b, n 1030 FORMAT (1X, ‘ m = ‘, F12.3, / , & 1X, ‘ b = ‘, F12.3, / , & 1X, ‘ N = ‘, I12) CLOSE(18)END IF errorcheck END PROGRAM
![Page 166: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/166.jpg)
Test the program:
(1) The input file INPUT.TXT:
1.1 1.12.2 2.23.3 3.34.4 4.45.5 5.56.6 6.67.7 7.7
Output:
m = 1.000 b = 0.000 N = 7
![Page 167: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/167.jpg)
(2) The input file INPUT1.TXT:
1.1 1.012.2 2.303.3 3.054.4 4.245.5 5.756.6 6.487.7 7.84
Output:
m = 1.024 b = -0.12 N = 7
![Page 168: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/168.jpg)
Ch. 5 Arrays
Fig. 5-1
a(1)
a(2)
a(3)
a(4)
a(5)
. . .
. . .
array a
e.g.,DO i = 1, 100 a(i) = SQRT (a(i))END DO
![Page 169: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/169.jpg)
Sec. 5.1 Declaring Arrays
• Type: real, integer, logical, or character
e.g.,
REAL, DIMENSION (16) :: voltage
voltage(1), voltage(2), . . . , voltage(16)
or
CHARACTER (len = 20), DIMENSION (50) :: last_name
last_name(1), . . . , last_name(50)
![Page 170: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/170.jpg)
• Rank: The # of subscripts declared for a given array
e.g., REAL, DIMENSION (3, 6) :: sum
a rank-2 array
• Extent: The # of elements in a given dimension of an array
e.g., The extent of the 1st subscript of sum is 3.
The extent of the 2nd subscript of sum is 6.
• Size: the # of elements
• Shape: the combination of rank and extent in each dimension
e.g., The shape of sum = 3 6
e.g., The size of sum = 18
![Page 171: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/171.jpg)
Array constructor:
e.g., INTEGER, DIMENDION (5) :: a = (/ 1, 2, 3, 4, 5 /)
Sec. 5.2 Using Array Elements in Fortran Statements
Sec. 5.2.1 Array Elements Are Just Ordinary Variables
e.g.,
INTEGER, DIMENDION (10) :: indexLOGICAL, DIMENSION (2) :: lvalREAL, DIMENSION (3) :: temp
Index(1) = 1Lval(2) = .TRUE.Temp(3) = REAL(index(1)) / 4.WRITE(*,*) ‘ index(1) = ‘, index(1)
![Page 172: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/172.jpg)
Sec. 5.2.2 Initialization of Array Elements
e.g.,INTEGER, DIMENDION (10) :: jWRITE(*,*) ‘ j(1)=‘, j(1)
uninitialized array
?• Initialization arrays with assignment statements:
e.g.,REAL, DIMENDION (10) :: array1DO i = 1, 10 array1(i) = 0.0END DO
orREAL, DIMENDION (10) :: array1array1 = (/ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0. /)
![Page 173: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/173.jpg)
• Initialization arrays in type declaration statements:
e.g.,
INTEGER, DIMENDION (5) :: array2 = (/ 1, 2, 3, 4, 5 /)
or
REAL, DIMENDION (100) :: array5 = 1.
or
INTEGER, DIMENDION (5) :: array2 = (/ ( i, i = 1, 5) /)
INTEGER, DIMENDION (25) :: array4 = (/ ((0, i = 1, 4), & 5*j, j = 1, 5) /)
0, 0, 0, 0, 5, 0, 0, 0, 0, 10, 0, 0, 0, 0, 15, . . .
or
array5(1) = 1., . . . , array5(100) = 1.
![Page 174: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/174.jpg)
•Initialization arrays with Fortran READ statements:
Just like any other variables. (See Sec. 5.4: I/O of Array Elements)
Sec. 5.2.3 Changing the Subscript Range of an Array
e.g.,
REAL, DIMENSION(5) :: arr
arr(1), arr(2), arr(3), arr(4), arr(5)
but arr(0) = ?
![Page 175: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/175.jpg)
e.g., c = Σ an = a0 + a1 + a2 + a3 + a4n=0
4
need a(0)!
Use
REAL, DIMENSION ( lower_bound : upper_bound ) :: arrar
e.g.,REAL, DIMENSION (-2:2) :: b
b(-2), b(-1), b(0), b(1), b(2)
orREAL, DIMENSION (5:9) :: c
c(5), c(6), c(7), c(8), c(9)
(5 elements)
(5 elements)
![Page 176: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/176.jpg)
Example (Fig. 5-4)
i i2
-5 25-4 16… …+4 16+5 25
PROGRAM squares_2IMPLICIT NONEINTEGER :: IINTEGER, DIMENSION(-5:5) :: number, square
DO i = -5, 5 number(i) = I square(i) = number(i)**2 WRITE(*, 100) number(i), square(i) 100 FORMAT (1X, ‘Number = ‘, I6, ‘ Square= ‘, I6)END DOEND PROGRAM
![Page 177: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/177.jpg)
Sec. 5.2.4 Out-of-bound Array Subscripts
e.g.,REAL, DIMENSION(5) :: a
a(1), a(2), a(3), a(4), a(5)
but if use a(6), out of bound!
Sec. 5.2.5 The Use of Named Constants with Array Declarations
e.g.,INTEGER, PARAMETER :: isize = 1000REAL, DIMENSION (isize) :: array1REAL, DIMENSION (isize) :: array2REAL, DIMENSION (2*isize) :: array3
![Page 178: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/178.jpg)
PROGRAM extremesIMPLICIT NONEINTEGER, PARAMETER :: max_size = 10INTEGER, DIMENSION (max_size) :: inputINTEGER :: ilarge, ismall, j, nvals, tempWRITE(*,*) ' Enter number of values in data set:'READ(*,*) nvalssize: IF (nvals <= max_size) THEN in: DO j = 1, nvals WRITE(*, 100) ' Enter value ', j 100 FORMAT (' ', A, I3, ':' ) READ(*,*) input(j) END DO in temp = input(1) ilarge = 1 large: DO j = 2, nvals IF (input(j) > temp) THEN temp = input(j) ilarge = j END IF END DO large
Example 5-1 (Finding the largest and smallest values in a data set)
![Page 179: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/179.jpg)
temp = input(1) ismall = 1 small: DO j = 2, nvals IF (input(j) < temp) THEN temp = input(j) ismall = j END IF END DO small WRITE(*, 110) 110 FORMAT(1X, 'The values are:') out: DO j = 1, nvals IF (j == ilarge) THEN WRITE(*, '(1X, I6, 2X, A)') input(j), 'LARGEST' ELSE IF (j == ismall) THEN WRITE(*, '(1X, I6, 2X, A)') input(j), 'SMALLEST' ELSE WRITE(*, '(1X, I6)') input(j) END IF END DO outELSE size WRITE(*, 120) nvals, max_size 120 FORMAT(1X, ' Too many input values: ', I6, '>', I6)END IF sizeEND PROGRAM
![Page 180: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/180.jpg)
Test:
Enter number of values in data set:6Enter value 1:-6Enter value 2:5Enter value 3:-11Enter value 4:16Enter value 5:9Enter value 6:0
The values are: -6 5 -11 SMALLEST 16 LARGEST 9 0
Output
![Page 181: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/181.jpg)
Sec. 5.3 Using Whole Arrays and Array Subsets in Fortran Statements
Sec. 5.3.1 Whole Array Operations
e.g.,
1.
2.
3.
4.
a(1)
a(4)
a(3)
a(2)
a
+
b(1)
b(2)
b(3)
b(4)
5.
6.
7.
8.
b
=
c
12.
10.
8.
6.
![Page 182: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/182.jpg)
Fig. 5-6
PROGRAM add_arraysIMPLICIT NONEINTEGER :: IREAL, DIMENSION(4) :: a = (/ 1., 2., 3., 4. /)REAL, DIMENSION(4) :: b = (/ 5., 6., 7., 8. /)REAL, DIMENSION(4) :: c, d
DO i = 1,4 c(i) = a(i) + b(i)END DOd = a + bWRITE(*, 100) ‘c’, cWRITE(*, 100) ‘d’, d100 FORMAT (1X, A, ‘ =‘, 5(F6.1, 1X))END PROGRAM
![Page 183: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/183.jpg)
If arrays a and b have the same shape,
Conformable!
e.g.,REAL, DIMENSION(1:4) :: a = (/ 1., 2., 3., 4. /)REAL, DIMENSION(5:8) :: b = (/ 5., 6., 7., 8. /)REAL, DIMENSION(101:104) :: cc = a + b
REAL, DIMENSION(4) :: a = (/ 1., 2., 3., 4. /)REAL :: b = 10REAL, DIMENSION(4) :: cc = a * b
or
c = (/ 10., 20., 30., 40. /)
(Not matrix multiplication)
![Page 184: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/184.jpg)
Elemental intrinsic functions: (see App. B for a complete list)
ABS, SIN, COS, EXP, and LOG.
e.g.,REAL, DIMENSION(4) :: x = (/ 0., 3.14, 1., 2. /), yINTEGER :: iDO i = 1, 4 y(i) = SIN(x(i))END DO
or use y = SIN(x)
REAL, DIMENSION(4) :: a = (/ -1., 2., -3., 4. /), yy = ABS(a)
or
y = (/ 1., 2., 3., 4. /)
![Page 185: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/185.jpg)
Sec. 5.3.2 Selecting Subsets of Arrays for Use in Calculations
Array section: A subset of an array.
• Subscript triplet:
subscript_1 : subscript_2 : stride
e.g.,
INTEGER, DIMENSION(10) :: a = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /)
array(1:10:2) = [1, 3, 5, 7, 9]
Alternative forms: subscript_1 : subscript_2 subscript_1 : : subscript_2
stride = 1to the last subscriptto the 1st subscript
![Page 186: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/186.jpg)
Example 5-5
INTEGER :: i = 3, j = 7REAL, DIMENSION(10) :: a = (/ 1., -2., 3., -4., 5, -6., 7., -8., & 9., -10. /)(a) a(:) = [1., -2., 3., -4., 5, -6., 7., -8., 9., -10.](b) a(i:j) = a(3:7:1) = [3., -4., 5., -6., 7.](c) a(i:j:i) = a(3:7:3) = [3., -6.](d) a(i:j:j) = a(3:7:7) = [3.](e) a(i:) = a(3:10:1) = [3., -4., 5., -6., 7., -8., 9., -10.](f) a(:j) = a(1:7:1) = [1., -2., 3., -4., 5., -6., 7.](g) a(::i) = a(1:10:3) = [1., -4., 7., -10.]
![Page 187: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/187.jpg)
• Vector subscript:
INTEGER, DIMENSION(5) :: vec = (/ 1, 6, 4, 1, 9 /)REAL, DIMENSION(10) :: a = (/ 1., -2., -3., -4., 5, -6., 7., -8., & 9., -10. /)
a(vec) = [1., -6., -4., 1., 9.]
a(1) a(1) a(9)a(4)a(6)
e.g.,
*Vector subscript cannot be used on the left side of an assignment statement.
e.g., INTEGER, DIMENSION(3) :: vec = (/ 1, 2, 1 /)REAL, DIMENSION(3) :: a = (/ 10., 20., 30. /)REAL, DIMENSION(2) :: bb(vec) = a (Incorrect!)
![Page 188: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/188.jpg)
Sec. 5.4 Input and Output
Sec. 5.4.1 Input and Output of Array Elements
e.g., WRITE(*, 100) a(1), a(2), a(3), a(4), a(5)100 FORMAT (1X, ‘a=‘, 5F10.2)
Just like any other variables.
Sec. 5.4.2 The Implied DO LOOP
e.g.,
WRITE(*, 100) (a(i), I = 1, 5)100 FORMAT (1X, ‘a=‘, 5F10.2)
![Page 189: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/189.jpg)
Sec. 5.4.3 Input and Output of Whple Arrays and Array Sections
Fig. 5-9 (array I/O)
PROGRAM array_ioIMPLICIT NONEREAL, DIMENSION(5) :: a = (/ 1., 2., 3., 20., 10. /)INTEGER, DIMENSION(4) :: vec = (/ 4, 3, 4, 5 /)WRITE(*, 100) a100 FORMAT (2X, 5F8.3)WRITE(*, 100) a(2: :2)WRITE(*, 100) a(vec)END PROGRAM
(Output) 1.000 2.000 3.000 20.000 10.000 2.000 20.00020.000 3.000 20.000 10.000
![Page 190: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/190.jpg)
Sec. 5.5 Examples
Example 5-3 Sorting Data
Ascending order (the lowest to the highest)
Descending order (the highest to the lowest)
or
e.g.,
(10, 3, 6, 4, 9)(sorting)
(3, 4, 6, 9, 10)
![Page 191: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/191.jpg)
Fig. 5-10 (selection sort)
10
9
4
6
3
(swap)3
10
6
4
9
(swap)3
4
6
10
9
(no swap)
3
4
6
10
9
(swap)
3
4
6
10
9
![Page 192: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/192.jpg)
Steps:
1. Get the input filename2. Open the input file3. Read the input data into an array4. Sort the data in ascending order5. Write the sorted data
![Page 193: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/193.jpg)
Fig. 5-13PROGRAM sort1IMPLICIT NONEINTEGER, PARAMETER :: max_size = 10REAL, DIMENSION (max_size) :: aCHARACTER (len = 20) :: filenameINTEGER :: i, iptr, j, statusINTEGER :: nvals = 0REAL :: tempWRITE(*, 1000)1000 FORMAT (1X, ‘ Enter the file name’)READ(*, ‘(A20)’) filenameOPEN (UNIT = 9, FILE = filename, STATUS = ‘OLD’, & ACTION = ‘READ’, IOSTAT = status)fileopen: IF(status == 0) THEN DO READ (9, *, IOSTAT = status) temp IF (status /= 0) EXIT nvals = nvals + 1 a(nvals) = temp END DO
![Page 194: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/194.jpg)
outer: DO i = 1, nvals – 1 iptr = i inner: DO j = i + 1, nvals minval: IF (a(j) < a(iptr)) THEN iptr = j END IF minval END DO inner ! swap a(iptr) with a(i) if i /= iptr swap: IF ( i /= iptr ) THEN temp = a(i) a(i) = a(iptr) a(iptr) = temp END IF swap END DO outer WRITE(*, ‘(A)’) ‘ The sorted data are:’ WRITE(*, 1040) ( a(i), i = 1, nvals) 1040 FORMAT (4X, F10.4)ELSE fileopenWRITE(*, 1050)status1050 FORMAT (1X, ‘File open failed: ’, I6)END IF fileopenEND PROGRAM
![Page 195: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/195.jpg)
Test:
INPUT2.TXT
13.312.-3.00.4.06.64.-6.
output
Enter the file nameINPUT2.TXTThe sorted data are: -6.0000 -3.0000 0.0000 4.0000 4.0000 6.600012.000013.3000
![Page 196: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/196.jpg)
Sec. 5.6 Two-Dimensional Features of Arrays
• 1-dim. array: rank-1 array or vector• 2-dim. array: rank-2 array or matrix
Fig. 5-17
a(1)
a(4)
a(2)
a(3)
row 1
row 3
row 2
row 4
a(irow)
(a) 1-dim array
b(1,1) b(1,2)
b(2,1) b(2,2)
b(1,3)
b(2,3)
row 1
row 2
col 1 col 2 col 3
b(irow, icol))
(b) 2-dim array
![Page 197: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/197.jpg)
Sec. 5.6.1 Declaring Rank-2 Arrays
e.g.,REAL, DIMENSION(3,6) :: sumINTEGER, DIMENSION(0:100, 0:20) :: hist
Sec. 5.6.2 Rank-2 Array Storage
Fig. 5-19
a(1,1) a(1,2) a(1,3)
a(2,2)a(2,1) a(2,3)
a(irow, icol)
(Memory allocation)
a(1,1)
a(2,1)
a(1,2)
a(2,2)
a(1,3)
a(2,3)
column major order
![Page 198: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/198.jpg)
Sec. 5.6.3 Initializing Rank-2 Array
1. Use assignment statements
e.g., 1
1
1
1
2
2
2
2
3
3
3
3
INTEGER, DIMENSION(4,3) :: istatDO i = 1, 4 DO j = 1, 3 istat(i, j) = j END DOEND DO
use
![Page 199: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/199.jpg)
orDO j = 1, 3 istat(:, j) = jEND DO
cannot use
istat = (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /)
( Array constructors always produce rank-1 array!)∵
use
istat = RESHAPE ( (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /), (/4, 3/) )
(column major) (data to be reshaped)
(a new shape)
![Page 200: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/200.jpg)
2. Use type declaration statements
INTEGER, DIMENSION(4, 3) :: istat (4,3) = & RESHAPE ( (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /), (/4, 3/) )
3. Use READ statements
INTEGER, DIMENSION(4,3) :: istatOPEN(7, FILE = ‘INITIAL.DAT’, STATUS = ‘OLD’, & ACTION = ‘READ’)READ (7, *) istat
(INITIAL.DAT: 1 1 1 1 2 2 2 2 3 3 3 3 )
or
READ (7, *) ((istat(i,j), j = 1, 3), i = 1, 4)
(INITIAL.DAT: 1 2 3 1 2 3 1 2 3 1 2 3 )
(i = 1) (i = 4)(i = 3)(i = 2)
![Page 201: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/201.jpg)
Sec. 5.6.4 Examples
Examples 5-5
power =
20.0 40.3 42.0 20.419.8 40.1 41.5 26.920.1 40.0 41.3 38.420.0 39.5 41.1 42.020.0 39.9 39.8 12.219.9 40.0 41.0 6.0
use
REAL,DIMENSION(6,4) :: power…OPEN(9, FILE = ‘INPUT1’, STATUS = ‘OLD’, & ACTION = ‘READ’)READ (9, *) power
![Page 202: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/202.jpg)
INPUT1: 20.0 19.8 20.1 20.0 20.0 19.9 40.3 40.1 40.0 39.5 39.9 40.0 42.0 41.5 41.3 41.1 39.8 41.0 20.4 26.9 38.4 42.0 12.2 6.0
OPEN(9, FILE = ‘INPUT2’, STATUS = ‘OLD’, & ACTION = ‘READ’)READ (9, *) ((power(i, j), j = 1, 4), i = 1, 6)
INPUT2:
or
20.0 40.3 42.0 20.419.8 40.1 41.5 26.920.1 40.0 41.3 38.420.0 39.5 41.1 42.020.0 39.9 39.8 12.219.9 40.0 41.0 6.0
![Page 203: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/203.jpg)
Sec. 5.6.5 Whole Array Operation and Array Subsets
e.g.,
a =
1 2 3 4 5 6 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25
a(:, 1) =
1 6111621
a(1, :) = [ 1 2 3 4 5 ]
a(1:3, 1:5:2) =
1 3 5 6 8 1011 13 15
![Page 204: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/204.jpg)
Sec. 5.7 Multidimensional or Rank-n Array
(up to 7)Fig. 5-22 ( A 2 × 2 × 2 array a)
a(1, 1, 1)
a(2, 1, 1)
a(1, 2, 1)
a(2, 2, 1)
a(1, 1, 2)
a(2, 1, 2)
a(1, 2, 2)
a(2, 2, 2)
(memory allocation)
…
…
![Page 205: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/205.jpg)
Sec. 5.8 Using Fortran Intrinsic Functions with Arrays
Sec. 5.8.1 Elemental Intrinsic Functions
e.g.,
ABS, SIN, COS, TAN, EXP, LOG, LOG10, MOD, AND SQRT.
REAL, DIMENSION :: x = (/ 10., 3.14, 1., 2. /), yINTEGER :: iDO i = 1, 4 y(i) = sin(x(i))END DO
equiv.y = sin(x)
![Page 206: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/206.jpg)
Sec. 5.8.2 Inquiry Intrinsic Functions
Table 5-1
LBOUND (ARRAY, DIM)SHAPE (SOURCE)SIZE(ARRAY, DIM)UBOUND (ARRAY, DIM)
(a particular dimension, e.g., 1 or 2)
Example 5-6 (Determining the Properties of an Array)
PROGRAM check_arrayREAL, DIMENSION(-5:5, 0:3) :: a = 0.WRITE(*, ‘(A, 7I6)’) ‘ The shape is: ‘, SHAPE(a)WRITE(*, ‘(A, I6)’) ‘ The size is: ‘, SIZE(a)WRITE(*, ‘(A, 7I6)’) ‘ The lower bounds are: ‘, LBOUND(a)WRITE(*, ‘(A, 7I6)’) ‘ The upper bounds are: ‘, UBOUND(a)END PROGRAM
![Page 207: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/207.jpg)
Output:
11 5 (size(a, 1) = 11, size(a, 2) = 4)44-5 0 (LBOUND(a, 1) = -5, LBOUND(a, 2) = 0) 5 3 (UBOUND(a, 1) = 5, UBOUND(a, 2) = 3)
Sec. 5.8.3 Transformational Intrinsic Functions
Table 5-2
DOT_PRODUCT (VECTOR_A, VECTOR_B)MATMUL (MATRIX_A, MATRIX_B)RESHAPE (SOURCE, SHAPE)
![Page 208: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/208.jpg)
Sec. 5.9 Masked Array Assignment: The WHERE Constructe.g.,
DO i = 1, ndim1 DO j = 1, ndim2 logical(i, j) = LOG (value(i, j)) END DOEND DO
equiv.logval = LOG(value)
But if value(i, j) 0≦ LOG(value(i, j) is not defined!(run-time errors!)
![Page 209: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/209.jpg)
DO i = 1, ndim1 DO j = 1, ndim2 IF (value(i, j) > 0.) THEN logical(i, j) = LOG (value(i, j)) ELSE logical(i, j) = -99999. END IF END DOEND DO
equiv.
WHERE (value > 0.) logval = LOG(value)ELSEWHERE logical = -99999.END WHERE
![Page 210: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/210.jpg)
The general form:
[name:] WHERE (mask_expr) Array Assignment Statements ! Block1ELSEWHERE [name:] Array Assignment Statements ! Block2END WHERE [name:]
Example 5-7 Limiting the Maximum and Minimum Values in an Array (-1000 input(i) 1000)≦ ≦
DO i = 1, 10000 IF ( input(i) > 1000. ) THEN input(i) = 1000. ELSE IF (input(i) < -1000.) THEN input(i) = -1000. END IF END DO
![Page 211: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/211.jpg)
equiv. WHERE ( ABS(input) > 1000.) input = SIGN(1000., input)END WHERE
( SIGN(A, B): returns the value of A with the sign of B.)
![Page 212: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/212.jpg)
Ch. 6 Procedures and Structured Programming
Sec. 6.1 Subroutines
The general form of a subroutine
SUBROUTINE subroutine_name (argument_list) . . . (Declaration section) . . . (Execution section) . . .RETURNEND SUBROUTINE [name]
![Page 213: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/213.jpg)
The calling program uses a CALL statement to call a subroutine:
CALL subroutine_name (argument_list)
Fig. 6-1 (the hypotenuse of a right triangle)
SUBROUTINE calc_hypotenuse (side_1, side_2, hypotenuse)IMPLICIT NONEREAL, INTENT(IN) :: side_1, side_2REAL, INTENT(OUT) :: hypotenuseREAL :: temp
temp = side_1**2 + side_2**2hypotenuse = SQRT(temp)RETURNEND SUBROUTINE
![Page 214: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/214.jpg)
Test : Write a test driven program.
Fig. 6-2
PROGRAM test_hypotenuseIMPLICIT NONEREAL :: s1, s2, hypot
WRITE(*,*) ‘Enter the length of side 1:’READ(*,*) s1WRITE(*,*) ‘Enter the length of side 2:’READ(*,*) s2
CALL calc_hypotenuse(s1, s2, hypot)WRITE(*, 1000) hypot1000 FORMAT (1X, ‘ The length of the hypotenuse is :’, F10.4)END PROGRAM
![Page 215: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/215.jpg)
Fig. 6-3 PROGRAM sort1IMPLICIT NONEINTEGER, PARAMETER :: max_size = 10REAL, DIMENSION (max_size) :: aCHARACTER (len = 20) :: filenameINTEGER :: i, iptr, j, statusINTEGER :: nvals = 0REAL :: tempWRITE(*, 1000)1000 FORMAT (1X, ‘ Enter the file name’)READ(*, ‘(A20)’) filenameOPEN (UNIT = 9, FILE = filename, STATUS = ‘OLD’, & ACTION = ‘READ’, IOSTAT = status)fileopen: IF(status == 0) THEN DO READ (9, *, IOSTAT = status) temp IF (status /= 0) EXIT nvals = nvals + 1 a(nvals) = temp END DO
Sec. 6.1.1 Sample Problem - Sorting
![Page 216: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/216.jpg)
CALL sort(a, nvals)WRITE(*, ‘(A)’) ‘ The sorted data are:’ WRITE(*, 1040) ( a(i), i = 1, nvals) 1040 FORMAT (4X, F10.4)ELSE fileopenWRITE(*, 1050) status1050 FORMAT (1X, ‘File open failed: ’, I6)END IF fileopenEND PROGRAM
![Page 217: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/217.jpg)
SUBROUTINE sort(arr, n)IMPLICIT NONEINTEGER, INTENT(IN) :: nREAL, DIMENSION(n), INTENT(INOUT) :: arrINTEGER :: i. iptr, jREAL :: tempouter: DO i = 1, n – 1 iptr = i inner: DO j = i + 1, n minval: IF (arr(j) < arr(iptr)) THEN iptr = j END IF minval END DO inner swap: IF ( i /= iptr ) THEN temp = arr(i) arr(i) = arr(iptr) arr(iptr) = temp END IF swap END DO outer END SUBROUTINE sort
![Page 218: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/218.jpg)
Test:
INPUT2.TXT
13.312.-3.00.4.06.64.-6.
output
Enter the file nameINPUT2.TXTThe sorted data are: -6.0000 -3.0000 0.0000 4.0000 4.0000 6.600012.000013.3000
![Page 219: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/219.jpg)
Sec. 6.1.2 The Intent Attribute
INTENT(IN), INTENT(OUT), INTENT(INOUT)
e.g.,
SUBROUTINE sub1(input, output)IMPLICIT NONEREAL, INTENT(IN) :: inputREAL, INTENT(OUT) :: output
output = 2. * inputinput = -1. ! This line is an errorEND SUBROUTINE
![Page 220: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/220.jpg)
Sec. 6.1.3 Passing Arrays to Subroutines
e.g.,
SUBROUTINE process (data1, data2, n, nvals)IMPLICIT NONEREAL, INTENT(IN), DIMENSION(n) :: data1REAL, INTENT(OUT), DIMENSION(n) :: data2
data2 = 3. * data1END SUBROUTINE process
![Page 221: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/221.jpg)
Sec. 6.1.4 Passing Character Variables to Subroutines
e.g.,
PROGRAM test_sample_stringIMPLICIT NONECHARACTER (len=15) :: aCALL sample(a)END PROGRAM
SUBROUTINE sample (string)IMPLICIT NONECHARACTER (len=*), INTENT(IN) :: stringWRITE(*,’(1X, A, I3)’) ‘Length of variable = ‘, LEN(string)END SUBROUTINE
![Page 222: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/222.jpg)
Sec. 6.1.5 Error Handling in Subroutines
Eg. 1, (Bad! If temp < 0, SQRT(temp) = ???)
SUBROUTINE process (a, b, result)IMPLICIT NONEREAL, INTENT(IN) :: a, bREAL, INTENT(OUT) :: resultREAL :: temp
temp = a - bresult = SQRT(temp)END SUBROUTINE
![Page 223: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/223.jpg)
Eg. 2, (Better! But still not good, STOP)∵
SUBROUTINE process (a, b, result)IMPLICIT NONEREAL, INTENT(IN) :: a, bREAL, INTENT(OUT) :: resultREAL :: temp
temp = a – bIF ( temp >= 0.) THEN result = SQRT(temp)ELSE WRITE(*,*)’ Square root of negative value in sub. Process!’ STOPEND IFEND SUBROUTINE
![Page 224: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/224.jpg)
Eg. 2, (Much better! error flag)∵
SUBROUTINE process (a, b, result, error)IMPLICIT NONEREAL, INTENT(IN) :: a, bREAL, INTENT(OUT) :: resultINTEGER, INTENT(OUT) :: errorREAL :: temp
temp = a – bIF ( temp >= 0.) THEN result = SQRT(temp) error = 0ELSE result = 0 error = 1END IFEND SUBROUTINE
![Page 225: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/225.jpg)
Example 6-4 (Gauss-Jordan Elimination)
Linear eq.:
1 x1 + 1 x2 + 1 x3 = 1
2 x1 + 1 x2 + 1 x3 = 2
1 x1 + 3 x2 + 2 x3 = 4
(1)
(3)
(2)
or
1 1 1
2 1 1
1 3 2
x1
x2
x3
=
1
2
4
1 1 1
2 1 1
1 3 2
1
2
4
or
![Page 226: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/226.jpg)
a1 x1 + 0 x2 + 0 x3 = b1
0 x1 + a2 x2 + 0 x3 = b2
0 x1 + 0 x2 + a3 x3 = b3
or
a1 0 0
0 a2 0
0 0 a3
x1
x2
x3
=
b1
b2
b3
.
.
.???
x1 = b1/a1,
x2 = b2/a2,
x3 = b3/a3.
![Page 227: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/227.jpg)
Gauss-Jordan Elimination:
1 x1 + 1 x2 + 1 x3 = 1
2 x1 + 1 x2 + 1 x3 = 2
1 x1 + 3 x2 + 2 x3 = 4
(1)
(3)
(2)
1 1 1
0 -1 -1
0 2 1
1
0
3
(1) × -2 + (2) 0 x1 – x2 – x3 = 0 (4)
(1) × -1 + (3) 0 x1 + 2x2 + x3 = 3 (5)
(new row 2)
(new row 3)
∴ (1) (4) (5)
![Page 228: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/228.jpg)
1 x1 + 1 x2 + 1 x3 = 1
0 x1 - 1 x2 - 1 x3 = 0
0 x1 + 2 x2 + 1 x3 = 3
(1)
(5)
(4)
1 0 0
0 -1 -1
0 0 1
1
0
3
(4) + (1) 1 x1 + 0 x2 + 0 x3 = 0 (6)
(4) × 2 + (5) 0 x1 + 0 x2 - 1 x3 = 3 (7)
(new row 2)
(new row 3)
∴ (6) (4) (7)
![Page 229: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/229.jpg)
1 x1 + 0 x2 + 0 x3 = 1
0 x1 - 1 x2 - 1 x3 = 0
0 x1 + 0 x2 - 1 x3 = 3
(6)
(7)
(4)
1 0 0
0 -1 0
0 0 -1
1
-3
3
(7) × -1 + (4) 0 x1 - 1 x2 + 0 x3 = 0 (8) (new row 2)
∴ (6) (7) (8)
x1 = -1,
-x2 = -3,
-x3 = 3.
x1 = -1, x2 = 3, x3 = -3.
![Page 230: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/230.jpg)
Gauss-Jordan Elimination:
3 x1 – 6 x2 + 7 x3 = 3
9 x1 + 0 x2 – 5 x3 = 3
5 x1 – 8 x2 + 6 x3 = -4
(1)
(3)
(2)
9 0 -5
5 -8 6
3 -6 7
3
-4
3
9 x1 + 0 x2 – 5 x3 = 3
5 x1 – 8 x2 + 6 x3 = -4
3 x1 – 6 x2 + 7 x3 = 3
![Page 231: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/231.jpg)
(1)
(3)
(2)
3
-51/9
2
(1) × (-5/9) + (2)
0 x1 – 8 x2 + (79/9) x3 = -51/9
(1) × (-3/9) + (3)
0 x1 – 6 x2 + (78/9) x3 = 2
(new row 2)
(new row 3)
9 x1 + 0 x2 – 5 x3 = 3
5 x1 – 8 x2 + 6 x3 = -4
3 x1 – 6 x2 + 7 x3 = 3
9 0 -5
0 -8 79/9
0 -6 78/9
![Page 232: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/232.jpg)
3
-51/9
2row 2 × (-(-6) /(-8)) + row 3,
9 0 -5
0 -8 79/9
0 -6 78/9
9 0 -5
0 -8 79/9
0 0 75/36
3
-51/9
225/36
row 3 × (-(-5) /(75/36))+ row 1, 9 0 0
0 -8 79/9
0 0 75/36
18
-51/9
225/36
![Page 233: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/233.jpg)
row 3 × (-(79/9) /(75/36))+ row 2,
9 0 0
0 -8 0
0 0 75/36
18
-32
225/36
x1 = 2,
x2 = 4,
x3 = 3.
9 0 0
0 -8 79/9
0 0 75/36
18
-51/9
225/36
![Page 234: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/234.jpg)
Computer program:
Reorder the equations !
Maximum pivot technique
Avoids divided-by-zero errors.
Reduces round-off errors.
![Page 235: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/235.jpg)
Fig. 6-6 ( Subroutine simul, [a] [x] = [b] )
SUBROUTINE simul (a, b, ndim, n, error)IMPLICIT NONEINTEGER, INTENT(IN) :: ndimREAL, INTENT(INOUT), DIMENSION(ndim,ndim) :: aREAL, INTENT(INOUT), DIMENSION(ndim) :: bINTEGER, INTENT(IN) :: nINTEGER, INTENT(OUT) :: errorREAL, PARAMETER :: epsilon = 1.0E-06REAL :: factor, tempINTEGER :: irow, ipeak, jrow, kcol
! Process n times to reorder the eqs.mainloop: DO irow = 1, n ! Find peak pivot for column irow in rows irow to n ipeak = irow
![Page 236: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/236.jpg)
max_pivot: DO jrow = irow+1, n IF (ABS(a(jrow, irow)) > ABS(a(ipeak, irow))) THEN ipeak = jrow END IF END DO max_pivot ! Check for singular eqs.singular: IF ( ABS(a(ipeak, irow)) < epsilon)THEN error = 1 RETURN END IF singular! Otherwise, if ipeak /= irow, swap eqs irow and ipeak. swap_eqn : IF (ipeak /= irow) THEN DO kcol = 1, n temp = a(ipeak, kcol) a(ipeak, kcol) = a(irow, kcol) a(irow, kcol) = temp END DO
![Page 237: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/237.jpg)
temp = b(ipeak) b(ipeak) = b(irow) b(irow) = temp END IF swap_eqn ! (Eq. irow) * [-a(jrow, irow)/a(irow, irow)] + (Eq. jrow) eliminate: DO jrow = 1, n IF (jrow /= irow) THEN factor = -a(jrow, irow) / a(irow, irow) DO kcol = 1, n a(jrow, kcol) = a(irow, kcol) * factor + a(jrow, kcol) END DO b(jrow) = b(irow) * factor + b(jrow) END IF END DO eliminateEND DO mainloop
![Page 238: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/238.jpg)
divide: DO irow = 1, n b(irow) = b(irow) / a(irow, irow) a(irow, irow) = a(irow, irow) / a(irow, irow)END DO divideerror = 0END SUBROUTINE simul
![Page 239: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/239.jpg)
Fig. 6-7 (A test driven program)
PROGRAM test_simulIMPLICIT NONEINTEGER, PARAMETER :: max_size = 10INTEGER :: i, j, n, istat, errorREAL, DIMENSION(max_size, max_size) :: aREAL, DIMENSION (max_size) :: bCHARACTER(len=20) :: file_name
! Get the eqs.WRITE(*,1000)1000 FORMAT(‘Enter the filename containing the eqs:’)READ(*, ‘(A20)’) file_nameOPEN(UNIT=3, FILE=file_name, STATUS=‘OLD’, & ACTION=‘READ’, IOSTAT=istat)
![Page 240: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/240.jpg)
file_open: IF (istat == 0) THEN READ(3, *) n size_ok: IF ( n <= max_size) THEN DO i = 1, n READ(3, *) (a(i, j), j = 1, n), b(i) END DO! Display coefficiants.WRITE(*, 1020)1020 FORMAT (/, 1X, ‘ Coeffs. Before call:’) DO i = 1, n WRITE(*, 1030) (a(i, j), j = 1, n), b(i) 1030 FORMAT (1X, 7F11.4) END DO! Solve eqs.CALL simul(a, b, max_size, n, error)
![Page 241: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/241.jpg)
error_check: IF (error /= 0) THEN WRITE(*,1040) 1040 FORMAT (/1X, ‘Zero pivot encountered!’, & // 1X, ‘ No unique solu.’) ELSE error_check WRITE(*, 1050) 1050 FORMAT ( /, 1X, ‘ Coeffs. After call:’) DO i = 1, n WRITE(*, 1030) ( a(i, j), j = 1, n), b(i) END DO WRITE(*, 1060) 1060 FORMAT (/, 1X, ‘ The solus. are:’) DO i = 1, n WRITE(*, 1070) i, b(i) 1070 FORMAT (3X, ‘X(‘, I2, ‘)=‘, F16.6) END DO END IF error_checkEND IF size_ok
![Page 242: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/242.jpg)
ELSE file_open WRITE(*, 1080) istat 1080 FORMAT(1X, ‘File open failed – status=‘, I6)END IF file_openEND PROGRAM
Test: LINPUT.TXT
31.0 1.0 1.0 1.02.0 1.0 1.0 2.01.0 3.0 2.0 4.0
x1 + x2 + x3 = 12 x1 + x2 + x3 = 2 x1 + 3x2 +2 x3 = 4
Output: x1 = 1 x2 = 3 x3 = -3
![Page 243: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/243.jpg)
Sec. 6.2 The SAVE Attribute and Statement
e.g.,
REAL, SAVE :: sums
SAVE :: var1, var2, …
or
or
SAVE (all local variables)
Any local variables declared with the SAVE attributewill be unchanged between calls to the procedure.
![Page 244: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/244.jpg)
Example 6-5 Statiscal Analysis: (Running Averages)
Average: x_ave =
Σ xii=1
N
N
Standard deviation:
S = N Σxi
2 – (i=1 i=1
N N
Σxi )2
N (N-1)
1/2
Input: x (i.e., xi , i = 1, 2, …, N) 0 ≧
Output: x_ave and S
![Page 245: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/245.jpg)
e.g.,
xi N Σ xi Σxi2 xave S
3.0 1 3.0 9.0 3.00 0.02.0 2 5.0 13.0 2.50 0.707 3.0 3 8.0 22.0 2.67 0.5774.0 4 12.0 38.0 3.00 0.8162.8 5 14.8 45.84 2.96 0.713
![Page 246: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/246.jpg)
Fig. 6-8
SUBROUTINE running_average(x, x_ave, s, nvals, reset)! IF ‘reset’ is “.TRUE.”, clear running sums and exit.IMPLICIT NONEREAL, INTENT(IN) :: xREAL, INTENT(OUT) :: x_ave, sINTEGER, INTENT(OUT) :: nvalsLOGICAL, INTENT(IN) :: resetINTEGER, SAVE :: nREAL, SAVE :: sum_xREAL, SAVE :: sum_x2
calc_sums: IF (reset) THEN n=0; sum_x = 0. ; sum_x2 = 0. x_ave = 0. ; s = 0. ; nvals = 0ELSE n = n + 1
![Page 247: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/247.jpg)
sum_x = sum_x + x sum_x2 = sum_x2 + x**2 x_ave = sum_x / n IF (n >= 2) then s = SQRT((n*sum_x2 – sum_x **2)/(n*(n-1))) ELSE s = 0. END IF nvals = nEND IF calc_sumsEND SUBROUTINE running_average
![Page 248: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/248.jpg)
Fig. 6-9 (Test driven program)
PROGRAM test_running_averageIMPLICIT NONEINTEGER :: istat, nvalsREAL :: x_ave, s, xCHARACTER(len=20) :: filename
! Clear the running sumsCALL running_average(0., x_ave, s, nvals, .TRUE.)WRITE(*,*)’ Enter the file name containing the data:’READ(*,’(A20)’) filenameOPEN(UNIT=21, FILE=filename, STATUS=‘OLD’, & ACTION=‘READ’, IOSTAT=istat)open ok: IF(istat == 0) THEN calc: DO READ(21, *, IOSTAT=istat) x IF (istat /= 0) EXIT
![Page 249: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/249.jpg)
CALL running_average(x, x_ave, s, nvals, .FALSE.) WRITE(*,1020)’ Value = ‘, x, ‘ x_ave = ‘, x_ave, & ‘ Std_dev = ‘, s, ‘ N = ‘, nvals 1020 FORMAT(1X, 3(A, F10.4), A, I6) END DO calcELSE openok WRITE(*, 1030) istat 1030 FORMAT(1X, ‘File open failed-status = ‘, I6)END IF openokEND PROGRAM
Test: RUNNING.TXT
3.02.03.04.02.8
3.0 3.00 0.0 1 2.0 2.50 0.707 23.0 2.67 0.577 34.0 3.00 0.816 42.8 2.96 0.713 5
x x_ave S N
Output
![Page 250: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/250.jpg)
Sec. 6.3 Automatic Arrays
A local explicit-shape array with non-constant bounds.
e.g.,
SUBROUTINE sub1(x, y, n, m)IMPLICIT NONEINTEGER, INTENT(IN) :: n, mREAL, INTENT(IN), DIMENSION(n, m) :: xREAL, INTENT(OUT), DIMENSION(n, m) :: yREAL, DIMENSION(n, m) :: temp ! Auto. array
temp = 0.. . .END SUBROUTINE
( Auto. arrays are automatically destroyed when subroutine ends.)
![Page 251: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/251.jpg)
Sec. 6.4 Sharing Data Using Modules
Programs Subroutines (or functions)
argument list
(exchange data)
Programs Subroutines (or functions)(share data)
module
![Page 252: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/252.jpg)
Fig. 6-11 (a simple module)
MODULE testIMPLICIT NONESAVE INTEGER, PARAMETER :: num_vals = 5REAL, DIMENSION(num_vals) :: valuesEND MODULE test
(SAVE should always be included in any module that declares sharable data.)
To use the values in the module,
USE module_name
![Page 253: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/253.jpg)
Fig. 6-12 (using a module to share data between a main program and a subroutine)PROGRAM test_moduleUSE testIMPLICIT NONEREAL, PARAMETER :: pi = 3.141592
values = pi * (/ 1., 2., 3., 4., 5. /)CALL sub1END PROGRAM
SUBROUTINE sub1USE testIMPLICIT NONEWRITE(*,*)valuesEND SUBROUTINE sub1
Output
3.14159 6.28318 9.42478 12.5664 15.7080
![Page 254: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/254.jpg)
Sec. 6.5 Modules Procedures
e.g., MODULE my_subsIMPLICIT NONE. . . (declare shared data here)
CONTAINS SUBROUTINE sub1(a, b, c, x, error) IMPLICIT NONE REAL, DIMENSION(3), INTENT(IN) :: a REAL, INTENT(IN) :: b, c REAL, INTENT(OUT) :: x LOGICAL, INTENT(OUT) :: error . . . END SUBROUTINE sub1END MODULE my_subs
![Page 255: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/255.jpg)
PROGRAM main_progUSE my_subsIMPLICIT NONE. . .CALL sub1(a, b, c, x, error). . .END PROGRAM
Sec. 6.5.1 Using Modules to Creat Explicit Interfaces
Why module procedure?
Explicit interface
(helps the compiler to catch errors)
Implicit interface: Assume that the programmer got the arguments right. (e.g., number, type, intent, …)
![Page 256: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/256.jpg)
Fig. 6-15 (argument mismatch)MODULE my_subsIMPLICIT NONECONTAINS SUBROUTINE bad_argument(i) IMPLICIT NONE INTEGER, INTENT(IN) :: I WRITE(*,*) ‘ I=‘ ,i END SUBROUTINE END MODULE
PROGRAM bad_callUSE my_subsIMPLICIT NONEREAL :: x = 1.CALL bad_argument(x)END PROGRAM
The computer will catch the argument mismatch!
![Page 257: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/257.jpg)
Sec. 6.5.2 Assumed-shape Arrays
e.g., MODULE test_moduleCONTAINS SUBROUTINE process2(data1, data2) REAL, INTENT(IN), DIMENSION(:, :) :: data1 REAL, INTENT(IN), DIMENSION(:, :) :: data2 data2 = 3. * data1 END SUBROUTINE process2END MODULE test_module
( Assumed-shape arrays work only if a procedure has an explicit interface. However, the upper and lower bounds of each dimension cannot be determined.)
![Page 258: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/258.jpg)
Fig. 6-16 (The use of assumen-shape arrays)
MODULE test_moduleCONTAINS SUBROUTINE test_array(array) IMPLICIT NONE REAL, DIMENSION(:, :) :: array INTEGER :: i1, i2, j1, j2
i1 = LBOUND(array, 1) i2 = UBOUND(array, 1) j1 = LBOUND(array, 2) j2 = UBOUND(array, 2) WRITE(*, 100) i1, i2, j1, j2 100 FORMAT(1X, ‘ The bounds are: (‘, I2, ‘:’, I2,’,’, I2,’:’,I2,’)’) WRITE(*, 110) SHAPE(array) 110 FORMAT(1X, ‘ The shape is: ‘, 2I4) WRITE(*, 120) SIZE(array) 120 FORMAT(1X, ‘ The size is: ‘, I4) END SUBROUTINE test_arrayEND MODULE test_module
![Page 259: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/259.jpg)
PROGRAM assumed_shapeUSE test_moduleIMPLICIT NONEREAL, DIMENSION(-5:5, -5:5) :: a = 0.REAL, DIMENSION(10, 2) :: b = 1.WRITE(*,*) ’ Calling test_array with array a:’CALL test_array(a)WRITE(*,*) ‘ Calling test_array with array b:’CALL test_array(b)END PROGRAM
Output:Calling test_array with array a:The bounds are: (1:11, 1:11)The shape is: 11 11The size is: 121Calling test_array with array b:The bounds are: (1:10, 1:2)The shape is: 10 2The size is: 20
![Page 260: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/260.jpg)
Sec. 6.6 Fortran Functions
Intrinsic functions: e.g., SIN(x), LOG(x).
User-defined functions (or function subprograms)
The general form is
FUNCTION name ( argument_list) . . . (Declaration) . . . (Execution) . . . name = expression RETURNEND FUNCTION [name]
optional
![Page 261: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/261.jpg)
Two equivalent forms:
INTEGER FUNCTION my_function(i, j)
FUNCTION my_function(i, j)INTEGER :: my_function
or
Fig. 6-17 (f(x) = ax2 + bx + c)
REAL FUNCTION quadf (x, a, b, c)IMPLICIT NONEREAL, INTENT (IN) :: x, a, b, c
quadf = a*x**2 + b*x + cEND FUNCTION
![Page 262: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/262.jpg)
Fig. 6-18 (a test driven program)
PROGRAM test_quadfIMPLICIT NONEREAL :: quadfREAL :: a, b, c, x, temp
WRITE(*,*) ‘ Enter quadratic coeffs. a, b, and c:’READ(*,*) a, b, cWRITE(*,*) ‘ Enter x value:’READ(*,*) xTemp = quadf(x, a, b, c)WRITE(*,100) ‘ f(‘, x, ‘)=‘, temp100 FORMAT(A, F10.4, A, F12.4)END PROGRAM
Test: a = 1., b = 2., c=3.x = 2.
f (x) = 11.
![Page 263: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/263.jpg)
If a function modifies the values in its argument list,
side effects. (Always declares all dummy arguments with the INTENT(IN) attribute.)
A function produces a single output value using its input arguments. If need more than one output value, should use a subroutine not a function.
Example 6-9 The Sinc Function
sinc(x) = sin(x)/x and sinc(0)=1.
See Fig. 6-19
![Page 264: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/264.jpg)
Fig. 6-20 (sinc(x))
FUNCTION sinc(x)IMPLICIT NONEREAL, INTENT(IN) :: xREAL :: sincREAL, PARAMETER :: epsilon = 1.0E-30
IF(ABS(x) > epsilon) THEN sinc = sin(x) / xELSE sinc = 1.END IFEND FUNCTION sinc
![Page 265: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/265.jpg)
Fig. 6-21 (a test driven program)
PROGRAM test_sincIMPLICIT NONEREAL :: xREAL :: sinc
WRITE(*,*) ‘ Enter x:’READ(*,*) xWRITE(*, 100) ‘ sinc(x) = ‘, sinc(x)100 FORMAT(1X, A, F8.5)END PROGRAM
Test: x sinc(x)
0. 1.000001.0E-29 1.000003.141593 0.000001.570796 0.63662
![Page 266: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/266.jpg)
Sec. 6.7 Passing Functions or Subroutines as Arguments to Procedures
e.g., PROGRAM testREAL, EXTERNAL :: fun_1, fun_2REAL :: x, y, output. . .CALL evaluate(fun_1, x, y, output)CALL evaluate(fun_2, x, y, output). . .END PROGRAM
SUBROUTINES evaluate(fub, a, b, result)REAL, EXTERNAL :: funREAL, INTENT(IN) :: a, bREAL, INTENT(OUT) :: resultResult = b*fun(a)END SUBROUTINE evaluate
(two user-defined funcs)
![Page 267: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/267.jpg)
Two equivalent forms:
REAL, EXTERNAL :: fun_1, fun_2
EXTERNAL fun_1, fun_2
or
EXAMPLE 6-10 (Passing Functions to Procedures in an Arguments List)
Σf(xi) i=1
N
Nave = , x1 = 0, xN = 1, N = 101
Δx =xN – x1
N - 1= 0.01
xi = x1 + (i -1) × Δx
![Page 268: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/268.jpg)
Fig. 6-22
REAL FUNCTION ave_value (func, first_value, last_value, n)IMPLICIT NONEREAL, EXTERNAL :: funcREAL, INTENT(IN) :: first_value, last_valueINTEGER, INTENT(IN) :: nREAL :: delta, sumINTEGER :: I
Delta = (last_value – first_value) / (n-1)Sum = 0.DO I = 1, n sum = sum + func( first_value + (i-1) * delta)END DOAve_value = sum / nEND FUNCTION
![Page 269: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/269.jpg)
Fig. 6-23 (Test driven program)
PROGRAM test_ave_valueIMPLICIT NONEREAL :: ave_valueREAL, EXTERNAL :: my_functionREAL :: aveAve = ave_value(my_function, 0., 1., 101)WRITE(*,1000) ‘ my-function’, ave1000 FORMAT (1X, ‘ The ave. value of ‘, A, & ‘ between 0. and 1. is ‘, F16.6, ‘.’)END PROGRAM
REAL FUNCTION my_function(x)IMPLICIT NONEREAL, INTENT(IN) :: xMy_function = 3. * xEND FUNCTION
![Page 270: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/270.jpg)
Ch. 7 Additional Data Types
Data types:Real data typeComplex data typeDerived data type
Sec. 7.1 Alternative KINDS of the REAL Data Type
REAL data type:
Single precision (32 bits, default): 6 ~ 7 significant digits, 10-38 ~ 1038 (range).
Double precision (64 bits): 15 ~ 16 significant digits, 10-308 ~ 10308 (range).
![Page 271: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/271.jpg)
Sec. 7.1.1 Kinds of REAL Constants and Variables
Kind number:
e.g., REAL (KIND = 4) :: value_1REAL (KIND = 8), DIMENSION(20) :: arrayREAL (4) :: temp
A better approach:
INTEGER, PARAMETER :: single = 4INTEGER, PARAMETER :: double = 8REAL (KIND = single) :: value_1REAL (KIND = double), DIMENSION(20) :: arrayREAL (single) :: temp
Valid real constants:
34._4 34._double 3.0E0 3.0D0
(a single_precision const)
(a double_precision const)
(depends on machine)
![Page 272: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/272.jpg)
Sec. 7.1.2 Determining the KIND of a Real Variable
Fig. 7-1
PROGRAM kindsIMPLICIT NONEWRITE(*, 100) KIND(0.0)WRITE(*, 101) KIND(0.0D0)100 FORMAT(‘The KIND for single precision is’, I2)101 FORMAT(‘The KIND for double precision is’, I2)END PROGRAM
Output:
The KIND for single precision is 1The KIND for double precision is 2
(depends on machine)
![Page 273: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/273.jpg)
Sec. 7.1.3 Selecting Precision in a Process-Independent MannerUse
kind_number = SELECTED_REAL_KIND (p=precision, r=range)
e.g., kind_number = SELECTED_REAL_KIND (p = 6, r = 37)kind_number = SELECTED_REAL_KIND (p = 12)kind_number = SELECTED_REAL_KIND (r = 100)kind_number = SELECTED_REAL_KIND (13, 200)kind_number = SELECTED_REAL_KIND (13)kind_number = SELECTED_REAL_KIND (p = 17)
SELECTED_REAL_KIND(p, r)SELECTED_INT_KIND(r)KIND(x)PRECISION(x)RANGE(x)
Function
Table 7-1 ( KIND-related intrinsic functions)
![Page 274: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/274.jpg)
Fig. 7-2 (Select desired kinds and get information about real values)
PROGRAM select_kindsIMPLICIT NONEINTEGER, PARAMETER :: sgl = SELECTED_REAL_KIND (p=6, r=37)INTEGER, PARAMETER :: dbl = SELECTED_REAL_KIND (p=13, r=200)REAL (kind = sgl) :: var1 = 0.REAL (kind = dbl) :: var2 = 0._dbl
WRITE(*, 100) ‘ Var1’, KIND(var1), PRECISION(var1), RANGE(var1)WRITE(*, 100) ‘ Var2’, KIND(var2), PRECISION(var2), RANGE(var2)100 FORMAT (1X, A, ‘: Kind = ‘, I2, ‘, Precision = ‘, I2, ‘, Range = ‘, I3)END PROGRAM
Output:
Var1: Kind = 1, Precision = 6, Range = 37Var2: Kind = 2, Precision = 15, Range = 307
![Page 275: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/275.jpg)
Sec. 7.1.4 Mixed_Mode Arithmetic
e.g., 1/3 + 1/3 = 0.3333 … + 0.3333… = 0.6666 … (by hand)
By computer:
Expression Result
(1) 1.D0/3. + 1/3 3.333333333333333E-001(2) 1.D0/3. + 1./3. 6.666666333333333E-001(3) 1.D0/3. + 1./3.D0 6.666666666666666E-001
![Page 276: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/276.jpg)
PROGRAM testREAL(2) :: aREAL(1) :: b
A = 1.0D-208B = 1.0E-37WRITE(*,*)’a = ‘, aWRITE(*,*)’b = ‘, bEND PROGRAM
Output:
a = 1.000000000000E-0208b = 1.000000E-37
![Page 277: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/277.jpg)
Sec. 7.1.5 Double-Precision Intrinsic Functions
See Table B-1 (p. 501)
e.g., ABS, COS, SIN, EXP, LOG, . . .
COS(0.2) = 0.980067
COS(0.2d0) = 0.980066577841
Sec. 7.1.6 When to Use High-precision Real Values
Double-precision:
larger (twice) memory size and slower speed
reduces round-off error problems
![Page 278: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/278.jpg)
Example 7-1 (Numerical Calculation of Derivatives)
d f(x)d x = limit
Δx 0
f(x+Δx)- f(x)Δx
In theory: The small Δx, the better the estimate of the derivative.
But, in practice: ???
e.g.,
f(x) = 1/x, df(x)/dx = -1/x2.
d f(x)
d x x = 0.15= - 44.44444444 . . .
![Page 279: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/279.jpg)
Fig. 7-4 (The derivative of f(x) = 1/x at x = 0.15, single and double-precision)
PROGRAM diffIMPLICIT NONEINTEGER, PARAMETER :: single = SELECTED_REAL_KIND(p=6, r=37)INTEGER, PARAMETER :: double = SELECTED_REAL_KIND(p=13)INTEGER :: iREAL(KIND=double) :: ans, d_ans, d_error, d_fx, d_fxdx, d_dx, d_x = 0.15D0REAL(KIND=single) :: s_ans, s_error, s_fx, s_fxdx, s_dx, s_x = 0.15E0
WRITE(*,1)1 FORMAT(1X, ‘ DX TRUE ANS Sp ANS Dp ANS’, & ‘ Sp ERR DP ERR’)ans = -(1.D0/d_x**2)step_size: DO I = 1, 10 s_dx = 1.0 /10.0**i d_dx = 1.D0 / 10.D0 **I ! Calculate s-p ans. s_fxdx = 1. /(s_x + s_dx) s_fx = 1./(s_x) s_ans = (s_fxdx – s_fx) /s_dx
![Page 280: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/280.jpg)
s_error = ((s_ans – ans)/ans)*100. ! Calculate d-p ans. d_fxdx = 1. /(d_x + d_dx) d_fx = 1./(d_x) d_ans = (d_fxdx – d_fx) /d_dx d_error = ((d_ans – ans)/ans)*100. WRITE(*, 100) d_dx, ans, s_ans, d_ans, s_error, d_error 100 FORMAT(1X, ES10.3, F12.7, F12.7, ES22.14, F9.3, F9.3) END DO step_sizeEND PROGRAM
Output: see Textbook (p. 370)!
![Page 281: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/281.jpg)
Sec. 7.2 The Complex Data Type
c = a + i b = z θ,∠
a = z cosθ, b = z sin θ, θ= arctan (b/a).
e.g.,
c1 = a1 + i b1, c2 = a2 + i b2,
c1 ± c2 = (a1 ± a2) + i (b1± b2),
c1 × c2 = (a1a2 - b1b2) + i (a1b2 ± b1a2),
c1 (a1a2 + b1b2) + i (b1a2 - a1b2)
c2 (a22
+ b22 )
=
![Page 282: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/282.jpg)
Sec. 7.2.1 Complex Constants and Variables
Complex constant:
(1., 0.) 1 + i 0
(0.7071, 0.7071) 0.7071 + i 0.7071
(0, - 1) - i
(1.01E6, 0.5E2) 1010000 + i 50
(1.12_dbl, 0.1_dbl) 1.12 + i 0.1
Complex variable:
COMPLEX (KIND = kind_num) :: var1, var2, . . .COMPLEX, DIMENSION (256) :: array
![Page 283: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/283.jpg)
Sec. 7.2.2 Initializing Complex Variables
Eg. 1,COMPLEX, DIMENSION (256) :: array1array1 = (0., 0.)
Eg. 2,
Complex :: a1 = (3.141592, -3.141592)
Eg. 3,
COMPLEX :: a1READ(*, ‘(2F10.2)’) a1
COMPLEX :: a1READ(*, *) a1
or
INPUT: (no parentheses)e.g., 1.0 0.25
INPUT: (with parentheses)e.g., (1.0, 0.25)
![Page 284: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/284.jpg)
Sec. 7.2.3 Using Complex Numbers with Relational Operators
Given c1 = a1 + i b1, c2 = a2 + i b2,
c1 == c2 (to see if equal)
c1 /= c2 (to see if not equal)
c1 c2 (cannot compare!)
<><=>=
|c1| |c2| (compare magnitude)
<><=>=
![Page 285: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/285.jpg)
Sec. 7.2.4 Complex Intrinsic Functions
See App. B
1. Type conversion functions:
Given a and b,
COMPLX (a, b, kind) a + i b
Given c ( = a + i b),
REAL(c) a
AIMAG(c) b
2. Absolute function:
c = a + i b,
CABS(c) c = (a2 + b2)1/2
![Page 286: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/286.jpg)
3. Math functions: (generic functions)
COS, SIN, EXP, ABS, . . .
e.g.,
PROGRAM compxlCOMPLEX :: a = (1.0, 0.25)write(*,*) cos(a)END PROGRAM
Output:(0.55727, -0.21256)
![Page 287: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/287.jpg)
ax2 + bx + c = 0,
x = -b ± ( b2 – 4ac )1/2
2a
If b2 – 4ac = 0
b2 – 4ac > 0
b2 – 4ac < 0
two distinct real roots
two complex roots
a single repeated root
Example 7-3 The Quadratic Eq. (revisited)
![Page 288: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/288.jpg)
PROGRAM roots_2IMPLICIT NONEREAL :: a, b, c, d, re, imCOMPLEX :: x1, x2
WRITE(*,*)'Enter the coeffs. a, b, and c:‘READ(*,*) a, b, cd = b**2 – 4.*a*cx1=(-b + SQRT(CMPLX(d,0.))) / (2. * a)x2=(-b – SQRT(CMPLX(d,0.))) / (2. * a)WRITE(*,*) ‘The roots are:‘WRITE(*,100) ’x1=‘, REAL(x1), ‘+ i’, AIMAG(x1)WRITE(*,100) ’x2=‘, REAL(x2), ‘+ i’, AIMAG(x2)100 FORMAT(A, F10.4, A, F10.4)END PROGRAM roots_2
Fig. 7-10
![Page 289: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/289.jpg)
Test:
x2 + 5x + 6 = 0, x1,2 = -2, -3
x2 + 4x + 4 = 0, x1,2 = -2
x2 + 2x + 5 = 0, x1,2 = -1 ± i 2
![Page 290: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/290.jpg)
Sec. 7.3 Derived Data Types
User-defined data types derived from intrinsic data types.
A convenient way to group together all the information about a particular item.
Derived data type: Array:
Components (names) Elements (numbers)
different types the same type
![Page 291: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/291.jpg)
The form of a derived data type:
TYPE [::] type_name component definitions . . .END TYPR [type_name]
e.g.,
TYPE :: person CHARACTER(len=14) :: first_name CHARACTER :: middle_initial CHARACTER(len=14) :: last_name CHARACTER(len=14) :: phone INTEGER :: age CHARACTER :: sex CHARACTER(len=11) :: ssnEND TYPE person
(optional)
(optional)
![Page 292: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/292.jpg)
Declare variables of the derived type person:
TYPE (person) :: john, janeTYPE(person), DIMENSION(100) :: people
(an array of 100 variables of type person)
Structure constructor:
e.g.,
e.g.,
john = person(‘John’, ‘R’, ‘Jones’, ‘323-6439’, 23, ‘M’, ‘123-45-6789’)jane = person(‘Jane’, ‘C’, ‘Bass’, ‘332-3060’, 17, ‘F’, ‘999-99-9999’)
![Page 293: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/293.jpg)
A derived data type within another derived data type:
TYPE :: grade_info TYPE (person) :: student INTEGER :: num_quizzes REAL, DIMENSION(10) :: quiz_grades INTEGER :: num_exams REAL, DIMENSION(10) :: exam_grades INTEGER :: final_exam_grade REAL :: averageEND TYPETYPE(grade_info), DIMENSION(30) :: class
(an array class of 30 variables of type grade_info)
e.g.,
![Page 294: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/294.jpg)
Sec. 7.3.1 Working with Derived Data Types
Component Selector:
e,g,.
john % age = 35
class(5) % final_exam_grade = 95
class(5) % student % age = 23
(variable of a derived data type)
(a component)
(5th student in the class)
(the age of the 5th student in the class)
![Page 295: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/295.jpg)
Sec. 7.3.2 Input and Output of Derived Data Types
Fig. 7-11 (I/O of variables of derived data types)
PROGRAM test_ioIMPLICIT NONETYPE :: person CHARACTER (len = 14) :: first_name CHARACTER :: middle_initial CHARACTER (len = 14) :: last_name CHARACTER (len = 14) :: phone INTEGER :: age CHARACTER :: sex CHARACTER (len = 11) :: ssnEND TYPE personTYPE (person) :: johnjohn = person(‘John’, ‘R’, ‘Jones’, ‘323-6439’, 23, ‘M’, ‘123-45-6789’)WRITE(*,*) ‘ Free format:’, johnWRITE(*,1000) john1000 FORMAT(‘ Formatted I/O:’, /, 4(1X, A, /), 1X, I4, /, 1X, A, /, 1X, A)END PROGRAM
![Page 296: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/296.jpg)
Sec. 7.3.3 Declaring Derived Data Types in Modules
Example 7-4 (Sorting derived data types by components)
Customer database:
John Q Public 123 Sesame Street Anywhere NY 10035James R Johnson Rt. 5 Box 207c West Monroe LA 71291. . .
Display the database in alphabetical order by last name, by city, or by zip code.
![Page 297: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/297.jpg)
Fig. 7-12 (sort a customer database)
MODULE typesIMPLICIT NONETYPE :: personal_info CHARACTER(len=12) :: first CHARACTER(len=12) :: mi CHARACTER(len=12) :: last CHARACTER(len=26) :: street CHARACTER(len=12) :: city CHARACTER(len=2) :: state INTEGER :: zipEND TYPE personal_infoEND MODULE types
![Page 298: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/298.jpg)
PROGRAM customer_databaseUSE typesIMPLICIT NONEINTEGER, PARAMETER :: max_size = 100LOGICAL, EXTERNAL :: lt_lastLOGICAL, EXTERNAL :: lt_cityLOGICAL, EXTERNAL :: lt_zipTYPE(personal_info), DIMENSION(max_size) :: customersLOGICAL :: exceed = .FALSE.CHARACTER (len=20) :: filenameINTEGER :: choice, i, nvals=0, statusTYPE(personal_info) :: temp
WRITE(*,*) ‘ Enter the file name:’READ(*,’(A20)’) filenameOPEN(UNIT=9, FILE=filename, STATUS=‘OLD’, IOSTAT=status) Fileopen: IF(status == 0) THEN
![Page 299: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/299.jpg)
DO READ(9,1010, IOSTAT=status) temp 1010 FORMAT(A12,1X,A1,1X,A12,1X,A26,1X,A12,1X,A2,1X,I5) IF(status /= 0)EXIT nvals = nvals + 1 size: IF (nvals <= max_size) THEN customers(nvals) = temp ELSE exceed = .TRUE. END IF size END DO toobig: IF(exceed) THEN WRITE(*,1020) nvals, max_size 1020 FORMAT (‘ Max. array size exceeded:’, I6, ‘>’, I6)ELSEWRITE(*,1030)1030 FORMAT(1X, ‘Enter the way to sort database:’, /, 1X, & ‘ 1 - By last name’, /,1X, ‘ 2 - By city’, /, 1X, ‘ 3 - By zip code’)
![Page 300: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/300.jpg)
READ(*,*) choice SELECT CASE (choice) CASE (1) CALL sort_database(customers, nvals, lt_last) CASE (2) CALL sort_database(customers, nvals, lt_city) CASE (3) CALL sort_database(customers, nvals, lt_zip) CASE DEFAULT WRITE(*,*)’ Invalid choice entered!’ END SELECT WRITE(*,’(A)’) ‘ The sorted database values are:’ WRITE(*, 1010)(customers(i), i=1, nvals) END IF toobigELSE fileopen WRITE(*,’(A, I6)’) ‘ File open error: IOSTAT=‘, statusEND IF fileopenEND PROGRAM
![Page 301: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/301.jpg)
SUBROUTINE sort_database (array, n, lt_fun)USE typesIMPLICIT NONEINTEGER, INTENT(IN) :: nTYPE(personal_info), DIMENSION(n), INTENT(INOUT) :: arrayLOGICAL, EXTERNAL :: lt_funINTEGER :: i, iptr, jTYPE(personal_info) :: tempouter: DO I = 1, n-1 iptr = I inner: DO j = i+1, n minval: IF (lt_fun(array(j), array(iptr))) THEN iptr = j END IF minval END DO inner swap: IF( I /= iptr) THEN temp=array(i) array(i) = array(iptr) array(iptr) = temp END IF swapEND DO outerEND SUBROUTINE sort_database
![Page 302: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/302.jpg)
LOGICAL FUNCTION lt_last(a,b)USE typesIMPLICIT NONETYPE(personal_info), INTENT(IN) :: a, blt_last = a%last < b%lastEND FUNCTION lt_last
LOGICAL FUNCTION lt_city(a,b)USE typesIMPLICIT NONETYPE(personal_info), INTENT(IN) :: a, blt_city = a%city < b%cityEND FUNCTION lt_city
LOGICAL FUNCTION lt_zip(a,b)USE typesIMPLICIT NONETYPE(personal_info), INTENT(IN) :: a, blt_zip = a%zip < b%zipEND FUNCTION lt_zip
![Page 303: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/303.jpg)
Test:
DATABASE.TXT
John Q Public 123 Sesame Street Anywhere NY 10035James R Johnson Rt. 5 Box 207C West Monroe LA 71291Joseph P Ziskend P. O. Box 433 APO AP 96555Andrew D Jackson Jackson Square New Orleans LA 70003Jane X Doe 12 Lakeside Drive Glenview IL 60025Colin A Jeffries 11 Main Street Chicago IL 60003
![Page 304: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/304.jpg)
Polar Coordinates: (extra)
c = a + i b = z θ,∠
z = (a2 + b2)1/2 , θ θ= arctan (b/a).a = z cosθ, b = z sin θ.
Given
p1 = z1 θ∠ 1, p2 = z2 θ∠ 2, θ1, θ2: in degrees.
p = p1 + p2 = z θ, z = ???, θ= ???.∠
Prob. Creat a derived data type called polar for z θ∠ ,
two components: magnitude z
angle θ
![Page 305: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/305.jpg)
MODULE mathIMPLICIT NONEREAL, PARAMETER :: const = 57. 296 ! 1 rad=57.296 degreesTYPE :: polar REAL :: z ! magnitude REAL :: theta ! AngleEND TYPE polarEND MODULE math
PROGRAM test_polarUSE mathIMPLICIT NONETYPE(polar) :: p1, p2, pWRITE(*,*)’ Enter z1 and theta1 (in degrees):’READ(*,*) p1%z, p1%thetaWRITE(*,*)’ Enter z2 and theta2 (in degrees):’READ(*,*) p2%z, p2%thetaCALL add_polar(p1, p2, p)WRITE(*,*)’ z = ‘, p%z, ‘ theta = ‘, p%theta, ‘ degrees’END PROGRAM
![Page 306: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/306.jpg)
SUBROUTINE add_polar(p1, p2, p)USE mathIMPLICIT NONETYPE(polar), INTENT(IN) :: p1, p2TYPE(polar), INTENT(OUT) :: pREAL :: a, ba = p1%z*cos(p1%theta/const) + p2%z*cos(p2%theta/const)b = p1%z*sin(p1%theta/const) + p2%z*sin(p2%theta/const)p%z = SQRT(a**2 + b**2)p%theta = ATAN2(b, a) * constRETURNEND SUBROUTINE
Test:
p1 = 3.0 30∠ 0, p2 = 4.0 60∠ 0,
Z = 6.766, θ= 47.190 .
![Page 307: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/307.jpg)
Ch. 8 Advanced Features of Procedures and Modules
Sec. 8.1 Internal Procedures
Internal procedure
host program unit
![Page 308: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/308.jpg)
Fig. 8-1 ( Sec(θ) )
PROGRAM test_secIMPLICIT NONEINTEGER, PARAMETER :: single = kind(0.0)REAL (KIND = single), PARAMETER :: pi = 3.141592REAL (KIND = single) :: thetaWRITE(*,*) ‘ Enetr angle in degrees:’READ(*,*) thetaWRITE(*, ‘(A, F10.4)’) ‘ The Secant is ‘, secant(theta)CONTAINS REAL FUNCTION secant(angle_in_degrees) REAL (KIND = single) :: angle_in_degrees secant = 1. /cos(angle_in_degrees * pi / 180.) END FUNCTION secantEND PROGRAM test_sec
Test: θ= 450, Sec(θ) = 1.4142
![Page 309: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/309.jpg)
Sec. 8.2 Recursive Procedures
e.g., N! =
N(N-1)!, N 1≧
1, N = 0
Fig. 8-2RECURSIVE SUBROUTINE factorial (n, result)IMPLICIT NONEINTEGER, INTENT(IN) :: nINTEGER, INTENT(OUT) :: resultINTEGER :: tempIF ( n >= 1 ) THEN CALL factorial (n-1, temp) result = n * tempELSE result = 1END IFEND SUBROUTINE factorial
![Page 310: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/310.jpg)
Fig. 8-3
RECURSIVE FUNCTION fact(n) RESULT(answer)IMPLICIT NONEINTEGER, INTENT(IN) :: nINTEGER :: answer
IF ( n >= 1 ) THEN answer = n * fact(n-1)ELSE answer = 1END IFEND FUNCTION fact
![Page 311: Introduction to Fortran 90/95 by Stephen J. Chapman Grading system: 1. Attendance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Tues. 11:10~12:00.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649e665503460f94b60feb/html5/thumbnails/311.jpg)
PROGRAM test_factorialIMPLICIT NONEINTEGER, EXTERNAL :: factINTEGER :: n, resultWRITE (*,*) ‘ Enter n ( >=0):’READ (*,*) nCALL factorial (n, result)WRITE(*,*) n, ‘! =‘, resultWRITE(*,*) n, ‘! =‘, fact(n)END PROGRAM
Test: 7 ! = 5040