- 49 Views
- Uploaded on
- Presentation posted in: General

Chapter 2

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.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Chapter 2

BasicFortran

- 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.

- Main text book of the class:
Programming in F

T.M.R. ELLIS, Ivor R. PHILLIPS, Addison-Wesley, 1998

- Auxiliary text book:
Essential Fortran

Loren P. Meissner, PWS Publishing, 1997

- There are five basic data types in fortran
- 1) INTEGER
- 2) REAL
- 3) COMPLEX
- 4) CHARACTER
- 5) LOGICAL

Numerical-data types

Strings of characters

Non-numerical

data types

Logical data values

- An Integer is a whole number (positive, negative, or zero) and does not contain commas or a decimal point.
Examples for valid integer numbers are:

1

137

-1126

+17735

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.)

- A real constant must contain a decimal point, but no commas are allowed. Valid real constants are:
1.623

-0.03275

+55765.

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

- A complex number is represented as a pair of real (i.e., floating point) numbers. The first component of the pair represents the real part of the complex data object, and the second represents the imaginary part. For example;
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.

- Character constants, also called strings, are sequences of symbols from the ANSI standard character set for Fortran.
- The number of a character constant between double quotes is the length of the constant
For example: ”PDQ123-A” (Character constant of length 8)

”John Q. Doe” (Character constant of length 11)

- An object of logical type has the value true or false,
or (0 or 1), or (yes or no).

OperatorMeaning

+Addition

-Substraction

*Multiplication

/Division

**Exponentiation (or ‘rising the power of’)

OperatorPriority

**High

* and /Medium

+ and -Low

Examples:

W=c/d*b

Total=2**3+5*2=18

W=x+z-y

- How to download and install the F_W95.EXE ?
1) Web page of the Class

2) Network neighborhood --> Bim -> Ntserver -> F

3) ftp:[email protected]

password is install

READING

Study pages 38-49 (Ellis & Philips’s book)

- A data object is a constant that never changes or a variable that can change during program execution.
- Data object may have names. For example, Average, X, Y, Einstein, or Potential_Energy.
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

- Every variable and named constant must appear in a type declaration
- The type of a Fortran variable determines the type of value that may be assigned to that variable.
- In every F program, the specification statement implicit none must immediately follow the program statement
program Research

implicit none

.

.

.

end program Research

Type Declaration Form

Type name ::List of names

Consider the following example

implicit none

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.

- Each type has a default computer representation
- For each numerical data type, F defines a set of integers to be used as kind type parameter values (i.e., the number 4 for real representation, number 8 for the higher-precision variant)
Length : A variable of character data type has a string length property.

- A character type declaration must specify string length
A type declaration appears in parentheses after the type name. If no

kind parameter is specified, F selects the default computer representa-

tion

Type name (Type properties) :: List of names

- Other data attributes may be specified between the type properties and the double colon.
Type name (Type properties), Attributes :: List of names

Example:

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 may have an explicit form, or it may be represented by a name.
EXPLICIT CONSTANTS

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.

- The name of a constant looks like the name of a variable and it must be listed in the type declaration
- The keyword parameter designates a named constant
- Houdini Principle: Don’t use magic numbers
- use a named constant rather than a explicit constant
- give always explanations ( use !)

- Declaration of a named constant is as follows:
Type name, parameter :: List of initializations

where each list item has the form

Name = Value definition

The value definition is an explicit constant.

Examples:

integer, parameter :: LENGTH=12

real, parameter :: PLANK=6.6260755e-34, PI=3.141593

real, parameter :: GRAVITY=9.807, AVAGADRO=6.0221367e23, &

twoPI=2.0*PI

integer, parameter :: A=20, HIGH=30, NEON=67

character (Len=2), parameter :: units=”Cm”

ATTENTION: Continuation line with ampersand symbol.

Exercise2:

- Do exercises 1.3 on pages 46-47 (Meissner’s book).
- Study pages between 49-65 (Ellis & Philips’s book).

Read (unit = *, fmt = *) Input List

Write (unit = *, fmt = *) Output List

- An asterisk as the unit in a read or write control list designates the default input device (the keyboard) or the default output device (The terminal screen)
- An asterisk as the format designates list-directed formatting. Input data values for on-line list-directed input are entered at the computer keyboard in free form. Consecutive values must be separated by blanks.
For example:

read (unit = *, fmt = *) Radii, I, Current, Top

can be entered as

9.75 10 15.32

765.3

- IMPORTANT: The items in an input list must be variable names.
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

- There is an increasing demand for high precision
- Computer hardware representation varies from 8-bit to 128 bit word length
- Precision requirements should be considered for both different type of computer hardware and problem type we have.
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.

- A Kind option provides flexible control of integer and real precision and range.
- 1) Each data type has a default computer representation
- 2) The kind type parameter of a data object determines its computer representation

- 3) Each kind is designated by a different integer kind selector value.
- 4) A kind value may be specified in the type declaration for a variable or named constant.

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:

678_SHORT

12_LONG

3.141592_HIGH

The kind selector must be a named constant of integer type

EXAMPLES:

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

Exercise3:

Do the example 1.4 on page 58 to see the difference in kind selection Meissner’s book.

- Assume that,
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

- show programlist_directed_inputexample !
- Compile
- Run

program list_directed_input

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

- Use a top-down approach when developing a program for a complex problem.
- Strive for simplicity and clarity
3) Each program unit should be documented

- Include opening documentation
- Use comments
- Use meaningful identifiers
- Label all output
4) A program should be formatted in a style that enhances its readability

5) Programs should be readable and understandable

- Do not use magic numbers
- Use comments to describe the purpose of a program and variables
6) Programs should be general and flexible