Title: 2' BASIC FORTRAN
12. BASIC FORTRAN
- ? VARIABLES CONSTANTS
- ? DATA TYPES
- ? OPERATIONS
- ? ASSIGNMENT
- ? I/O
- ? PROGRAMMING STYLE
2VARIABLES CONSTANTS
- Variables are locations in the computers memory
in which variable information may be stored. - Constants are locations in which information is
stored which cannot be altered during the
execution of the program.
3DATA TYPES
- Numerical Data Types
- INTEGER
- REAL
- COMPLEX
- Non numerical Data Types
- CHARACTER
- LOGICAL
4Integer Numbers
- An integer is a whole number (positive, negative,
or zero) and does not contain commas or a decimal
point. - Examples 5 1024 -512
- a5 b1024 c-512
- Integer variable definition in F
- integer a,b,c
5Real Numbers
- A real constant must contain a decimal point, but
no commas are allowed. - Examples 5.125
- -0.0273
- 0.0571
- 6.5274684E3 (6.5274684X103)
- x5.125 y-0.0273 z0.0571
- Real variable definition in F
- real x,y,z
6Complex Numbers
- 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. - Example
- Z a i b Z (a,b)
- -34i
- complex z
7Character constants (strings)
- F character set
- A B C D E F G H I J K L M N O P Q R S T U V W X Y
Z - a b c d e f g h i j k l m n o p q r s t u w x y z
- 0 1 2 3 4 5 6 7 8 9
- ? - / ( ) , . lt gt ! ?
- where ? represents the space or blank character
8Character strings
- Character constants, also called strings, are
sequences of - symbols from the ANSI standard character set for
Fortran. - The number of character constants between double
quotes is the length of constant - For example
- abcD-12o (the length is 8)
9Names Declarations
- character (len3) schr
- schrend
- The character data does not contain any blanks,
- commas and slashes. A name may contain up
to - 31 letters, digits, and underscore characters.
- 2. The character data is within the single record
or line. - 3. The first non-blank character is not a
quotation mark. - 4. The leading characters are not numeric
followed by an asterisk since this would be
confused with the multiple data item form (nc). - IMPORTANT keywords such as program, write, and
end are not actually names
10Ellis and Philips, 1998, p59
- program character_example
- character (len3) string_1
- character (len4) string_2, string_3
- string_1End
- string_2string_1
- string_3Final
- print ,string_1,string_2,string_3
- end program character_example
- EndEnd Fina
11Ellis and Philips, 1998, p51
- program list_directed_input_example
- 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_example
12- Input Output
- 1,2.3,4,5.6,7,8.9 1 2.300 4 5.600 7 8.900
- 9 8.7 6 5.4 3 2.1 9 8.700 6 5.400 3 2.100
- 1 2.3 1 2.300 4 5.600 7 8.900
- 4 5.6
- 7 8.9
- 9,,,5.4,,2.1 9 -1.000 -2 5.400 -3 2.100
- 1,2.3,4,5.6/ 1 2.300 4 5.600 -3 -3.000
- / -1 -1.000 -2 -2.000 -3 -3.000
13NAMED CONSTANTS
- real, parameter pi3.1415926, pi_by_twopi/2.0
- integer, parameter max_cases100
14Logical type An object of logical type has the
value true or false, or (0 or 1), or (yes or no)
Example Logical Circuits (Nyhoff and Leestma,
1992, p 108-109)
15- A and B are inputs.
- SUM and CARRY are outputs.
- AND gate An output pulse is produced only if
there are pulses on both input lines. - OR gate An output pulse is produced only if
there is an input pulse on at least one of the
input lines. - NOT gate An output pulse is produced only when
there is no incoming pulse.
16- False(F)0 (absence of pulse)
- True(T)1 (presence of pulse)
- SUM (A .OR. B) .AND. .NOT. (A .AND. B)
- CARRY A .AND. B
- A B CARRY SUM A B CARRY SUM
- 1 1 1 0 T T T F
- 1 0 0 1 T F F T
- 0 1 0 1 F T F T
- 0 0 0 0 F F F F
17ARITHMETIC OPERATORS IN F
- Operator Meaning
- addition
- - substraction
- multiplication
- / division
- exponentiation
-
18Arithmetic operator priorities
- Operator Priority
- High
- and / Medium
- and - Low
19- Example
- abcd/e-fg/hijk
- Steps
- temp_1fg
- temp_2cd
- temp_3temp_2/e
- temp_4temp_1/h
- temp_5ij
- temp_6btemp_3
- 7. temp_7temp_4temp_6
- temp_8temp_5temp_7
- 9. atemp_8k
20- Evaluation of the expression proceeds from left
to right, within the priority level, except for
exponentiation which is carried out from right to
left, but may be altered by the use of
parentheses. - If one of the operands of an arithmetic operator
is real, then the evaluation of that operation is
carried out using real arithmetic, with any
integer operand being converted to real. - If an integer value is assigned to a real
variable it is converted to its real equivalent
before assignment if real value is assigned to
an integer variable it is truncated before
conversion to integer, and any fractional part is
lost.
21MIXED MODE ASSIGNMENT
- 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 (bc)/d
- The result is 90.0
- a (c/d)b
- a gets 0 value.
- This phenomenon is known as integer division
22SIMPLE INPUT OUTPUT
- Read (unit , fmt ) Input List
- Write (unit , fmt ) Output List
- An asterisk as the unit in a read or write
controls 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. Values must
be separated by - blanks.
- For example
- read (unit , fmt ) xval,yval,zval
- can be entered as
- 10.0 100.0 2.5
23Programming style
- Writing elegant programs requires
- ? Careful initial design
- ? Maintainability
- ? Portability (i.e. it can be compiled and
executed on the other machines)
24Steps
- Goal of the program
- Explanations of inputs and outputs
- Clear design for the method
- Check for existing procedures libraries
- Use modular design (single block of code lt 50
lines) - Use descriptive names for variables and program
units - Perform as much as error checking on the input as
is possible including for the different cases of
the input variables - Test the program by executing it
25A main program unit
- program name
- use statements
- .
- specification statements
- .
- executable statements
- .
- end program name
-
26A module program unit
- module name
- use statements
- .
- .
- specification statements
- end module name
27A module program unit containing procedure
definitions
- module name
- use statements
- .
- specification statements
- .
- contains
- procedure definitions
- .
- .
- end module name
28IN-CLASS PROBLEM SESSION -2
- Objective
- Learning Basic Fortran (data types, variables
- operations, etc.) Numerical methods
(interpolation) - Assignment
- In the following figure, moisture content of core
samples (grams - water/100g dried soils) is plotted as a function
of depth (ft) given in the - following Table. Write a program to calculate
the moisture content at - depth of 7.5 ft by using the following formula.
- Depth (ft) Moisture (grams water/100g dried
solids) - 0 124
- 5 78
- 10 54
- 15 35
- 20 30
- 25 21
- 30 22
- 35 18
-
29(No Transcript)
30HOMEWORK
- Objective
- Learning Basic Fortran (data types, variables,
operations, - etc) Numerical methods (weighted average)
- Assignment
- Write a program to calculate a weighted average
value of - the shear wave velocities of three layered earth
model and - dominant resonance period of this model.
- The following formulas can be used
- V(h1v1h2v2h3v3)/(h1h2h3)
- T(4(h1h2h3))/V
- v1, v2, v3 shear wave velocities of layers
- h1, h2, h3 thicknesses of layers
- V weighted average value of shear wave
velocities - T resonance period
- v1100.0 m/s, v2200.0 m/s, v3300.0 m/s,
- h110.0 m, h220.0 m, h330.0 m