example in final fall 2006

31
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) 0 0 b) 3 0 c) 0 3 d) 0 8 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

Upload: maj

Post on 11-Jan-2016

41 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Example in Final Fall 2006

Example in Final Fall 2006PROGRAM EXAM

IMPLICIT NONEINTEGER :: A=3, B=8REAL :: CALC

B = CALC(A, B)A = CALC(A, B)WRITE(*,*) A, B

END PROGRAM EXAM

a) 0 0b) 3 0c) 0 3d) 0 8e) None of the above

REAL FUNCTION CALC(X, Y)IMPLICIT NONEINTEGER :: X, Y

IF(X > Y) THENY = Y/XCALC = Y 

ELSECALC = Y/X

END IFEND FUNCTION CALC

Page 2: Example in Final Fall 2006

Example in Midterm Fall 2006PROGRAM midterm

IMPLICIT NONE

INTEGER :: a, ba = 15b = 5CALL mysub(a, b)WRITE(*,*) b-a

END PROGRAM

a). -6532b). -150c). 1d). 2356e). None of the above

SUBROUTINE mysub(b,a)IMPLICIT NONEINTEGER a, b, i, jIF(a > b) THEN

a = (a - 10)**2b = b*b

ELSEa = a*bb = b*b

END IFif(a == b) THEN DO i=1, 15

DO j=i, 25-ia = a + i*jb = b - a

END DO END DO IF(a /= b) THEN

a = 2b = 3

END IFEND IF

END SUBROUTINE

Page 3: Example in Final Fall 2006

Formatting input and output

Yi Lin

Page 4: Example in Final Fall 2006

Why formatting?

PROGRAM testIMPLICIT NONEREAL::x=1.1, y=1100.1003

WRITE(*,*) xWRITE(*,*) y

END PROGRAM

Output:

1.1000001100.100

Nicer format:

1.10001100.1003

Page 5: Example in Final Fall 2006

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.

Page 6: Example in Final Fall 2006

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.1003Width=10 (including “.”, white spaces will be filled on the leftmost side)

labelformat-code

Decimal digits=4

Page 7: Example in Final Fall 2006

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

Page 8: Example in Final Fall 2006

FORMAT statement, example 4

Example REAL::x=1.0, y=1100.1003

write(*, 900) x, y900 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

Page 9: Example in Final Fall 2006

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)

Page 10: Example in Final Fall 2006

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

Page 11: Example in Final Fall 2006

FORMAT statement, Exponent notation

E: Exponent notation

REAL::y=1100.1003

WRITE(*,100) y

100 FORMAT(E10.2)

w=10 (including “.”)

d=2

##0.11E+04

Page 12: Example in Final Fall 2006

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=1000WRITE(*,100) “a=“, a

100 FORMAT(A5,I6)WRITE(*,200) “a=“,a

200 FORMAT(A,I4)WRITE(*,300) “a=“,a

300 FORMAT(A,I3)

###a=##1000

A5 I6

a=***A I3

a=1000

A I4

Page 13: Example in Final Fall 2006

FORMAT statement, horizontal skip

nX: horizontally skip n spacesINTEGER::a=1000

WRITE(*,100) “a=“, a

100 FORMAT(A, 4X, I4) If n is omitted, n=1

100 FORMAT(A,X,I4) a=#1000A I4X

a=####1000A I44X

Page 14: Example in Final Fall 2006

FORMAT statement, vertical skip

n/: vertically skip n linesINTEGER::a=1000

WRITE(*,100) “a=“, a

100 FORMAT(A, 2/, I4) If n is omitted, n=1

a=##1000

A

I4

2/

Page 15: Example in Final Fall 2006

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 I4I4

• And similarly nFw.d

Page 16: Example in Final Fall 2006

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)

Page 17: Example in Final Fall 2006

FORMAT statement, Simplified form

Format strings in read/write statementswrite (*,”(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)

Page 18: Example in Final Fall 2006

FORMAT statement, READ

All of these format-codes are also applied to READ statement.INTEGER::a,bREAD(*,*) a,b

From the keyboard, we just need to type1,2 (or 1#2) ! a=1, b=2

But with formatting input, we must be careful about the number of spaces input.

Page 19: Example in Final Fall 2006

FORMAT statement, READ

ExampleINTEGER::a,bREAD(*,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!

Page 20: Example in Final Fall 2006

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

Page 21: Example in Final Fall 2006

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)

Page 22: Example in Final Fall 2006

Format Read

ExampleINTEGER::a, b,c

READ(*,100) a,b,c

100FORMAT(I3,x,I2,2x,I4)

• Input: 123456789• A=123• B=56• C=9

Page 23: Example in Final Fall 2006

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.

Page 24: Example in Final Fall 2006

File input/output

Three steps to use a file1. 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. )

Page 25: Example in Final Fall 2006

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”)

Page 26: Example in Final Fall 2006

File input/output, OPEN

Important keywords: [UNIT=] u ! u is a unique number to identify the fileFILE= fname ! File name

Some other keywords (not required in course materials)STATUS, ERR, ISOTAT, ACCESS, FORM, RECL, POSITION

Page 27: Example in Final Fall 2006

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,*)

Page 28: Example in Final Fall 2006

FILE input/output, READ/WRITE

READREAD(unit, label) list-of-variables

label format(format-codes)

or

READ(unit, *) …

WRITEWRITE(unit, label) list-of-variables

label format(format-codes)

or

WRITE(unit, *) …

Page 29: Example in Final Fall 2006

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)

Page 30: Example in Final Fall 2006

FILE input/output, Example

! Input 10 integers from keyboard and write them to file “inputData.txt”PROGRAM fileTest

IMPLICIT NONEINTEGER::count, a

OPEN(UNIT=10,FILE=“inputData.txt”) ! Open file “inputData.txt”DO count=1,10

WRITE(*,*) “Input an integer number from keyboard:”READ(*,*) aWRITE(10,100) “a=“, a ! Write to “inputData.txt”

END DO CLOSE(10); ! Close file “inputData.txt”

100 FORMAT(A2, I8) END PROGRAM

a=######51a=#######6…

inputData.txt

Page 31: Example in Final Fall 2006

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.