Transcript MET 50
MET 50
FORTRAN PROGRAMMING
BASICS (2)
Programming Basics
2
A few extra things from last week…
For a program written in Fortran 90, use the
extension “.f90”
1.
This tells the compiler: “Hey – this is a Fortran 90 code”.
*.f might be interpreted at Fortran 77.
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
3
2. When writing a number like: 6.7371E6, always
write this as:
(real number) E (integer)
So 6.371E6 is OK
But 6.371E1.5 is not OK.
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
4
3. Real versus Integer: it is dangerous to mix real and
integer variables in Fortran
Example:
REAL :: A=10.0, B=4.0, C
INTEGER :: K=4, L
C = A/B ! Produces C = 10./4. = 2.5
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
5
REAL :: A=10.0, B=4.0, C, D
INTEGER :: K=10, L=4, KL
C = A/B
! Produces C = 10./4. = 2.5
KL = K/L ! Produces KL = 10/4 = 2
! Rounded down to nearest integer
D = A/L
! A/L = 10.0/4 = 2.5
! A/L is treated as REAL
! D is REAL and has value 2.5
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
6
Example:
REAL :: A=2.0, B=4.0, C
INTEGER :: K=4, L
C = A**(3/2)
! 3/2 = 1 (INTEGER!)
! C = A**1 = A = 2.0
! but (2)**(3/2) = SQRT(2**3) = 8
So…you get an error…
Should be: C=A**(3./2.)
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
7
4. Order of operations in Fortran:
a. Stuff inside parentheses is done first.
b. Inside parentheses, the order is:
Exponentiation (A**2) – right left
c.
A**2**3 is computed as
A**(2**3)=A**8
d. Multiplication & division: right left
e.
Addition & subtraction: right left
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
8
Example:
X = SQRT(B**2 – 4.0*A*C)
X = SQRT(B**2 – 4.0*A*C)
X = SQRT(B**2 – 4.0*A*C)
X = SQRT(B**2 – 4.0*A*C)
X = SQRT(B**2 – 4.0*A*C)
X = SQRT(B**2 – 4.0*A*C)
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
9
Page 25, Q 10.
((2 + 3)**2) / (8 - (2 + 1))
=((5)**2) / (8 - (3))
= (5**2) / (5)
= (25) / (5)
=5
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
10
But…caution about parentheses…
((2 + 3)**2) / (8 – (2 + 1))
= (5**2) / (7)
= (25) / (7)
5
Parentheses matter!!!
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
11
And…
((2 + 3)**2) / (8 – (2 + 1)
Will not run!
Why??
Parentheses really matter!!!
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
12
4. PRINT*, READ* statements:
We have met the PRINT* statement.
a. PRINT*, VAR
prints the value of “VAR”
b. PRINT*, VAR, TAR prints the values of “VAR”
and “TAR”
c. Quantities are printed to the screen
(only)…”hardcopy”?
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
13
Results may be printed “ugly”, such as:
4.500000
7.800000
We can make things a bit nicer, as in:
PRINT*, ‘value of VAR is’, VAR, ‘value of TAR is’, TAR
Would value of VAR is
MET 50, FALL 2011, CHAPTER 2 PART 2
4.500000
value of TAR is 7.800000
9/8/2011
Programming Basics
14
We can make things better still using the WRITE
command (Chapter 5)(or sooner!)
READ* statement is the simplest way to input data to a
program.
In lab-02, you ran “add2.f” to add 2 numbers.
The code prompted you for two numbers.
How?
Using the READ* statement.
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
15
Example:
REAL :: A, B, C
READ*, A, B
C=A*B
PRINT*, A, B, C
As this code runs, it will stop – waiting for YOU to enter
values of A and B at the “READ” command.
Codes in this class will run FAST (since they are very
small), so if the code stops, it is either expecting input –
or it’s broken
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
16
To help you see what is going on in your code, it is
good practice to add some PRINT statements:
PRINT*, ‘enter values for A and B’
READ*, A, B
Or:
PRINT*, ‘enter first number’
READ*, A
PRINT*, ‘enter second number’
READ*, A
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
17
This is a style thing!
There is a more powerful READ statement – Cht. 5
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
18
5. Comment statements:
Comment statements are vital!
Use to explain what this section of code does.
! State @ top of code what the program does!
! Read in parameter values
! Main computation
! Write results
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
19
5. Next lecture?
“selective execution”
Fortran equivalent to:
“IF it’s Sunday, sleep in. ELSE, set alarm.”
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011
Programming Basics
20
6. Next lab?
Practice finding problems with REAL and INTEGER
numbers mixed.
READ* and PRINT* statements
MET 50, FALL 2011, CHAPTER 2 PART 2
9/8/2011