(4) Selection In Fortran (Sina)

# (4) Selection In Fortran (Sina) - Selection in Fortran Sina...

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

Selection in Fortran Sina meraji Fall, 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: 101101 11 001011 11 111001 11 1 01110 10 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 4 3 2 1 +, - *, / ^ () operators Associativity Left to right Left to right Right to left 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”? ! In Fortran, you can use variables without declaring them ! All the variables started with I, J, K, L, M and N are Integer ! All the others Real

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

View Full Document
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!
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)

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

View Full Document
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
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 preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
PROGRAM RootFinding
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern