You are on page 1of 27

Department of Civil Engineering

University of Malaya
Course Code :KAEA 2101
Course Name :Computer Programming
Chapter 2 :Elementary of Fortran
Prepared By :AP. Dr. Lai Sai Hin
Previous Example: Radioactive decay








PROGRAM Radioactive_Decay
!-------------------------------------------------------------------------------------------------------------------------------
! This program calculates the amount of a radioactive substance that
! remains after a specified time, given an initial amount and its
! half-life. Variables used are:
! InitialAmount : initial amount of substance (mg)
! Half Life : half-life of substance (days)
! Time : time at which the amount remaining is calculated (days)
! AmountRemaining : amount of substance remaining (mg)
!
! Input: InitialAmount, Half Life, Time
! Output: AmountRemaining
!-------------------------------------------------------------------------------------------------------------------------------
IMPLICIT NONE

REAL :: InitialAmount, Half Life, Time, AmountRemaining

! Get values for InitialAmount, Half Life, and Time.
PRINT *, "Enter initial amount (mg) of substance, its halflife (days)"
PRINT *, "and time (days) at which to find amount remaining:"
READ *, InitialAmount, Half Life, Time

! Compute the amount remaining at the specified time.
AmountRemaining = InitialAmount * 0.5 ** (Time / Half Life)

! Display AmountRemaining.
PRINT *, "Amount remaining =, AmountRemaining, "mg"

END PROGRAM Radioactive_Decay
Data Type
Data Types
Fortran Provides 5 basic data types
Integer
Real
Complex
Character
Logical


Numeric types
Constant/variable
Chapter 3
Data Types
Integers
A whole number
Positive, negative, or Zero
Does not contain comma or decimal point
0
137
+2516
-17745
9,999
16.0
- - 5
7-

Valid integer constants
Invalid
Data Types
Reals
ordinary decimal numbers, or in exponential notation
Positive or negative
Must contain a decimal point, without comma
123.234
-0.01536
+56473.02
+56473.
12,345.03
580
1.23234E2
123.234 E0
12323.4E-2
123234E-3
Valid real constants
Invalid
Valid exponential representation
Data Types
Character Strings
Also called strings
Sequences of symbols from the Fortran character set
Must be enclosed between double quotes or between
apostrophes.
PDQ123-A
John Q. Dow
John Q. Doe
Dont
John Q. Doe
John Q. Doe



Invalid
Valid
Data Types
Character Strings



Data Types
Identifiers






Mass
Speed_of_Light
speed-of Light
6Feet
Velocity
VELOCITY
vELocITy


Invalid
Same
e.g.:READ, PRINT
Valid
Data Types
Variables
Symbolic name used to refer to a quantity
InitialAmount, Half Life, Time, AmountRemaining
Variable names are identifiers, must follow the rules for
forming valid identifiers
Necessary to declare the type of each variable in a
Fortran program
REAL :: list
REAL :: InitialAmount, HalfLife, Time, AmountRemaining
INTEGER :: list
CHARACTER (LEN = N) :: list or CHARACTER (N) :: list
CHARACTER (LEN = 15) :: FirstName, LastName
CHARACTER :: Initial
CHARACTER (LEN =15) :: FirstName, LastName*20, Initial*1, City
Length = 1
Override the specified length
Data Types
IMPLICIT NONE Statement

Variable Initialization
Initial values can be assigned to variables (at compile
time)
REAL :: list
REAL :: W, X, Y, Z
REAL :: W = 1.0, X = 2.5, Y = 7.73, Z = -2.956

Constant Attribute
Fortran allows programmer to specify a constant by including
a PARAMETER attribute in the declaration of the identifier
For frequently used constant such as:
INTEGER :: Limit
INTEGER, PARAMETER :: Limit = 50
CHARACTER(2) :: Unit
CHARACTER(2), PARAMETER :: Unit = cm
REAL :: Pi, TwoPi, g
REAL, PARAMETER :: Pi = 3.141593, TwoPi = 2.0*Pi, g = 9.81
REAL, PARAMETER :: BirthRate = 0.1758, DeathRate = 0.1257

Why need to declare attribute ???
Increase readability,
Easier to modification.
Change = (0.1758-0.1257) * Population
Population = Population + Change
:
PopulationIncrease = 0.1758 * population
PopulationDevrease = 0.1257 * Population
Arithmetic Operations & Functions
Operation
In Fortran
Addition (+), Subtraction (-), Multiplication (*), Division
(/), Exponentiation (**)
B
2
- 4AC B ** 2 4 * A * C
- (A + B) OK
N * -2 not OK N * (-2) (OK)
When 2 constants or variables of the same type are
combined using one of the 4 basic arithmetic operation,
the result has the same type as the operands
3 + 4 = 7
3.0 + 4.0 = 7.0
9.0 / 4.0 = 2.25
9 / 4 = 2
Arithmetic Operations & Functions
Mixed-Mode Expressions
it is possible to combine integer and real quantities
When an integer quantity is combined with a real one,
the integer quantity is converted to its real equivalent,
the result is of real type



???
A real exponent
should be never be
used in place of an
integer exponent
or
Arithmetic Operations & Functions
Mixed-Mode Expressions

4. The standard order of evaluation can be modified using parentheses to enclose
subexpressions, the subexpressions are evaluated first in the standard manner.
5. If the parentheses are nested, the computations in the inner most parentheses are
performed first.
(5 * (11 5) ** 2) * 4 + 9 (5 * 6 ** 2) * 4 + 9 180 * 4 + 9 720 + 9 729
Arithmetic Operations & Functions
Functions
Fortran provides library functions for many of the
common mathematical operations and functions
SQRT(argument)
argument is a real-valued constant, variable, expression
SQRT(7.0)
SQRT(B**2-4.0*A*C) Nonnegative value only
to calculate SQRT of an integer, necessary to convert it to a real value
SQRT(REAL(7))

Arithmetic Operations & Functions
Basic Fortran Functions

Arithmetic Operations & Functions
The ASSIGNMENT STATEMENT
Used to assign values to variables
Suppose that Xcoordinate and YCoordinate are real variables, Number and Term
are integer variables.
REAL :: Xcoordinate, Ycoordinate
INTEGER :: Number, Term
Consider the following Assignment Statements
XCoordinate = 5.23
YCoordinate = SQRT(25.0) 5.0
Number = 17
Term = Number / 3 + 2 7

Arithmetic Operations & Functions
Mixed-Mode ASSIGNMENT
If an integer-valued expression is assigned to a real variable,
the integer value is converted to a real constant and then
assigned to the variable
if N has the value 9, Alpha and Beta are real variable,
Alpha = 3 3.0
Beta = (N + 3) / 5 2.0

If an real -valued expression is assigned to an integer
variable, the fraction part of the real value is truncated, the
integer part is assigned to the variable

if X has the value 5.75, I, Kappa, Mu are integer variables
I = 3.14159 3
Kappa = X / 2.0 2
Mu = 1.0 / X 0

Input / Output
Fortran provides 2 types of input/output statements
Type 1
- Explicitly specify the format of input & output.
Type 2: list-directed input/output
- predetermined standard formats that match the input/output
list are automatically provided by the compiler
- List-Directed Input
READ *, InitialHeight, InitialVelocity, Time
- List-Directed Output



Execution
100.0, 90.0, 4.5
100.0 90.0 4.5
100.0 90.0
4.5
Example 1: Velocity and Height of a Projectile
!
Program Composition and Format
General form of a program in Fortran
Heading
PROGRAM name
Opening documentation
Specification Part / Type Declaration
IMPLICIT NONE
Declaration statement
Execution part
Nonexecutable statements
- provide information, do not cause any specific action to be performed during
execution,
e.g.: PROGRAM statement & type statements
Executable statements
- specify actions to be performed during execution
e.g.: Assignment statements, input/output ststements
Subprogram part
- contain internal subprogram, chapter 6
END PROGRAM statement

File Input/Output
Interactive Input/Output
So far we assumed the data for the sample program was entered from
the keyboard, and the output was displayed on the screen
Input/Output Using File
To store input data in a disk file, design the program to read data from
this file, and to store a program output in a disk file.
E.g.: Projectile Program
- input value are read from file named fig2-6.dat
100.0, 90.0
4.5
- output to be written to a file named fig2-6.out
At time 4.5000000 seconds
the vertical velocity is 45.8700752 m/sec
and the height is 4.0570767E+02 meters
Opening Files / OPEN statement
- before a file can be used for input or output, it must be opened
OPEN (UNIT = unit-number, FILE = file-name, STATUS = status)
unit-number is an integer that will be used to reference this file in a READ or WRITE
statement, status =OLD if the file already exists, or NEW otherwise
OPEN(UINT = 12, FILE = fig2-6.dat, STATUS = OLD)
OPEN(UNIT = 13, FILE = fig-2.6.out, STATUS = NEW)


File Input/Output
Opening Files / OPEN statement
- before a file can be used for input or output, it must be opened
OPEN (UNIT = unit-number, FILE = file-name, STATUS = status)
unit-number is an integer that will be used to reference this file in a READ or WRITE
statement, status =OLD if the file already exists, or NEW otherwise
OPEN(UINT = 12, FILE = fig2-6.dat, STATUS = OLD)
OPEN(UNIT = 13, FILE = fig2-6.out, STATUS = NEW)
File I/O
- once a file has been given a unit number, data can be read from or written
to that file.
READ (unit-number, *) input-list
WRITE (unit-number, *) output-list
READ (12, *) InitialHeight, InitialVelocity, Time
WRITE (13, *) At time, Time, seconds
WRITE (13, *) the vertical velocity is, Velocity, m/sec
WRITE (13, *) and the height is, Height, maters
At time 4.5000000 seconds
the vertical velocity is 45.8700752 m/sec
and the height is 4.0570767E+02 meters
Execution
Fig2-6.out
Example 2: Projectile program with file I/O
Example 3:
read Section 2.7, Page 78, Nyhoff & Leestma, 1997
Example 4:
read Section 2.8, Page 84, Nyhoff & Leestma, 1997
Example 5:
read Section 2.9, Page 88, Nyhoff & Leestma, 1997
Thank You

You might also like