REAL Input

WARNING: This example assumes spaces are ignored for the I and F descriptors.

Problem Statement

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 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, x, y, Sum
   CHARACTER(LEN=40) :: ReadIn, WriteOut

   ReadIn   = "(2F10.2,F20.2,F10.2)"
   WriteOut = "(I5, A, 2F7.2)"

   READ(*,"(I5)")  Number
   Sum = 0.0
   WRITE(*,"(A, A)")  " ", "Input Number List"
   WRITE(*,"(A, A)")  " ", "================="
   DO i = 1, Number
      READ(*,ReadIn)  u, v, x, y
      WRITE(*,WriteOut)  i, " - ", v, y
      Sum = Sum + v*y
   END DO
   WRITE(*,"(A, A)")  " ", "---------------------"
   WRITE(*,"(A, A, F7.2)")  " ", "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
....5....0....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

Discussion