Read/Write

 

The input and output is achieved by using keywords, READ and WRITE (or PRINT) respectively.

 

The syntax of input statement:

                        READ (input_device, format[, options])  list_of_variables

                        READ format, list_of_variables  (standard input device, usually the keyboard)

 

For example

                        READ (*,*) A, B, C

            the input device * indicates that data is read from the standard input device (usually the keyboard); the format * indicates that it is free format and the computer will decide how to format; the program is expecting 3 numbers entered from keyboard and those three numbers will be stored in the three variables A, B and C.

            The statement can also be replaced by

READ *, A, B, C 

 

The syntax of output statement:

                        WRITE (output_device, format[, options]) list

                        PRINT format, list       (standard output device, usually the screen)

 

For example

                        WRITE (*,*) ‘A=’, A, ‘B=’, B, ‘C=’, C

            the output device * is to use the standard output device ( usually the screen); the format * means that it is free format and the computer decides how to format; the list consists of character strings (inside ‘ ’) and variables; data in the variables will be displayed instead of the variable names.

            This statement can be replaced by

                        PRINT *, ‘A=’, A, ‘B=’, B, ‘C=’, C

            If A, B and C have data of 1, 2 and 3 respectively, the output will be

                                    A=1  B=2  C= 3  

 

 

Exercises

 

1). Give an example of using READ to input 2 real numbers and 1 integer number (declare the variables before the READ statement).

 

2). Write the output statement in the following program.

      integer a, b, c

      read(*,*) a, b

      c = a + b

      <output statement>

      end

For example, if numbers in a and b are 5 and 6, the output should be displayed as

            5 + 6 = 11

 

3). Type in the following program to a file (cylinder.f95), and then compile it. You should find the program has mistakes. Correct the mistakes and run the program.

!  

!   Your name, Group

!   Date

!

! finds surface area and volume of a cylinder

!

      Program cylinder

      IMPLICIT NONE

      pi = 4*atan(1.0)

      write(*,*)'diameter of cylinder?'

      read(*,*)dia

      write(*,*)'length of cylinder?'

      read(*,*)length

      sarea = pi*dia*length

      vol = pi*(dia**2)*length/4.

      write(*,*)' '

      write(*,*)'diameter of cyl.     =',dia

      write(*,*)'length of cyl.       =',length

      write(*,*)'surface area of cyl. =',sarea

      write(*,*)'volume of cyl.       =',vol

      stop

      end program cylinder

 

 

4). Write a program (TrigonometricFunctions.f95) that reads an angle in degrees from the keyboard and prints out the sine, cosine, and tangent of the angle in the form given below:

 

deg      =  30.0000

sin(deg) =  0.5

cos(deg) =  0.866025388

tan(deg) =  0.577350259