Download presentation

Presentation is loading. Please wait.

Published byDania Tallent Modified over 2 years ago

1
FORTRAN PROGRAMMING BASICS (2) MET 50

2
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

3
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

4
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

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

7
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

8
Programming Basics Example: X = SQRT(B**2 – 4.0*A*C) 9/8/2011 8 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 9/8/2011 9 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) 55 Parentheses matter!!! 9/8/2011 10 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!!! 9/8/2011 11 MET 50, FALL 2011, CHAPTER 2 PART 2

12
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

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 9/8/2011 13 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. 9/8/2011 14 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 9/8/2011 15 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 9/8/2011 16 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 9/8/2011 17 MET 50, FALL 2011, CHAPTER 2 PART 2

18
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

19
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

20
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

Similar presentations

OK

CSCE 206 1 Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.

CSCE 206 1 Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on class 9 motion powerpoint Ppt on chapter 12 electricity rates Ppt on advertising and brand management Ppt on javascript events textbox Ppt on mammals and egg laying animals name Ppt on earth damage Ppt on aggregate production planning Ppt on nitrogen cycle and nitrogen fixation meaning Ppt on event handling in javascript Ppt on abstract art tattoo