(4) Selection In Fortran (Sina)

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

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

View Full Document Right Arrow Icon
Selection in Fortran Sina meraji Fall, 2009
Image of page 1

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

View Full Document Right Arrow Icon
Today ! Review ! READ statement ! IMPLICIT NONE ! RELATIONAL operators ! Selection (IF-THEN)
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
Review INTEGER :: X X = 1/2 ->X = 0 REAL :: X X = 1/2 -> X = ?? Truncation: 1/2 ! 0.5
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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
Image of page 9

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

View Full Document Right Arrow Icon
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!
Image of page 10
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)
Image of page 11

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

View Full Document Right Arrow Icon
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
Image of page 12
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
Image of page 13

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

View Full Document Right Arrow Icon
PROGRAM RootFinding
Image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern