{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04Choices-handout

# 04Choices-handout - Selection in Fortran Amir Hossein...

This preview shows pages 1–13. Sign up to view the full content.

Selection in Fortran Amir Hossein Rabbani Winter, 2009

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Today Review READ statement IMPLICIT NONE RELATIONAL operators Selection (IF-THEN)
Review Variable Data Types: INTEGER •REAL •LOGICAL COMPLEX •CHARACTER Variable precision: This is for INTEGER, to know how to use KIND parameter for other data types refer to the sources introduced in the class 1 0111010 11100111 00101111 10110111 INTEGER Default size = 4 bytes (KIND=4) Sign bit 1: negative 0: positive

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Review INTEGER :: X X = 1/2 ->X = 0 REAL :: X X = 1/2 -> X = ?? Truncation: 1/2 0.5
Review INTEGER :: X X = 1/2 ->X = 0 REAL :: X X = 1/2 -> STILL X = 0 !! QuickTimeª and a decompressor are needed to see this picture. Truncation: 1/2 0.5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Review INTEGER :: X X = 1/2 ->X = 0 REAL :: X X = 1/2 -> STILL X = 0 !! REAL :: X X = 1.0 / 2.0 -> X = 0.5 Truncation: 1/2 0.5
Review operators Precedence () 1 ** 2 *, / 3 +, - 4 operators Associativity () Left to right ** Right to left *, / Left to right +, - Left to right 100 + (1 + 250 / 100) ** 3 = 127

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The READ Statement Syntax:   READ (*,*) var 1 , var 2 , . . ., var n Example READ (*,*) Celsius !From Elmer Fudd program Semantics: Start a new line that contains the input Input values must be the same type as the corresponding variables (e.g. you can't put a real number into an integer) Data must be separated by commas or blanks Extra input values on that line are ignored
What is this “IMPLICIT-NONE”? Writing Implicit-None at the beginning of the program tells the computer to point out errors if you try to use a variable that doesn't exist. For example, if I write “Celcius” (instead of “Celsius”) by accident, it will give me an error when I compile If we write “Celcius” instead of “Celsius” most likely we made a mistake that will matter later on in our program. While it is good that the program compiles, the program will not work correctly and we'll have to find the mistake by hand!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Another simple example: Roots finding Finding roots for quadratic aX 2 + bX + c = 0 Input a, b, c Output root(s): ( -b + SQRT(b*b - 4*a*c) ) / (2*a) ( -b - SQRT(b*b - 4*a*c) ) / (2*a)
Roots finding (cont.) start Initialize a=1, b=-5, c=6 x = (b*b – 4*a*c) R1 = (-b + sqrt(x)) / (2*a) R2 = (-b - sqrt(x)) / (2*a) End

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
PROGRAM RootFinding IMPLICIT NONE INTEGER a, b, c REAL x, y, z, w, r1, r2 a = 1 b = 5 c = 8 !To calculate b * b - 4 * a *c x = b*b y = a*c z = 4*y w = x - z r1 = (-b + SQRT(w) / (2*a)) r2 = (-b - SQRT(w) / (2*a)) WRITE (*,*) r1, r2 END PROGRAM RootFinding Declare variables, Must be at the head of block initialize variables with values statements: calculation statements:Output results
This is the end of the preview. Sign up to access the rest of the document.
• Spring '04
• Vybihal
• Quadratic equation, Complex number, Amir Hossein Rabbani Winter, PROGRAM RootFinding, END PROGRAM RootFinding

{[ snackBarMessage ]}