UNIHIVE

Study Hub UNIPORT ENG 213.1

ENG 213.1 · Computer Programming for Engineers

Input and output

List-directed READ and PRINT, formatted output with FORMAT descriptors, and prompts.

Our notes

1 min read

List-directed input and output

The simplest form uses * for "the default format":
read *, a, b, c
print *, 'Sum =', a + b + c
The user types the values separated by spaces or commas and presses Enter.

Prompting the user

print *, 'Enter pipe diameter (m) and velocity (m/s):'
read *, d, v
Always say what to type and in which units.

Formatted output

List-directed output prints many digits in a layout the compiler chooses. For neat tables, give a format:
print '(A, F8.3)', 'Velocity (m/s) = ', v

DescriptorMeaningExample output
Iwinteger in w columnsI5 → " 42"
Fw.dreal, w columns, d decimalsF8.3 → " 12.346"
Ew.dexponent formE12.4 → " 0.1235E+02"
ESw.dscientific (one digit before the point)ES12.4 → " 1.2346E+01"
Atextthe string as is
nXn spaces3X
/new line

If the number does not fit in w columns, Fortran prints asterisks: always allow room for the sign and the decimal point.

Worked example: a table

program ctof
  implicit none
  integer :: c
  real :: f
  print '(A)', ' C F'
  do c = 0, 100, 25
    f = 9.0 / 5.0 * c + 32.0
    print '(I4, F8.1)', c, f
  end do
end program ctof
Output:
   C F
   0 32.0
  25 77.0
  50 122.0
  75 167.0
 100 212.0

Using the FORMAT statement (older style)

     write(6, 100) x, y
100 format(1X, 'X =', F8.3, ' Y =', F8.3)
Unit 6 is normally the screen and unit 5 the keyboard. Modern code usually puts the format string in the statement itself.

Exams usually ask

  • Write READ and PRINT statements for given data.
  • Show the exact output of a formatted PRINT statement.

Common mistake

Choosing a field too narrow: printing 1234.567 with F6.2 gives because the value needs at least 7 columns.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All ENG 213.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.