Presentation is loading. Please wait.

# FORTRAN PROGRAMMING BASICS (2) MET 50. Programming Basics A few extra things from last week… 1. For a program written in Fortran 90, use the extension.

## Presentation on theme: "FORTRAN PROGRAMMING BASICS (2) MET 50. Programming Basics A few extra things from last week… 1. For a program written in Fortran 90, use the extension."— Presentation transcript:

FORTRAN PROGRAMMING BASICS (2) MET 50

Programming Basics A few extra things from last week… 1. For a program written in Fortran 90, use the extension “.f90”  This tells the compiler: “Hey – this is a Fortran 90 code”.  *.f might be interpreted at Fortran 77. 9/8/2011 2 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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. 9/8/2011 3 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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 9/8/2011 4 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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 9/8/2011 5 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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.) 9/8/2011 6 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 4. Order of operations in Fortran: a. Stuff inside parentheses is done first. b. Inside parentheses, the order is: c. Exponentiation (A**2) – right  left  A**2**3 is computed as  A**(2**3)=A**8 d. Multiplication & division: right  left e. Addition & subtraction: right  left 9/8/2011 7 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics Example: X = SQRT(B**2 – 4.0*A*C) 9/8/2011 8 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics Page 25, Q 10. ((2 + 3)**2) / (8 - (2 + 1)) =((5)**2) / (8 - (3)) = (5**2) / (5) = (25) / (5) = 5 9/8/2011 9 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics But…caution about parentheses… ((2 + 3)**2) / (8 – (2 + 1)) = (5**2) / (7) = (25) / (7) 55 Parentheses matter!!! 9/8/2011 10 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics And… ((2 + 3)**2) / (8 – (2 + 1) Will not run! Why?? Parentheses really matter!!! 9/8/2011 11 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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”? 9/8/2011 12 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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 4.500000 value of TAR is 7.800000 9/8/2011 13 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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. 9/8/2011 14 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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  9/8/2011 15 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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 9/8/2011 16 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics This is a style thing! There is a more powerful READ statement – Cht. 5 9/8/2011 17 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 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 9/8/2011 18 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 5. Next lecture? “selective execution” Fortran equivalent to: “IF it’s Sunday, sleep in. ELSE, set alarm.” 9/8/2011 19 MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 6. Next lab? Practice finding problems with REAL and INTEGER numbers mixed. READ* and PRINT* statements 9/8/2011 20 MET 50, FALL 2011, CHAPTER 2 PART 2

Download ppt "FORTRAN PROGRAMMING BASICS (2) MET 50. Programming Basics A few extra things from last week… 1. For a program written in Fortran 90, use the extension."

Similar presentations

Ads by Google