Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example in Final Fall 2006 PROGRAM EXAM IMPLICIT NONE INTEGER :: A=3, B=8 REAL :: CALC B = CALC(A, B) A = CALC(A, B) WRITE(*,*) A, B END PROGRAM EXAM a)

Similar presentations


Presentation on theme: "Example in Final Fall 2006 PROGRAM EXAM IMPLICIT NONE INTEGER :: A=3, B=8 REAL :: CALC B = CALC(A, B) A = CALC(A, B) WRITE(*,*) A, B END PROGRAM EXAM a)"— Presentation transcript:

1 Example in Final Fall 2006 PROGRAM EXAM IMPLICIT NONE INTEGER :: A=3, B=8 REAL :: CALC B = CALC(A, B) A = CALC(A, B) WRITE(*,*) A, B END PROGRAM EXAM a) 00 b) 30 c) 03 d) 08 e) None of the above REAL FUNCTION CALC(X, Y) IMPLICIT NONE INTEGER :: X, Y IF(X > Y) THEN Y = Y/X CALC = Y ELSE CALC = Y/X END IF END FUNCTION CALC

2 Example in Midterm Fall 2006 PROGRAM midterm IMPLICIT NONE INTEGER :: a, b a = 15 b = 5 CALL mysub(a, b) WRITE(*,*) b-a END PROGRAM a). -6532 b). -150 c). 1 d). 2356 e). None of the above SUBROUTINE mysub(b,a) IMPLICIT NONE INTEGER a, b, i, j IF(a > b) THEN a = (a - 10)**2 b = b*b ELSE a = a*b b = b*b END IF if(a == b) THEN DO i=1, 15 DO j=i, 25-i a = a + i*j b = b - a END DO IF(a /= b) THEN a = 2 b = 3 END IF END SUBROUTINE

3 Formatting input and output Yi Lin

4 Why formatting? PROGRAM test IMPLICIT NONE REAL::x=1.1, y=1100.1003 WRITE(*,*) x WRITE(*,*) y END PROGRAM Output: 1.100000 1100.100 Nicer format: 1.1000 1100.1003

5 FORMAT statement Syntax write(*, label) list-of-variables label format format-code Semantics Output list-of-variables according to the format- codes specified on the line labeled.

6 FORMAT statement, example 1 Example REAL::y=1100.1003 write(*, 900) y 900 format (F10.4) F10.4 means that the number y should be printed using fixed point notation with field width 10 and 4 decimal places. #1100.1003 Width=10 (including “.”, white spaces will be filled on the leftmost side) label format-code Decimal digits=4

7 FORMAT statement, example 2 Example REAL::x=1.0, y=1100.1003 write(*, 900) x, y 900 format (F3.1, F10.4) F3.1 is for x and F10.4 is for y correspondingly. 1.0#1100.1003 (#: white space) Width should be larger enough! List-of-variables

8 FORMAT statement, example 4 Example REAL::x=1.0, y=1100.1003 write(*, 900) x, y 900 format (F3.1, F9.4) (F3.1,F9.4):1.01100.1003 (F3.1,F10.4):1.0#1100.1003 (F3.1,F8.4): 1.0******** *: Width=8 is not wide enough to output y. 4 integer digits + 4 decimal digits + 1 for “.” = 9 digits

9 Common format codes E.g., 900 format (F10.4) The most common format code letters are: F - real numbers, fixed point format I - integer A - text string E - real numbers, exponent notation X - horizontal skip (space) / - vertical skip (newline)

10 FORMAT statement, Fw.d The format code F (and similarly D, E) has the general form Fw.d where w is an integer constant denoting the field width and d is an integer constant denoting the number of decimal digits. F10.4 If a number or string does not fill up the entire field width, spaces will be added. Usually the text will be adjusted to the right, but the exact rules vary among the different format codes. w=10 (including “.”) d=4 #1100.1003

11 FORMAT statement, Exponent notation E: Exponent notation REAL::y=1100.1003 WRITE(*,100) y 100FORMAT(E10.2) w=10 (including “.”) d=2 ##0.11E+04

12 FORMAT statement, example 5 For integers only the field width is specified, so the syntax is Iw. Similarly, character strings can be specified as Aw but the field width is often dropped. INTEGER::a=1000 WRITE(*,100) “a=“, a 100FORMAT(A5,I6) WRITE(*,200) “a=“,a 200 FORMAT(A,I4) WRITE(*,300) “a=“,a 300FORMAT(A,I3) ###a=##1000 A5I6 a=*** AI3 a=1000 AI4

13 FORMAT statement, horizontal skip nX: horizontally skip n spaces INTEGER::a=1000 WRITE(*,100) “a=“, a 100FORMAT(A, 4X, I4) If n is omitted, n=1 100 FORMAT(A,X,I4) a=#1000 AI4X a=####1000 AI44X

14 FORMAT statement, vertical skip n/: vertically skip n lines INTEGER::a=1000 WRITE(*,100) “a=“, a 100FORMAT(A, 2/, I4) If n is omitted, n=1 a= # 1000 A I4 2/

15 FORMAT statement, repeating nIw: = repeat Iw n times INEGER::a=1, b=10, c=100 WRITE(*,100) a,b,c 100 FORMAT(3I4) ###1##10#100 I4 And similarly nFw.d

16 FORMAT statement, repeating (cont.) n(format-codes): = repeat format-codes n times FORMAT(3(I4,F10.4)) Is equivalent to FORMAT(I4,F10.4,I4,F10.4,I4,F10.4)

17 FORMAT statement, Simplified form Format strings in read/write statements write (*,”(A, F8.3)”) “The answer is x = “, x is equivalent to write (*,990) “The answer is x = “, x 990 format (A, F8.3) Sometimes text strings are given in the format statements, e.g. the following version is also equivalent: write (*,999) x 999 format (“The answer is x = “, F8.3)

18 FORMAT statement, READ All of these format-codes are also applied to READ statement. INTEGER::a,b READ(*,*) a,b From the keyboard, we just need to type 1,2 (or 1#2) ! a=1, b=2 But with formatting input, we must be careful about the number of spaces input.

19 FORMAT statement, READ Example INTEGER::a,b READ(*,100) a,b 100 FORMAT(2I3) ! eqv. To FORMAT(I3,I3) If inputting “1,2”, the computer will take the first 3 characters “1,2” and assign them to a.  Runtime error! “1,2” not an integer If input “##1, 2”, a=##1. But “,2###” will be assigned to b.  Runtime error!

20 FORMAT statement, READ Correct inputs for (2I3), e.g., “##1##2”a=##1, b=##2 “1##2##”a=1##, b=2## “#1##2#”a=#1#, b=#2# What if “#1#22222”? a=#1#, b=222

21 FORMAT statement, READ Correct inputs for READ(*, “(F5.1)”) x? “##3.4”  x=3.4 “123.456”  x=123.4 “12345”  x=1234.5 (take the leftmost 5 digits first, then assign the last digit as decimal part and the leftmost 4 digits as integer part)

22 Format Read Example INTEGER::a, b,c READ(*,100) a,b,c 100 FORMAT(I3,x,I2,2x,I4) Input: 123456789 A=123 B=56 C=9

23 File input/output READ(*,*)/WRITE(*,*) only reads from/writes to standard input(e.g., keyboard)/output(screen). Files: a data storage unit in hard disks. E.g., HelloWorld.f90 E.g., studentRecords.txt E.g., experimentalData.txt We need to read data from existing files and/or write data to files.

24 File input/output Three steps to use a file 1. Open a file 2. Input/output using READ and WRITE – READ: read data from the opened file – WRITE: write data to the opened file 3. Close the file ( A file that has not been closed can usually not be read. )

25 File input/output, OPEN To open a file, syntax: OPEN ([olist] ) where, olist is a list of keyword clauses separated by “,”: keyword "=" value {"," keyword "=" value} Example: OPEN(UNIT=10, FILE=“expData.txt”)

26 File input/output, OPEN Important keywords: [UNIT=] u! u is a unique number to identify the file FILE= fname! File name Some other keywords (not required in course materials) STATUS, ERR, ISOTAT, ACCESS, FORM, RECL, POSITION

27 File input/output, OPEN [UNIT=]u OPEN(10, “expData.txt”) OPEN(UNIT=10, “expData.txt”) Unit number (i.e., u) is an integer from 1-99, with some reserved: 5: standard input READ(*,*) == READ(5,*) 6: standard output WRITE(*,*) == WRITE(6,*)

28 FILE input/output, READ/WRITE READ READ(unit, label) list-of-variables labelformat(format-codes) or READ(unit, *) … WRITE WRITE(unit, label) list-of-variables labelformat(format-codes) or WRITE(unit, *) …

29 FILE input/output, CLOSE A file that has not been closed can usually not be read. Syntax: CLOSE ([UNIT=]u) For example: CLOSE (10) CLOSE (UNIT=10)

30 FILE input/output, Example ! Input 10 integers from keyboard and write them to file “inputData.txt” PROGRAM fileTest IMPLICIT NONE INTEGER::count, a OPEN(UNIT=10,FILE=“inputData.txt”) ! Open file “inputData.txt” DO count=1,10 WRITE(*,*) “Input an integer number from keyboard:” READ(*,*) a WRITE(10,100) “a=“, a ! Write to “inputData.txt” END DO CLOSE(10);! Close file “inputData.txt” 100 FORMAT(A2, I8) END PROGRAM a=######51 a=#######6 … inputData.txt

31 Example in Midterm Fall 2006 program mid implicit none integer :: i character*1 :: a(4) read(*,7) (a(i), i = 1,4) 7 format (4(A1,X)) write(*,8) (a(i), i = 4,1,-1) 8 format (4A1) end program 1. There will be an error message because the input string is too long 2. pmoc 3. iumc 4. cmui 5. None of the above.


Download ppt "Example in Final Fall 2006 PROGRAM EXAM IMPLICIT NONE INTEGER :: A=3, B=8 REAL :: CALC B = CALC(A, B) A = CALC(A, B) WRITE(*,*) A, B END PROGRAM EXAM a)"

Similar presentations


Ads by Google