Skip Input Items

WARNING: This example assumes spaces are ignored for the I and F descriptors and the output is sent to a printer.

Problem Statement

We shall redo the REAL numbers input problem discussed previously in REAL input. In fact, we shall modify the previous program using nX and Tc edit descriptors.

Suppose we have an input organized as follows. The first line gives the number of lines, followed by that number of input lines. Each line is divided into four parts with 10 positions each, except for the third data which occupies positions 21 to 40. Each part contains a REAL number. Moreover, each input number should have two digits for the fractional part. Note that the number are not aligned well.

         1    1    2    2    3    3    4    4    5
....5....0....5....0....5....0....5....0....5....0
5
12.3            1234  1.234             3.14
  1.2E-3        3341    1.2E-5            1.23E+1
7.69E-3         2314      12.34E-9       2.44E-1
12345           3309        1 3 4 5       7.64E-2
  77 88         1953    7 5               3.34E+1
Write a Fortran program that reads in the above input, computes the sum of the product of the second and fourth numbers on each line, and prints the following table:
         1    1    2    2
....5....0....5....0....5
 Input Number List
 =================

    1 -   12.34   3.14
    2 -   33.41  12.30
    3 -   23.14   0.24
    4 -   33.09   0.08
    5 -   19.53  33.40
 ---------------------
 Sum = 1110.17
Note that the first position is always for printer control.

Solution

PROGRAM  Select_Numbers
   IMPLICIT  NONE
   INTEGER           :: Number, i
   REAL              :: u, v, Sum
   CHARACTER(LEN=40) :: ReadIn, WriteOut, Title, Last

   Title    = "(1X,A)"
   ReadIn   = "(T11, F10.2, T41, F10.2)"
   WriteOut = "(1X, I4, A, 2F7.2)"
   Last     = "(1X, A, F7.2)"

   READ(*,"(I5)")  Number
   Sum = 0.0
   WRITE(*,Title)  "Input Number List"
   WRITE(*,Title)  "================="
   WRITE(*,*)
   DO i = 1, Number
      READ(*,ReadIn)  u, v
      WRITE(*,WriteOut)  i, " - ", u, v
      Sum = Sum + u*v
   END DO
   WRITE(*,Title)  "---------------------"
   WRITE(*,Last)   "Sum = ", Sum
END PROGRAM  Select_Numbers
Click here to download this program.

Program Input and Output

The output of the program is:
         1    1    2    2    3    3    4    4    5
....5....0....5....0....5....0....5....0....5....0
5
12.3            1234  1.234             3.14
  1.2E-3        3341    1.2E-5            1.23E+1
7.69E-3         2314      12.34E-9       2.44E-1
12345           3309        1 3 4 5       7.64E-2
  77 88         1953    7 5               3.34E+1

Discussion