Академический Документы
Профессиональный Документы
Культура Документы
31 characters
declaration
section
program my_first_program
! Declare variables
integer :: i, j, k ! i, j, k are integer
! Interactive input
write(*,*) 'Enter the numbers'
read(*,*) i, j
execution
section
comment after
exclamation point
termination
section
stop
end program
tell compiler no
more statement
(uppercase) 26
(lowercase) 26
(numbers) 10
(underscore) 1
- * / ** (arithmetic symbols) 5
) . = , ' $ : ! " % & ; < > ? blank (miscellaneous symbols) 17
~ Z
~ z
~ 9
Fortran Statement
Executable statement
Non-executable statement :
provide information necessary for the proper operation of the program
Free-source form statement (90/95/2003)
Fixed-source form statement (77 and earlier version)
But backward compatible,
i.e., you can use fixed-source form in 90/95, but cannot use free-source form in 77.
12
x = y+z
12
x = y &
+z
12
&
x = y &
+z
12
&
x = y
+z
! summation of x and y
continuation indicator
40 lines
syntax error
statement
label column
1
instructions column
5
9 2
9 10 11 12 13 14 15
x = y + z
2 4
w r i t e
1
( * , * )
A Hollerith card that, when punched, will contain one Fortran statement.
Punching machine
Card reader
What computer (IBM 704) looked like when Fortran was introduced.
money$
(cannot use $)
Integer Constant
legal integer constants:
0
-9999
+18
14
Real Constant
legal real constants:
10.
-99.9
+1.0E-3
124.8E20
0.15E+1
(=1 x 10-3)
(=1.248 x 1022)
A real value is stored in two parts: the mantissa and the exponent
3.14159265
+1.0E-3
(=1.0 x 10-3)
124.8E20
(=1.248 x 1022)
0.15E+1
precision in
decimal digits
bits in exponent
range in
exponent
32
24
10-38 ~ 1038
current Intel
64-bit PC
32
24
10-38 ~ 1038
declared as
double precision
64
53
15
11
10-308 ~ 10308
64
49
14
15
10-2465 ~ 102465
Cray
(supercomputer)
Logical Constants
(Logical constants are rarely used)
.TRUE.
.FALSE.
PROGRAM my_first_program
! Purpose:
! To illustrate some of the basic features of a Fortran program.
!
! Declare the variables used in this program.
INTEGER :: i, j, k ! All variables are integers
! Get the variables to multiply together.
WRITE(*,*) 'Enter the numbers to multiply: '
READ(*,*) i, j
! Multiply the numbers together.
k = i * j
! Write out the result.
WRITE(*,*) 'Result = ', k
! Finish up.
STOP
END PROGRAM
If original i is 3,
after the assignment statement i is 4
Arithmetic Operators: + - * / **
a*-b
wrong
operation a**-2
a*(-b)
a**(-2)
2**((8+2)/5) = 2**(10/5)
= 2**2
= 4
Integer Arithmetic
i = 3/4
4/4
5/4
6/4
7/4
8/4
9/4
0 is assigned to i
1
1
1
1
2
2
0.75
0.333333...
a**b**c = a**(b**c)
(a**b)**c
43 = 49 = 262144
642 = 4096
The computer converts the integers into real numbers, and real arithmetic is used.
3/2
3./2.
3./2
3/2.
1
1.5
1.5
1.5
integer (= 0)
1+1/4
1.+1/4
1.
1+1./4
real (=0.25)
1.25
But
(-2.0)**2.0
(X 0)
ABS(X)
R/I
R/I
SIN(X)
(X in radians)
COS(X)
(X in radians)
TAN(X)
(X in radians)
EXP(X)
ALOG(X)
ALOG10(X)
INT(X)
NINT(X)
REAL(I)
MOD(A,B)
MAX(A,B)
MIN(A,B)
MIN(A,B)
ASIN(X)
ACOS(X)
ATAN(X)
PROGRAM my_first_program
! Purpose:
! To illustrate some of the basic features of a Fortran program.
!
! Declare the variables used in this program.
INTEGER :: i, j, k ! All variables are integers
! Get the variables to multiply together.
WRITE(*,*) 'Enter the numbers to multiply: '
READ(*,*) i, j
! Multiply the numbers together.
k = i * j
! Write out the result.
WRITE(*,*) 'Result = ', k
! Finish up.
STOP
END PROGRAM
The types of the variables in the input variable list determine the required
format of the input data.
read(*,*) i, j
begin to read another line of data
read(*,*) k, l
write(*,*) " Output: ", i, j, k, l
> 1, 2, 3, 4
> 5, 6, 7
> Output: 1 2 5 6
input in console
output in console
Initialization of Variables
3 ways to initialize a variable:
integer :: i
i = 1
real :: pi
pi = acos(-1.)
integer :: i
read(*,*) i
real :: pi
read(*,*) i
integer :: i = 1
real :: pi = acos(-1.)
Program Example:
5
T t 32 273.15
9
Convert temperature T in degrees of Fahrenheit (F) to an absolute
temperature t in Kelvin (K).
5
=
32 + 273.15
9
program temp_conversion
! Purpose: To convert an input temperature from degrees
!
Fahrenheit to an output temperature in kelvins.
implicit none
! Force explicit declaration of variables
! Declare variables, and define each variables when it is declared
real :: temp_f
! Temperature in degrees Fahrenheit
real :: temp_k
! Temperature in kelvins
! Prompt the user for the input temperature.
write(*,*) 'Enter the temperature in degrees Fahrenheit: '
read (*,*) temp_f
! Convert to kelvins.
temp_k = (5./9.)*(temp_f-32.)+273.15
! Write out the result.
write(*,*) temp_f, ' degrees Fahrenheit = ', temp_k, ' kelvins '
! Finish up.
end program
In Emacs editor, create a new file named 2-6.f90 in the folder \examples
Copy the program from course webpage and paste on the Emacs editor, and save it.
>gfortran 2-6.f90
>dir
>a.exe
Enter the temperature in degrees Fahrenheit:
38.
38.000000
degrees Fahrenheit =
276.48334
kelvins
>
>gfortran 2-6.f90 -o 2-6.out
>dir
>2-6.out
Enter the temperature in degrees Fahrenheit:
80.
80.000000
>
degrees Fahrenheit =
299.81665
kelvins
Homework 1
Write a Fortran program which prompts to input your name (in English) and ID as in the
example program, and output as follow:
My name is your name.
My student ID is your id.
Homework 2
Write a Fortran program to computer the area of a triangle determined by jointing the
three points (x1,y1), (x2,y2) and (x3,y3) given by a user who executes the program.
Use implicit none statement in your program.
Ask to receive a return receipt when mailing your homework as shown in the figure.