Saturday, February 5, 2011

Classical Fortran: Programming for Engineering and Scientific Applications, Second EditionMost of the examples shown here are originally from the following book,

Michael Kupferschmid, "Classical Fortran: Programming for Engineering and Scientific Applications", Taylor & Francis Group, 2009

The generalized form of READ command is as follows,

READ (unit, format) list

where, the value of 'unit' could be any value ranging from 0-99 or star symbol '*'. Both 5 or * means standard-in (usually keyboards). If the 'format' is *, then the format will be according to the compiler default. However if 'format' is not * , then it will refer the label of the FORMAT statement. Note that the FORMAT statement is used to specify exactly how the input/output will be. The 'list' is the list of variables to be read.

Any of the commands are used to read an input for variable A,

READ (*,*) 
and

READ *,
are same commands. It means the program will take an input from standard-in (usually keyboard).
An example of FORMAT statement (just copy and paste in your editor)



         PROGRAM readonce
         REAL
*8 X/1.23D0/, Y/53.0D0/
         
J=37
         WRITE
(6,901J,X,Y
 901     FORMAT
(1X,I3/'X=',T7,F5.2/D8.2)
         
STOP
         END PROGRAM readonce 
In the 5th line of the above program, 901 means just a label and nothing else. In that FORMAT statement, 1X means skip one space; I3 means print the integer in 3 spaces; / means skip to next line; 'X=' means print X=; T7 means tab to column 7; F5.2 means print a real number using a total of 5 spaces with 2 digits following the decimal point; again / means skip the next line; and similarly D8.2 means print a real number with 8 space and 2 decimal digits. The output of the above program is as follows,

  37
X
=     1.23
0.53D
+02 


Field spec.         Meaning
------------------------------------------------------------------------------------------------------------------------------------
 nX                    skip n space
 Iw                     print an integer right justified in w column
 mIw                  m times Iw
 Fw.d                 print a real number with w column and d digit after decimal point
 Dw.d                 same as Fw.d same as F but use fraction-exponent form
 L1                     print a logical value as T or F
 Zh                     print a hexadecimal value having h hexits
 Tn                     tab to column n
 'string'               print the text string
 /                       skip to the next line
------------------------------------------------------------------------------------------------------------------------------------
                                                                           (pp.149 of Kupferschmid M., 2009)

Now see what happens by running the following program (just copy and paste),



      program read3
      write
(6,
10
10    format('enter a number: ')
      
read(5,*) Z
      stop
      end 

After running the above program you will be asked to enter a value. Enter a numerical value 456. Here the output you will see as follows,


enter a number: 
456 
Note that the cursor here is going to the next line, where you entering the number 456. Now try the following program again and see what happens.



      program read3
      write
(6,10

10    format('enter a number: ',$)
      
read(5,*) Z
      stop
      end 
Enter 456 and finally you will see the following output form of output. Here the cursor is in the same line, not in the next line.

enter a number: 456
hmmmm!


Code: The Hidden Language of Computer Hardware and Software

 The Code Book: The Science of Secrecy from Ancient Egypt to Quantum Cryptography

 Code Complete: A Practical Handbook of Software Construction

 The Da Vinci Code