Chapter 2. Basic Fortran. Attendance Requirements. Attendance is required for both class and lab. hours Minimum %70 for class attendance Minimum %80 for lab. Attendance This is one and only requirement for taking the final exam. Text Books. Main text book of the class: Programming in F
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Programming in F
T.M.R. ELLIS, Ivor R. PHILLIPS, Addison-Wesley, 1998
Loren P. Meissner, PWS Publishing, 1997
Strings of characters
Logical data values
Examples for valid integer numbers are:
the followings are the examples for invalid integer number:
8,675 (Commas are not allowed in numerical constants)
26.0 (Integer constants may not contain decimal points)
--5 (Only one algebraic sign is allowed)
7- (The algebraic sign must precede the string of digits.)
invalid real constants
15,627 (commas are not allowed in numerical constants.)
786 (Real constants must contain a decimal point.)
a real constant can be represented by an exponential. An exponent written as the letter E with an integer constant following. For example: 6527.4684 may also be written as
6.5274684E3 which means 6.5274684X103
or similarly; 65.274684E2
0.65274684E4 and 65274.684E-1
Z = a + i b Z = (a,b)
NOTE: Since you don’t have enough background on complex numbers, you are not fully responsible from complex numbers and its related examples. But, you should now as much as taught.
For example: ”PDQ123-A” (Character constant of length 8)
”John Q. Doe” (Character constant of length 11)
or (0 or 1), or (yes or no).
**Exponentiation (or ‘rising the power of’)
* and /Medium
+ and -Low
1) Web page of the Class
2) Network neighborhood --> Bim -> Ntserver -> F
3) ftp:[email protected]
password is install
Study pages 38-49 (Ellis & Philips’s book)
Names in a program must conform to 3 rules:
1) A name may contain up to 31 letters, digits, and underscore characters
2) The first character of a name must be a letter
3) Imbedded blank characters are not permitted in a name
IMPORTANT: keywords such as program, write, and end are not actually names
end program Research
Type Declaration Form
Type name ::List of names
Consider the following example
integer :: Counts, Loop_Index
real :: Current, Resistance, Voltage
Names defined as part of the F language, including keywords and intrinsic function names (such as sin, tan, abs, etc.), must be written in lower case. Names that you invent can use any combination of upper and lower case, but each name must be written consistently.
Kind : A variable of any numerical type has a kind type parameter, which designates a subtype or variant of the type.
Length : A variable of character data type has a string length property.
A type declaration appears in parentheses after the type name. If no
kind parameter is specified, F selects the default computer representa-
Type name (Type properties) :: List of names
Type name (Type properties), Attributes :: List of names
integer, parameter :: ARRAY_SIZE=12, SL=20
character (Len=SL), save :: Element_Name
integer, dimension (ARRAY_SIZE) :: chart, list
Exercise 1:Do exercises 1.2 on page 42 (Meissner’s book)!.
A constant in a program has a fixed value during the execution of the program. Consider the cases that:
1) A value may need to be changed before the program is executed again.
2) A constant in a declaration, such as the size of an array or the length of a character string, may need to be revised.
Therefore we prefer to use a named constant instead of an explicit constant to prevent searching for all the appearances of a certain constant within the program which is a tedious and an error-prone task.
Type name, parameter :: List of initializations
where each list item has the form
Name = Value definition
The value definition is an explicit constant.
integer, parameter :: LENGTH=12
real, parameter :: PLANK=6.6260755e-34, PI=3.141593
real, parameter :: GRAVITY=9.807, AVAGADRO=6.0221367e23, &
integer, parameter :: A=20, HIGH=30, NEON=67
character (Len=2), parameter :: units=”Cm”
ATTENTION: Continuation line with ampersand symbol.
Read (unit = *, fmt = *) Input List
Write (unit = *, fmt = *) Output List
read (unit = *, fmt = *) Radii, I, Current, Top
can be entered as
9.75 10 15.32
write (unit = *, fmt = *) ” Please enter the diameter of a circle”
read (unit = *, fmt = *) Diameter
write (unit = *, fmt = *) ” Diameter = ”, Diameter, ”circumference =”, &
3.1416*Diameter, ”Area = ”, 3.1416*Diameter**2
NUMERICAL PRESICION AND RANGE
For example: 9 decimal digits of numerical precision can be satisfied by 1) Single-precision arithmetic if the computer word length is 64 bits, 2) double-precision arithmetic if the word length is 32 bits.
The intrinsic inquiry function selected_real_kind(P) returns the kind value for a processor representation that supplies at least P decimal digits of precision.
Example: selected_real_kind(6) returns a processor kind value that provides at least 6 decimal digits of precision
A numerical constant may be followed by an underscore and a kind selector:
The kind selector must be a named constant of integer type
integer, parameter :: NORMAL = selected_int_kind(9), &
LOW = selected_real_kind(6), HIGH = selected_real_kind(15)
integer (kind = NORMAL) :: First, Second
real (kind = LOW), parameter :: FOURTH = 12.0
Do the example 1.4 on page 58 to see the difference in kind selection Meissner’s book.
b is a real variable whose value is 100.0, while c and d are integers having the values 9 and 10, respectively.
a = b*c/d
result is 90.0
a = c/d*b
a gets 0 value.
This phenomenon is known as integer division
integer :: int_1, int_2, int_3
real :: real_1, real_2, real_3
! Initialize all variables
int_1 = -1
int_2 = -2
int_3 = -3
real_1 = -1.0
real_2 = -2.0
real_3 = -3.0
! Read data
read *, int_1,real_1,int_2,real_2,int_3,real_3
! Print new values
print *, int_1,real_1,int_2,real_2,int_3,real_3
end program list_directed_input
A program must be correct, readable, and understandable. The basic principles for developing a good program are as follows:
1) Programs cannot be considered correct until they have been validated using test data.
2) Programs should be well structured
3) Each program unit should be documented
4) A program should be formatted in a style that enhances its readability
5) Programs should be readable and understandable
6) Programs should be general and flexible