Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Computer Programming Using Oo-Fortran[1]

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Computer Programming Using Oo-Fortran[1]

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

COMPUTER PROGRAMMING

USING OO-FORTRAN
Fortran Evolution
• Fortran stands for FORmula TRANslation. The first
compiler appeared in 1957 and the first official
standard in 1972 which was given the name of
`Fortran 66'. This was updated in 1980 to Fortran
77, updated in 1991 to Fortran 90, updated in
1997 to Fortran 95, and further updated in 2004
to Fortran 2003. At each update some
obsolescent features were removed, some
mistakes corrected and a limited number of new
facilities were added.
Character Set
• The following are valid in a Fortran 95
program:
• alphanumeric: a-z, A-Z, 0-9, and _ (the
underscore); the lower case letters are
equivalent to the upper case letters
• symbolic:
Intrinsic Types
• Fortran 95 has two broad classes of object
type:
•  numeric;
•  non-numeric

• which give rise to six simple intrinsic types,


known as default types. These are
demonstrated by the following code:
Intrinsic Types
Numeric Storage
• In general, there are two types of numbers used in Fortran 95
programs, INTEGERs (whole numbers) and REALs (floating
point numbers).
• INTEGERs are stored exactly, often in the range [-32768,
32767].
• REALs are stored approximately.
• Their form is a mantissa and an exponent. For example 6.6356
x 1023 . The exponent can take only a finite range of values,
typically [-307, 308].
• You can get numeric exceptions if you try to assign a value
outside the permitted range of values to a variable.
• In Fortran 95 you can decide what numeric range is to be
supported. CHARACTERs are stored differently.
Literal Constants
• A literal constant is an entity with a fixed
value. For example:
Literal Constants
• Note:
•  REALs contain a decimal point, INTEGERs do not;
•  REALs can have an exponential form;
•  there is only a finite range of values that numeric
literals can take;
•  character literals are delimited by a pair of " or a pair
of ';
•  two occurrences of the delimiter inside a string
produce one occurrence on output;
•  there are only two LOGICAL values.
Names
• In Fortran 95 English (or any other natural language)
names can be assigned to variables (memory
locations) and procedures etc. Each name:
•  must be unique within the program;
•  must start with a letter;
•  may use only letters, digits and the underscore;
•  may use the underscore to separate words in long
names;
•  may not be longer than 31 characters.
Names
Significance of Blanks
• In free form source code blanks must not
appear:
•  within keywords;
•  within names.
Significance of Blanks
• Blanks must appear:
•  between two separate keywords;
•  between keywords and names not
otherwise separated by punctuation or other
special characters.
Significance of Blanks
• Blanks must appear:
•  between two separate keywords;
•  between keywords and names not
otherwise separated by punctuation or other
special characters.
Implicit Typing
• Any undeclared variable has an implicit type:
•  if the first letter of its name is I, J, K, L, M or
N then the type is INTEGER;
•  if it is any other letter then the type is REAL.

• Implicit typing is potentially very dangerous


and should always be turned off by adding:
»IMPLICIT NONE
Implicit Typing
• at the start of the declaration of variables.
Consider:
REAL :: body_temp 98.4
...
bodytemp = 36.9
• With implicit typing this declares a REAL
variable bodytemp and sets it to 36.9 and
leaves the value in the variable body_temp
unaltered.
Numeric and Logical Type Declarations
• With IMPLICIT NONE variables must be
declared. A simplified syntax follows:
• < type > [,< attribute-list >] :: < variable-list >&
• [ =< value >]
• Optional components are shown in [square
brackets]
Numeric and Logical Type Declarations
• The following are all valid declarations:
• INTEGER :: i, j
• REAL :: x, y
• COMPLEX :: val
• LOGICAL :: on, off
Character Declarations
• Character variables are declared in a similar
way to numeric types. CHARACTER variables
can:
•  refer to one character;
•  refer to a string of characters which is
achieved by adding a length specifier to the
object declaration.
Character Declarations
• The following are all valid declarations:
• CHARACTER :: sex
• CHARACTER(LEN=10) :: name
• CHARACTER(LEN=10),
DIMENSION(10,10) :: Harray
Initialisation
• Declaring a variable does not automatically
assign a value, say zero, to this variable: until a
value has been assigned to it a variable is
known as an unassigned variable. Variables
can be given initial values, which can use
initialisation expressions and literals. Consider
these examples:
Initialisation
• INTEGER :: i = 5, j = 100
• REAL :: x, y = 1.0E5
• COMPLEX :: val = (1.0,1.732)
• CHARACTER(LEN=5) :: light = 'Amber'
• CHARACTER(LEN=9) :: gumboot = 'Wellie'
• LOGICAL :: on = .TRUE., off = .FALSE.
How to Write a Computer Program
• There are 4 main steps:
• 1. specify the problem;
• 2. analyse and break down into a series of steps
towards solution;
• 3. write the Fortran 95 code;
• 4. compile and run (i.e., test the program).

• It may be necessary to iterate between steps 3 and 4 in


order to remove any mistakes. The testing step is very
important.
How to Write a Computer Program
• For example, consider a program to convert a
temperature from Fahrenheit to Celsius scale.
• To convert from oF (Fahrenheit) to oC (Celsius)
we can use the following formula:
• c = 5 x (f - 32)/9
• To convert from oC to oK (Kelvin) we add 273.
How to Write a Computer Program
• The algorithm consists of:
• 1. READ a value of temperature on the
Fahrenheit scale;
• 2. calculate the corresponding temperature on
the Celsius scale;
• 3. WRITE the value just found;
• 4. calculate the corresponding temperature in
degrees Kelvin;
• 5. WRITE this value.
How to Write a Computer Program
• To program this problem one might use the following
code in a file called TempFtoC.f95:
• PROGRAM Temp_Conversion
• ! Convert a temperature value from Fahrenheit to
Celsius
• IMPLICIT NONE
• REAL :: Deg_F, Deg_C, Deg_K ! 3 real type variables
• ! Obtain a temperature value
• WRITE(unit=6,fmt=”(A28)”,advance=”no”) &
• "Please type in the temp in F: "
How to Write a Computer Program
• READ*, Deg_F
• ! Convert from Fahrenheit to Celsius
• Deg_C = 5.0*(Deg_F-32.0)/9.0
• ! Output this new value
• WRITE(unit=6,fmt=”(A17,F6.1,A2)”) &
• "This is equal to ", Deg_C, " C"
• ! Convert to Kelvin and output
• Deg_K = Deg_C + 273.0
• WRITE(unit=6,fmt=”(A4,F6.1,A2)”) "and ", Deg_K, " K"
• END PROGRAM Temp_Conversion
How to Write a Computer Program
• The form of the program source is essentially
free with:
•  up to 132 characters per line;
•  significant blanks;
•  `!' comment initiator;
•  `&' line continuation character;
•  `;' statement separator.

You might also like