Input and Output

In the preceding section, we considered the assignment statement, which enables us to calculate the values of expressions and store the results of these computations by assigning them to variables. An assignment statement does not, however, display these results on an output device, nor does it allow the user to enter new values during execution. For example, if a projectile is launched from an initial height of HGHT0 with an initial vertical velocity of VELOC0 and a vertical acceleration of ACCEL, then the equations

HGHT = 0.5 * ACCEL * TIME ** 2 + VELOCO * TIME + HGHT0

and

VELOC = ACCEL * TIME + VELOC0

give the height (HGHT) and the vertical velocity (VELOC) at any TIME after launch. The program below assigns the value‹9.807 (m/sec2) to ACCEL, 150.0 (m) to HGHT0,100.0 (m/sec) to VELOCO, and 5.0 (sec) to TIME and then computes the corresponding values of HGHT and VELOC.

      PROGRAM PROJEC
************************************************************************
* This program calculates the velocity and height of a projectile      *
* given its initial height, initial velocity, and constant             *
* acceleration.  Variables used are:                                   *
*    HGHT0   :  initial height                                         *
*    HGHT    :  height at any time                                     *
*    VELOC0  :  initial vertical velocity                              *
*    VELOC   :  vertical velocity at any time                          *
*    ACCEL   :  vertical acceleration                                  *
*    TIME    :  time elapsed since projectile was launched             *
*                                                                      *
* Input:   none                                                        *
* Output:  none                                                        *
************************************************************************

      REAL HGHT0, HGHT, VELOC0, VELOC, ACCEL, TIME


      ACCEL = -9.807
      HGHT0 = 150.0
      VELOC0 = 100.0
      TIME = 5.0
      HGHT = 0.5 * ACCEL * TIME ** 2  +  VELOC0 * TIME  +  HGHT0
      VELOC = ACCEL * TIME + VELOC0
      END
      

The values of HGHT and VELOC are calculated as desired, but they are stored internally and are not displayed to the user. Moreover, if the same calculation is to be done for the same acceleration but with values 100.0 for HGHT0, 90.0 for VELOC0, and 4.3 for TIME, then several statements must be modified, as shown in below, and the program executed again.

      PROGRAM PROJEC
************************************************************************
* This program calculates the velocity and height of a projectile      *
* given its initial height, initial velocity, and constant             *
* acceleration.  Variables used are:                                   *
*    HGHT0   :  initial height                                         *
*    HGHT    :  height at any time                                     *
*    VELOC0  :  initial vertical velocity                              *
*    VELOC   :  vertical velocity at any time                          *
*    ACCEL   :  vertical acceleration                                  *
*    TIME    :  time elapsed since projectile was launched             *
*                                                                      *
* Input:   none                                                        *
* Output:  none                                                        *
************************************************************************

      REAL HGHT0, HGHT, VELOC0, VELOC, ACCEL, TIME

      ACCEL = -9.807
      HGHT0 = 100.0
      VELOC0 = 90.0
      TIME = 4.3
      HGHT = 0.5 * ACCEL * TIME ** 2  +  VELOC0 * TIME  +  HGHT0
      VELOC = ACCEL * TIME + VELOC0
      END


The output statement that we consider in this section provides a method for easily displaying information. We also consider an input statement that provides a convenient way to assign values from an external source during execution of the program.

FORTRAN provides two types of input/output statements. In the first type, the programmer must explicitly specify the format in which the data is presented for input or, in the case of output, the precise format in which it is to be displayed. In the second type of input/output, certain predetermined standard formats that match the types of items in the input/output list are automatically provided by the compiler. It is this second type, known as list-directed input/output, that we consider in this section. List-Directed Output

The simplest list-directed output statement has the following form:

List-Directed Output Statement

Form:
PRINT *, output-list
where:
output-list is a single expression or a list of expressions separated by commas. Each of these expressions is a constant, a variable, or a formula.

The statement may also be used in the form

PRINT *
where no output list is used.
Purpose:
Displays the values of the items in the output list. Each PRINT statement produces a new line of output. If the output list is omitted, a blank line is displayed.

For example, to display some of the relevant information from the preceding example, we might add two PRINT statements, as shown below. Execution of the program produces output similar to that shown. The exact format and spacing used to display these values are compiler dependent; for example, in some systems, real values might be displayed in scientific notation, and the number of spaces in an output line might be different from that shown.

      PROGRAM PROJEC
************************************************************************
* This program calculates the velocity and height of a projectile      *
* given its initial height, initial velocity, and constant             *
* acceleration.  Variables used are:                                   *
*    HGHT0   :  initial height                                         *
*    HGHT    :  height at any time                                     *
*    VELOC0  :  initial vertical velocity                              *
*    VELOC   :  vertical velocity at any time                          *
*    ACCEL   :  vertical acceleration                                  *
*    TIME    :  time elapsed since projectile was launched             *
*                                                                      *
* Input:   HGHT0, VELOC0, TIME                                         *
* Output:  VELOC, HGHT                                                 *
************************************************************************

      REAL HGHT0, HGHT, VELOC0, VELOC, ACCEL, TIME

      ACCEL = -9.807
      READ *, HGHT0, VELOC0, TIME
      HGHT = 0.5 * ACCEL * TIME ** 2  +  VELOC0 * TIME  +  HGHT0
      VELOC = ACCEL * TIME + VELOC0
      PRINT *, 'AT TIME ', TIME, ' THE VERTICAL VELOCITY IS ', VELOC
      PRINT *, 'AND THE HEIGHT IS ', HGHT
      END

In some situations, one or more blank lines in the output improve readability. A blank line can be displayed by a PRINT statement of the form

PRINT *

in which the output list is empty. Note that the comma that normally follows the * is also omitted. Execution of each statement of this form causes a single blank line to be displayed.

List-Directed Input

The simplest form of the list-directed input statement is

List-Directed Input Statement

READ *, input-list
where
input-list is a single variable or variables separated by commas.
Purpose:
Causes the transfer of values from some external source (usually the keyboard or a file) and the assignment of these values to the variables in the input list. The following rules apply:
  1. A new line of data is processed each time a READ statement is executed.
  2. If there are fewer entries in a line of input data than there are variables in the input list, successive lines of input are processed until values for all variables in the list have been obtained.
  3. If there are more entries in a line of input data than there are variables in the input list, the first data values are used, and all remaining value's are ignored.
  4. The entries in each line of input data must be constants and of the same type as the variables to which they are assigned. (However, an integer value may be assigned to a real or a double precision variable, and a real value may be as signed to a double precision variable, with automatic conversion taking place.)
  5. Consecutive entries in a line of input data must be separated by a comma or by one or more spaces

For example, the statement

READ *, HGHTO, VELOCO, TIME

assigns values to the variables HGHTO, VELOCO, and TIME. Therefore, this single READ statement replaces the three assignment statements used to assign values to these variables in the preceding examples. The modified program is shown below.

      PROGRAM PROJEC
************************************************************************
* This program calculates the velocity and height of a projectile      *
* given its initial height, initial velocity, and constant             *
* acceleration.  Variables used are:                                   *
*    HGHT0   :  initial height                                         *
*    HGHT    :  height at any time                                     *
*    VELOC0  :  initial vertical velocity                              *
*    VELOC   :  vertical velocity at any time                          *
*    ACCEL   :  vertical acceleration                                  *
*    TIME    :  time elapsed since projectile was launched             *
*                                                                      *
* Input:   HGHT0, VELOC0, TIME                                         *
* Output:  VELOC, HGHT                                                 *
************************************************************************

      REAL HGHT0, HGHT, VELOC0, VELOC, ACCEL, TIME

      ACCEL = -9.807
      READ *, HGHT0, VELOC0, TIME
      HGHT = 0.5 * ACCEL * TIME ** 2  +  VELOC0 * TIME  +  HGHT0
      VELOC = ACCEL * TIME + VELOC0
      PRINT *, 'AT TIME ', TIME, ' THE VERTICAL VELOCITY IS ', VELOC
      PRINT *, 'AND THE HEIGHT IS ', HGHT
      END


In an interactive mode of operation, the values assigned to variables in an input list are entered during program execution. In this case, when a READ statement is encountered, program execution is suspended while the user enters values for all the variables in the input list.

Program execution then automatically resumes. Because execution is interrupted by a READ statement and because the correct number and types of values must be entered before execution can resume, it is good practice to provide some message to prompt the user when it is necessary to enter data values. This is accomplished by preceding each READ statement with a PRINT statement that displays the appropriate prompts. The program below illustrates this by prompting the user when values for HGHT0,VELOC0, and TIME are to be entered; it is a modification of the program in above.

      PROGRAM PROJEC
************************************************************************
* This program calculates the velocity and height of a projectile      *
* given its initial height, initial velocity, and constant             *
* acceleration.  Variables used are:                                   *
*    HGHT0   :  initial height                                         *
*    HGHT    :  height at any time                                     *
*    VELOC0  :  initial vertical velocity                              *
*    VELOC   :  vertical velocity at any time                          *
*    ACCEL   :  vertical acceleration                                  *
*    TIME    :  time elapsed since projectile was launched             *
*                                                                      *
* Input:   HGHT0, VELOC0, TIME                                         *
* Output:  VELOC, HGHT                                                 *
************************************************************************

      REAL HGHT0, HGHT, VELOC0, VELOC, ACCEL, TIME

      ACCEL = -9.807
      PRINT *, 'ENTER THE INITIAL HEIGHT AND VELOCITY:'
      READ *, HGHT0, VELOC0
      PRINT *, 'ENTER TIME AT WHICH TO CALCULATE HEIGHT AND VELOCITY:'
      READ *, TIME
      HGHT = 0.5 * ACCEL * TIME ** 2  +  VELOC0 * TIME  +  HGHT0
      VELOC = ACCEL * TIME + VELOC0
      PRINT *, 'AT TIME ', TIME, ' THE VERTICAL VELOCITY IS ', VELOC
      PRINT *, 'AND THE HEIGHT IS ', HGHT
      END

Now go to the exercises and work your way through the input/output and arithmetic problems.


Work your way through the following components attempting the exercises as you come across them:

| Programs | Variables | Arithmetic Operations | Input and Output | Looping in Programs | Arrays in Programs | Checking variables | Subprograms and functions |