1 / 20

MET 50

MET 50. Fortran programming basics (2). Programming Basics. A few extra things from last week… 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. Programming Basics.

denali
Download Presentation

MET 50

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MET 50 Fortran programming basics (2)

  2. Programming Basics A few extra things from last week… • 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. MET 50, FALL 2011, CHAPTER 2 PART 2

  3. Programming Basics • 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

  4. Programming Basics 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

  5. 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 MET 50, FALL 2011, CHAPTER 2 PART 2

  6. 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.) MET 50, FALL 2011, CHAPTER 2 PART 2

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

  8. Programming Basics 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. Programming Basics 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

  10. Programming Basics 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

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

  12. Programming Basics PRINT*, READ* statements: We have met the PRINT* statement. PRINT*, VAR prints the value of “VAR” PRINT*, VAR, TAR prints the values of “VAR” and “TAR” Quantities are printed to the screen (only)…”hardcopy”? MET 50, FALL 2011, CHAPTER 2 PART 2

  13. 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 MET 50, FALL 2011, CHAPTER 2 PART 2

  14. 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. MET 50, FALL 2011, CHAPTER 2 PART 2

  15. 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  MET 50, FALL 2011, CHAPTER 2 PART 2

  16. 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 MET 50, FALL 2011, CHAPTER 2 PART 2

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

  18. Programming Basics 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

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

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

More Related