{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

(8) Input & Output

# (8) Input & Output - Verifying ISBN Numbers(again The...

This preview shows pages 1–4. Sign up to view the full content.

9/23/2009 1 The Ins and Outs of FORTRAN Input and Output Nathan Friedman 2009 1 Format Verifying ISBN Numbers (again ?!?) program isbn implicit none integer :: digits(10) integer :: pos, sum logical :: valid read (*,”(10I1)”) (digits(pos), pos = 1,10) 2009 Format 2 sum = 0 do pos = 1,10 sum = sum + (11-pos)*digits(pos) end do valid = mod(sum,11) == 0 if (valid) then write(*,*) "ISBN is valid" else write(*,*) "ISBN is invalid" end if end program isbn What happened to the second * ? The second * in the read was replaced by a format string A format string can be used to specify 2009 Format 3 where to look for the input The format (10I1) means we expect 10 values Each value is an integer (I) Each value is one digit in length (1) FORTRAN Formats The READ and WRITE statements we have seen so far are called free- format statements. They are easy to 2009 Format 4 use but we have no control over the placement of the input or appearance of the output. To control the appearance of the input and output, Fortran allows us to use format specifications How much was that? PROGRAM cost IMPLICIT NONE REAL :: price, gst, pst READ(*,*) price 2009 Format 5 gst = 0.07*price pst = 0.075*(price + gst) WRITE(*,*) "Price: ",price WRITE(*,*) "GST: ", gst WRITE(*,*) "PST: ", pst WRITE(*,*) "Total Cost: ",price+gst+pst END PROGRAM cost The results aren’t very pretty. > 136.95 Price: 136 9500 2009 Format 6 Price: 136.9500 GST: 9.586500 PST: 10.99024 Total Cost: 157.5267

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9/23/2009 2 Wouldn’t this be nicer? > 136.95 Price: 136 95 2009 Format 7 Price: 136.95 GST: 9.59 PST: 10.99 Total Cost: 157.53 Formats FORTRAN formats allow us to specify the placement of values both in output and input Using format descriptors we can control the appearance of output values 2009 Format 8 Format descriptors specify The appearance of output values Repetition Vertical positioning Horizontal positioning Cost With Formatting PROGRAM cost IMPLICIT NONE REAL :: price, gst, pst READ(*,*) price 2009 Format 9 gst = 0.07*price pst = 0.075*(price + gst) WRITE(*,100) "Price: ",price WRITE(*,100) "GST: ", gst WRITE(*,100) "PST: ", pst WRITE(*,100) "Total Cost: ",price+gst+pst 100 FORMAT (A15,F7.2) END PROGRAM cost Fortran Formats The FORMAT statement in the previous example specifies a format 2009 Format 10 100 FORMAT (A15,F7.2) A format is list of descriptors inside parentheses ( .... format descriptors .... ) Fortran Formats – Method 1 There are two ways to specify a format. In the first, we write the format as a 2009 Format 11 character string and use it to replace the second asterisk in WRITE(*,*). WRITE(*,"(A15,F7.2)") "Total Cost: ", price+gst+pst Fortran Formats – Method 2 The most common method uses a FORMAT statement A FORMAT statement has the syntax: 2009 Format 12 label FORMAT format-code To use the format, we specify its label in the WRITE statement WRITE(*,100) "Total Cost: ", price+gst+pst 100 Format (A15,F7.2)
9/23/2009 3 Format Codes We will look at a few of the many format codes available in FORTRAN for specifying: 2009 Format 13 1. Real values 2. Integer values 3. Character values 4. Horizontal spacing 5.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 10

(8) Input & Output - Verifying ISBN Numbers(again The...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online