FORTRAN 90 Lecture : Rafel Hekmat Hameed University of Babylon Subject : Fortran 90 College of Engineering Year : Second B.Sc Mechanical Engineering Dep. Directed Input: The READ Statement The READ statement can read input values into a set of variables from the keyboard. The READ statement has the following forms: READ(*,*) var1, var2, ..., varn Read*, var1, var2, ..., varn The following example reads in four values into variables Factor, N, Multiple and tolerance in this order. INTEGER :: Factor, N REAL :: Multiple, tolerance READ(*,*) Factor, N, Multiple, tolerance The following example reads in a string into Title, followed by three real numbers into Height, Length and Area. CHARACTER(LEN=10) :: Title REAL :: Height, Length, Area READ(*,*) Title, Height, Length, Area Preparing Input Data: The input data values must be separated by space or commas. For the following READ CHARACTER(LEN=5) :: Name REAL :: height, length ϭ INTEGER :: count, MaxLength READ(*,*) Name, height, count, length, MaxLength The input data may look like the following: "Smith" 100.0 25 123.579 10000 The execution of a READ always starts searching for input values with a new input line. INTEGER :: I, J, K, L, M, N READ(*,*) I, J READ(*,*) K, L, M READ(*,*) N If the above READ statements are used to read the following input lines, 100 200 300 400 500 600 Then I, J, K, L, M and N will receive 100, 200, 300, 400, 500 and 600, respectively. If the input value is an integer and the corresponding variable is of REAL type, the input integer will be convert to a real number. But, if the input value is a real number and the corresponding variable is of INTEGER type, an error will occur. Finally, a READ without a list of variables simply skips a line of input. Consider the following: INTEGER :: P, Q, R, S READ(*,*) P, Q READ(*,*) READ(*,*) R, S If the input lines are 100 200 300 400 500 600 Ϯ 700 800 900 The first READ reads 100 and 200 into P and Q and 300 is lost. The second READ starts with a new input line, which is the second one. It does not read in anything. The third READ starts with the third line and reads 700 and 800 into R and S. The third value on the third line, 900, is also lost. Directed Output: The WRITE Statement The WRITE statement can display the results of a set of expressions and character strings. In general, WRITE displays the output on the screen. The WRITE statement has the following forms: WRITE(*,*) exp1, exp2, ..., expn PRINT*, exp1, exp2, ..., expn The first form starts with WRITE(*,*), followed by a list of arithmetic expressions or character strings, separated by commas. The following example displays the values of four variables on screen: INTEGER :: Factor, N REAL :: Multiple, tolerance WRITE(*,*) Factor, N, Multiple, tolerance The following example displays the string content of Title, followed by the result of (Height + Length) * Area. CHARACTER(LEN=10) :: Title REAL :: Height, Length, Area WRITE(*,*) Title, (Height + Length) * Area ϯ PROGRAM output implicit none character (len=15) :: name integer :: iyear , imonths , iage write(*,*) 'hello, what is your name' read(*,*) name write(*,*) "how old are you, (years and months)?" read(*,*) iyears, imonths iage=iyeats*12+imonth write(*,*) name,'is',iage,'months old!' stop end File I/O It is also possible to read or write from files which are stored on some external storage device, typically a disk (hard disk, floppy) or a tape. In Fortran each file is associated with a unit number, an integer between 1 and 99. Before you can use a file you have to open it. OPEN ( unit= 1,file='aa.dat') If you read data from file aa.dat, then read statement will be Read (1,*) a,b,c Or if you need to print your results in to output file bb.dat then the write statement will be OPEN ( unit= 22,file='bb.dat') Write (22,*) x ϰ Examples (H.W) Given three real numbers, its arithmetic mean (average), geometric mean and harmonic mean are defined as follows: Write a program to compute and display the means of three REAL variables initialized with positive real values. Print your results in out put ut file Program Output Data items: 1., 2., 3. Arithmetic mean = 2. Geometric mean = 1.81712067 Harmonic Mean = 1.63636363 ϱ