Chapter 8 More control over input and output - PowerPoint PPT Presentation

1 / 76
About This Presentation
Title:

Chapter 8 More control over input and output

Description:

must be delimited by matching apostrophes or quotation marks ... not a quotation mark or an apostrophe, since this would be taken as a delimiting character. ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 77
Provided by: assis7
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8 More control over input and output


1
Chapter 8 More control over input and output
8.1 The interface between the user and the
computer 8.2 Formats and edit descriptors 8.3
Input editing using embedded formats 8.4 FORMAT
statements 8.5 Output editing 8.6 READ, WRITE
and PRINT statements 8.7 Printer control
characters 8.8 More powerful formats
2
8.1 The interface between the user and the
computer
  • 8.1 computers deal w/ numbers/chars. How to
    input/output (i.e read/write) data?
  • A line of input data 123456789
  • There are an enormous number of possible
    interpretations of this line.
  • It could be the number 123456789
  • Or it could be the nine numbers 1, 2, 3, 4, 5,
    6, 7, 8 and 9
  • Or it could be the three numbers 123, 456, 789
  • Or it could even be the number 12345.6789
  • Or it could be the four numbers 1.23, 0.45, 67
    and 8900
  • Or it could be one of hundreds of other valid
    interpretations of these nine digits

3
8.1 (2)
  • If it was input we can check what was typed in
  • For output we have various options
  • The rules accord with the natural way of
    presenting data, lead to a quite unambiguous
    interpretation of the data.
  • With output, we would like to have more control
    over the way
  • in which the results are laid out on the screen.
  • For example for 2 vars values 12.34, -7.89 to
    be printed in many formatse.g.
  • The answers are 12.34 -7.89
  • The answers are
  • 12.34 -7.89
  • or
  • The answers are
  • 12.34
  • -7.89
  • or a number of other variations.

4
8.1 (3)
  • There is also the question of
  • where the data comes from and
  • where the results are to be sent.
  • All of these questions need to be resolved every
    time
  • any data is input to,
  • or results are output from, a program.
  • Up to this point all our input and output has
    been carried out
  • using list-directed READ and PRINT statements.
  • Recall READ, or PRINT uses value separators
    to separate
  • different data

5
8.1 (4)
  • The data is considered to be a sequence of
    alternating values and value separators, with the
    occurrence of a value separator indicating the
    termination of the previous value.
  • For numeric data
  • The value separators are of four types
  • a comma,
  • optionally preceded and/or followed by one or
    more blanks.
  • a slash (/),
  • optionally preceded and/or followed by one or
    more blanks.
  • one or more consecutive blanks.
  • the end of the record (that is, of the line),
  • optionally preceded by one or more blanks.

6
8.1 (5)
If there are no values between two consecutive
value separators, then the effect is to read a
null value to leave the value unchanged. If
a slash value separator is encountered then no
more data items are read, and processing of the
input statement is ended. If there are any
remaining items in the input list, the result
is as though null values had been input to them.
7
8.1 (6)
  • Character strings being input by a list-directed
    READ statement
  • must be delimited by matching apostrophes or
    quotation marks
  • unless all of the following conditions are met
  • For CHAR data need apostrophes or quotes unless
  • the character data does not contain any blanks,
    any commas or any slashes. (that is, it does not
    contain any of
  • the value separators discussed earlier)
  • the character data is all contained within a
    single record or line.
  • the first non-blank character is
  • not a quotation mark or an apostrophe,
  • since this would be taken as a delimiting
    character.
  • the leading characters are not numeric followed
    by an asterisk, since this would be confused
    with
  • the multiple data item form (nc).

8
8.1 (7)
  • If all of these conditions are met,
  • the character data being input is a single
    'word'.
  • The layout of the results by a list-directed
    PRINT statement
  • is left to the processor to determine.

9
8.2 Formats and edit descriptors
Input and output editing
10
8.2 (2)
The key element in both the input and output
processes, is the editing of information in one
form for presentation in another form. The input
and output statements that we have been using
thus far have taken the forms (Editing)
form so far
e.g. READ,x,y READ ,
input_list PRINT ,output_list
11
8.2 (3)
Each of these statements actually has three
forms Other forms to be studied
100 FORMAT(I9) READ ch_var, input_list
READ I9 , n READ label,
input_list e.g READ 100, n READ ,
input_list READ ,
n and PRINT ch_var, output_list PRINT label,
output_list PRINT ,output_list i.e READ
(edit_descriptor_list),input_list where ch_var
is a character constant, character variable,
character array, character array element or
other character expression, and label is a
statement label.
12
8.2 (4)
The item following READ and PRINT is a format
specifier which provides a link to the
information necessary. This information is
called a format and consists of a list of edit
descriptors enclosed in parentheses (ed_des1,ed
_des2,) The first variation, is called an
embedded format READ (edit_descriptor_list),
input_list or READ (edit_descriptor_list),
input_list We shall always use the first of the
two forms shown above. In the second variation
the statement label is the label of a new type
of statement called a FORMAT statement.
13
8.3 Input editing using embedded formats
The various edit descriptors are concerned with
the editing of actual data, and altering the
order in which the characters in the input
record are edited. If we wished to read the line
shown in Figure 8.1 as a single integer we
could write READ '(I9) ', n as three
separate integers we could write READ '(I3, I3,
I3) ', n1, n2, n3
14
8.3 (2)
Edit descriptors for input Descriptor
Meaning

Iw Read the next w characters as an
integer Fw.d Read the next w characters as a
real number with d digits after the decimal
place Ew.d if no decimal point is
present Aw Read the next w characters as
characters A Read sufficient characters to fill
the input list item, stored as
characters Lw Read the next w characters as the
representation of a logical value nX Ignore
the next n characters Tc Next character to be
read is at position c TLn Next character to be
read is n characters before (TL) or TRn after
(TR) the current position TL To left of current
position TR To right of current position
15
8.3 (3)
There is an implied concept of an
index. Normally this index is moved through the
record as characters are read however, the X, T,
TL and TR edit descriptors allow the index to be
moved without any characters being read. e.g.
Space or tab between positions e.g.
123456789 (input) Thus, READ '(4X, I5)', num !
Leave 4 spaces then read num56789 will ignore
the first four columns and then read READ
'(I2, 3X, I3)', i, j ! Read i, leave 3 spaces
read j, will cause the value 12 to be stored in
i and 678 in j.
16
8.3 (4)
The next three edit descriptors are, essentially,
three variations on a single theme Tc TRn T
Ln The first of these causes a tab to character
position c READ '(T4, I2, T8, I2, T2, I4)', x,
y, z ! Move to pos. 4, read x, will read the
number 45 into x, reading 89 into y, and
reading the number 2345 into z. The T edit
descriptor moves to a character position which
is defined absolutely by its position in the
record, or line of data. The TL and TR edit
descriptors specify a relative tab.
17
8.3 (5)
The letters TR followed by a number n indicate
that the next character is to be n positions to
the right of the current position it is
thus identical in its effect to nX. The
letters TL followed by a number n specifies a tab
to the left. Fw.d causes the next w
characters to be read and converted into a real
number. If the w columns do not contain any
decimal point then the number has d decimal
places.
18
8.3 (6)
READ '(F9.4)', real_num 123456789 The variable
real_num will therefore have the number
12345.6789 The input line of data is
123456789 READ '(F3.1, F2.2, F3.0, TL6, F4.2)',
r1, r2, r3, r4 cause the value 12.3 to be stored
in r1, 0.45 in r2, 678.0 in r3, and 34.56 in
r4. For another line of data .23.56.8 The
value 0.23 stored in r1 r2 is given the value
0.5 6.8 is the value stored in r3 '3.56' is
stored in r4
19
8.3 (7)
The effect of the F edit descriptor during
input READ '(F3.1, F2.2, F3.0, TL6, F4.2)',
r1, r2, r3, r4 Data 123456789 .23.56.8 r1
contains 12.3 0.23 r2 contains 0.45 0.5 r3
contains 678.0 6.8 r4 contains 34.56 3.56
Ew. d on input, is interpreted in an identical
way. On output, however, as we shall see, it
is different from an F edit descriptor.
20
8.3 (8)
  • The A edit descriptor is used to control the
    editing of character data.
  • It takes one of the forms
  • Aw
  • A
  • the edit descriptor Aw refers to the next w
    characters.
  • - For CHAR data use Aw, A (here
    wlen(CHAR))
  • If we assume that length of the input list item
    is len then
  • If w is less than len, then, extra blank
    characters will be added at the end.
  • If w is greater than len, however,
  • the rightmost len characters of the input
    character string
  • will be stored in the input list item.

21
8.3 (9)
An A edit descriptor without any field width w is
treated as though the field width was
identical to the length of the corresponding
input list item. Thus, for CHARACTER
ch110, ch28, ch315 the following two
statements will have an identical effect READ
'(A10, A8, A15)', ch1, ch2, ch3 READ '(A, A,
A)', ch1, ch2, ch3 eg.
InputlineMississipi Amazon is the longest river
22
8.3 (10)
The edit descriptor used with logical data, takes
the form Lw This edit descriptor processes
the next w characters to derive either a true
value, a false value, or an error. There are
exactly two ways of representing true and
false Tccc c or .Tcccc Fccc c or .Fcccc
where c represents any character.
23
8.3 (11)
Logical data for input with a L8 edit
descriptor Data items interpreted Data items
interpreted as true as false T F TRUE
FALSE .T .F .T. .F. .TRUE .FALSE .TRU
E. .FALSE. Truthful Fanciful terrible futi
le true false .t
.f If the first non-blank character, other than
a period, is not T or F, or their lower case
equivalents, then an error will occur.
24
8.3 (12)
EXAMPLE 8.1 (1)Problem A survey, consisting of a
maximum of 1000 respondents, has recorded the
name, age, sex, marital status, height and
weight of a number of people.
25
8.3 (13)
The information has been recorded as follows
e.g. Thomas Smith M
0 32 178 80.50 First
name in columns 1-15 Last name in columns
21-40 Sex coded in column 43 F female M
male Marital status coded in column 45 0
single, 1 married, 2 widowed 3 divorced, 4
cohabiting, 9 unknown Age (yrs) in columns 47,
48 Height (cm) in columns 51-53 Weight (kg) in
columns 56-62 in the form kkk.ggg
26
8.3 (14)
The data is terminated by a line which has END OF
DATA typed in columns 1 to 11. Write and test a
procedure to read this data and store it in a
form suitable for subsequent analysis. Such
analysis will require the heights to be stored in
meters. A module, Global_Data, is available which
contains, among other things, the type definition
for a type person with components defined as
follows MODULE Global_data
IMPLICIT none SAVE
TYPE person CHARACTER(LEN15)
first_name CHARACTER(LEN20)
last_name CHARACTER sex ! M or
F INTEGER marital_status, age REAL
height, weight END TYPE person
END MODULE Global_data
27
8.3 (15)
(2)Analysis When developing a large program it
is always a good idea first to write and
thoroughly test an input procedure. Data
design Purpose Type Name A Arguments
Array of personal data PERSON people Number
of people in survey INTEGER number_people B
Local variables Maximum number of data
sets INTEGER max_people DO
variable INTEGER i
28
8.3 (16)
  • Structure plan
  • Subroutine input(people, number_people)
  • USE Global_data
  • 1 Set max_people to the size of the dummy
    argument people
  • Repeat up to max_people times
  • 2.1 Read next record
  • 2.2 If terminator found then exit
  • 3 Return number of data sets read

29
8.3 (17)
(3)Solution SUBROUTINE input(people,
number_people) USE Global_Data IMPLICIT
NONE ! An input subroutine for a survey !
Dummy arguments TYPE(person), DIMENSION()
people INTEGER number_people ! Local
variables INTEGER i, max_people ! Store
maximum number of allowable data sets
max_people SIZE(people)
30
8.3 (18)
! Display data format PRINT ,"Type data
as follows " PRINT ,"Cols. 1-15 First
name" PRINT ,"Cols. 21-40 Last name"
PRINT ,"Col. 43 Sex (Ffemale, Mmale)"
PRINT ,"Col. 45 Marital status (0single,
1married," PRINT ," 2widowed,
3divorced, 4cohabiting," PRINT ,"
9unknown)" PRINT ,"Cols. 47,48 Age
(in years)" PRINT ,"Cols. 51-53 Height (in
cm.)" PRINT ,"Cols. 56-62 Weight (in kg in
the form kkk.ggg)" PRINT ," " PRINT
,"Data should be terminated by the words
'END OF DATA' typed in cols. 1-11"
PRINT ," "
31
8.3 (19)
! Loop to read data DO i1, max_people
READ(A15,5X,A20,2X,A1,1X,I1,1X,I
2, 2X,F3.2,2X,F7.3)',
people(i) ! Check if this is the
terminator record IF(people(i)first_name(111)
"END OF DATA") EXIT END DO
32
8.3 (20)
! Check to see if a terminator was found
IF (igtmax_people) THEN PRINT
,"Maximum number of records (
,max_people,") read" PRINT
,"with no terminator - input halted"
! Save number of data records read
number_people max_people ELSE
number_people i-1 END IF
RETURN END SUBROUTINE input
33
8.4 FORMAT statements
We can define the format in a special,
non-executable, FORMAT statement which takes
the form label FORMAT (edit_descriptor_list) A
FORMAT statement must always be preceded by a
label because that is referenced in a READ
statement. READ label, output_list e.g
100 FORMAT(I9) READ 100, n The labelled
FORMAT statement may appear anywhere in the
program unit after the initial statement and
any USE statements, and before the END
statement. We recommend that any FORMAT
statements in a program unit should be kept
together either near the start of the program
unit, or after all the executable statements.
34
8.4 FORMAT statements
  • label FORMAT(edit_descriptor_list)
  • An example of the use of FORMAT statements
  • SUBROUTINE format_statement_demo(group_1,
    group_2, group_3)
  • USE Global_Data
  • IMPLICIT NONE
  • ! An input subroutine for a survey
  • ! Dummy arguments
  • TYPE(person), DINENSION() group_1, group_2,
    group_3
  • !Local variables
  • INTEGER i1, i2, i3, code

35
  • ! Input format
  • 100 FORMAT (A15,5X,A20,2X,I1,1X,I1,1X,I2,2X,F3.2
    ,2X,F7.3)
  • SELECT CASE (code)
  • CASE (-1) ! code lt 0 group_1
  • READ 100, group_l(i1)
  • ..
  • CASE (0) ! code 0 group_2
  • READ 100, group_2(i2)
  • CASE (1) ! code gt 0 group_3
  • READ 100, group_3(i3)
  • END SELECT
  • END SUBROUTINE input

36
8.5 Output editing
The I edit descriptor (Iw) causes an integer to
be output in such a way as to utilize the next
w character positions. tom 23 dick
715 harry -12 PRINT '(I5, I5, I5)' ,tom,
dick, harry will produce the following line of
output ???23??715??-12
37
8.5 (4)
Edit descriptors for output Descriptor Meaning
Iw Output an integer in the next w character
positions. Fw.d Output a real number in the next
w character positions with d decimal
places. Ew.d Output a real number in the next w
character positions using an exponent format
with d decimal places in the mantissa and
four characters for the exponent. Aw Output a
character string in the next w character
positions. A Output a character string,
starting at the next character position, with
no leading or trailing blanks.
38
8.5 (5)
Lw Output w - 1 blanks, followed by T or F to
represent a logical
value nX Ignore the next n character
positions. Tc Output the next item starting at
character position c. TLn Output the next item
starting n character positions before (TL)
or TRn after (TR) the current
position. "c1c2...cn" Output the string of
characters c1c2 ... cn, starting at the next
character 'c1c2...cn' position
If the output is to go to the computer's printer,
the first character on the line will not be
printed. Fw.d indicates that a real number is to
be output occupying w characters, of which the
last d are to follow the decimal point.
39
8.5 (6)
. The real value to be output is rounded to d
places of decimals before it is sent to the
relevant output device. E.g tom 23
dick 715 harry -12
PRINT (I5,I5,I5),tom,dick,harry
will produce
???23??715??-12 E.g x 3.14159 y
-275.3024 z 12.9999 PRINT '(F10.3, F10.3,
F10.3)', x, y, z will produce ?????3.142??-275.
302????13.000
40
8.5 (7)
Ew descriptor e.g. 1.5E-6 or 1.5D-6
1.510-6 Some of the ways in
which the real data item 361.764 may be written
361.764 3.617642 361764
-3 0.0361764E4 3617.64D-1 3.61764E2 361.7
640 The E edit descriptor is used for a form of
notation which represents the number as a
mantissa and an exponent. Recall Fw.d will be
rounded to d decimal places Ew.d uses the
fraction exponent form where Wtotal no. of
character places gt 4 character places for
exponent d digits for fraction
41
8.5 (9)
  • The E edit descriptor, produces a representation
    of
  • a real number consisting of a decimal
    fraction, m, in the range
  • 0.1 lt m lt 1.0,
  • with d digits of precision, followed by a
    four character exponent
  • the whole number will occupy a field
    width of w characters.
  • The number 0.000 036 176 4, will be output as
  • F10.4 ????0.0000
  • F12.6 ????0.000036
  • F14.8 ????0.00003618
  • E10.4 0.3618E-04
  • E12.6 0.361764E-04
  • E14.8 0.36176400E-04
  • NOTE Remaining places of w chars are filled
    with spaces at the
  • beginning or left of the number

42
8.5 (10)
  • If the exponent is greater than 99, or less than
    -99,
  • then the exponent will be output as a plus or
    minus sign, followed by a three digit
    exponent.
  • If the number does not require the full field
    width w,
  • it will be preceded by one or more spaces.
  • Aw will cause characters to be output to
  • the next w character positions of the output
    record,
  • CHARACTER ch110, ch26, ch315
  • PRINT '(A10, A6, A15)', ch1, ch2, ch3
  • eg. Mississipi Amazon longest river
  • If the length of the output list item is not
    exactly w, the rules are
  • If w is greater than len then
  • the character string will be preceded by 1 or
    more blanks.
  • If w is less than len then the leftmost w
    characters will be output.

43
8.5 (11)

E.g. Spaces at the beginning An example of
tabular printing PROGRAM tabular_output IMPLICIT
NONE REAL, PARAMETER third1.0/3.0 REAL
x INTEGER i PRINT, The
output. DO i1,10 ! PRINT The output.
xi PRINT '(F15.4,F15.4,F15.4)', x,
SQRT(x), xthird END DO END PROGRAM
tabular_output
44
8.5 (12)
The output from this program will
be 1.0000 1.0000 1.0000 2.0000 1.4142 1.2599
3.0000 1.7321 1.4422 4.0000 2.0000 1.5874
5.0000 2.2361 1.7100 6.0000 2.4495 1.8171 7.
0000 2.6458 1.9129 8.0000 2.8284 2.0000 9.00
00 3.0000 2.0801 10.0000
3.1623 2.1544
45
8.5 (13)
Note that for list-directed input assignment
rules apply. With an A edit descriptor, the
character string being output will occupy
exactly the space it requires. The descriptor Lw
will cause w - 1 blanks to be output, followed by
the letter T or the letter F to indicate true or
false. A number, called a repeat count, may be
placed before the I, F, E, A or L edit
descriptors to indicate how many times they
are to be repeated. Thus (I5, I5, I5, F6.2,
F6.2, F6.2, F6.2) could be written as (3I5,
4F6.2)
46
8.5 (14)
A repeat count may be used in formats for both
input and output. A format may also include a
character constant edit descriptor.
Thus PRINT '( "The result is ", I5 )',
result PRINT "( 'The result is ', I5 )",
result The important thing is to be
consistent. The X edit descriptor (nX) is used
to ignore, or skip over, the next n character
positions.
47
8.5 (15)
. E.g. CHARACTER(LEN6) aSPRING,
bFALL PRINT ( 5X,A8,5X,A4 ),a,b will
print - - - - - - - SPRING - - - - -
FALL E.g. Usually we combine print of text and
numbers in format e.g. Result21030 PRINT(
The result is, I5 ), result will print The
result is 21030
48
8.6 READ, WRITE and PRINT statements
A more general form of READ statement READ
(cilist) input_list where cilist is a control
information list consisting of one or more
items, known as specifiers, separated by
commas. All specifiers take the same basic
form keyword value the keyword may be
omitted in two cases, in certain circumstances.
49
8.6 (3)
The default input unit will usually be unit 5.
We shall assume that it is unit 5, but it must
be emphasized that this is only an
assumption. UNIT 5 or UNIT to identify
the default input unit. If, and only if, the
unit specifier is the first item in the control
information list we may omit the UNIT keyword
and the sign, and simply write 5 or
50
8.6 (4)
However, we recommend that the UNIT keyword
should always be included. The input will need
to be converted from some external form,to an
internal form. We need a format, identified by a
format specifier FMT ch_var FMT
label FMT
51
8.6 (5)
If the format specifier is the second item in the
control information list and the first item is a
unit specifier without any keyword then then, the
FMT keyword and sign may also be omitted from
the format specifier. Recall READ,a,b,c OR
with format READ(,3F6.2) a,b,c means
that we read from the standard or default input
unit of the computer. Another input unit in
FORTRAN is UNIT5 for READ and UNIT6 for
PRINT E.g. READ (UNIT5, FMT'(3F6.2) ') x, y,
z READ (FMT'(3F6.2)', UNIT5) x, y, z READ (5,
FMT100) x, y, z READ (5, 100) x, y, z READ (5,
'(3F6.2)') x, y, z 100 FORMAT (3F6.2)
52
8.6 (6)
As with the UNIT keyword, we strongly recommend
that the FMT keyword should always be
included. The statement READ (,) a, b, c is
identical to READ , a, b, c The remaining
specifier that we shall discuss here is concerned
with monitoring the outcome of the reading
process. IOSTAT io_status where io_status
is an integer variable.
53
8.6 (7)
  • There are four possibilities
  • The variable is set to zero to indicate that no
    errors occurred.
  • The variable is set to a processor-dependent
    positive value to indicate that an error has
    occurred.
  • The variable is set to a processor-dependent
    negative value to indicate that a condition
    known as
  • an end-of-file condition has occurred.
  • The variable is set to a processor-dependent
    negative value to indicate that a condition
    known as
  • an end-of-record condition has occurred.

54
8.6 (8)
We use IOSTAT to determine whether or not the
reading of data was carried out successfully by
testing the value of the variable in an IF or
CASE construct An asterisk as a unit identifier
refers to the default input/output unit. READ
(UNIT,FMT'(5F6.3)',IOSTATioerror)
p,q,r,s,t IF (ioerror / 0) THEN ! ioerror
is non-zero . ! Print error/warning
PRINT,Error in Read message .
! and take remedial action . ! before
exit from procedure RETURN END
IF ! Continue with normal processing . END
55
8.7 Printer control characters (SKIP 8.7)
When a line of output is to be sent to the
printer, the Fortran output system will remove
the first character of the line and interpret it
as a printer control character. There are four
characters which have a particular
significance. If the first character is not one
of these four then the effect on the printer is
undefined in practice, however, any other
character will usually have the same effect as
does a space. Because the first character is
removed and not printed it is important that we
insert an extra (control) character at the start
of each record that is to be output to the
printer.
56
8.7 (2)
Printer control characters (The new compiler
fixed this problem) Character Vertical spacing
before printing ? (space) one line (i.e. print
on next line) 0 (zero) two lines 1 (one) first
line of next page (plus) no paper advance
(overprint) The two formats shown could be
rewritten as 200 FORMAT (1X, F5.2) 201 FORMAT
(1X, F5.3) This only applies to the printer, or
to other units which the compiler designates as
printing units. The PRINT statement
automatically inserts a (space) control character
at the start of each line if the default output
unit is the printer.
57
8.7 (3)
E.g. Use F5.2 to print number will actually
use the first of the 5 char places for control
and will only print 4 chars and . An example of
printer control errors PROGRAM
poor_printer_control IMPLICIT NONE REAL x,
y x 3.0 y 4.0 WRITE (UNIT, FMT200)
x WRITE (UNIT, FMT200) y WRITE (UNIT,
FMT200) xy WRITE (UNIT, FMT201) x/y 200
FORMAT (F5.2) 201 FORMAT (F5.3) END PROGRAM
poor_printer_control
58
8.7 (4)
The output from this program will
be 3.00 4.00 - - - - - - - - - - - -
-----------(new page)! because ! of xy
12.00 2.00 ! 2 lines because
x/y0.750 .750 We could better use 200 FORMAT
(1X, F5.2) 201 FORMAT (1X, F5.3) instead of
200 FORMAT (F5.2) 201 FORMAT (F5.3)
59
8.8 More powerful formats
Concern multi-records formats, and the repetition
of formats. E.g.1 Consider (implied Do Loop
for input) READ 100, (arr(i), i1, 4) READ 100,
(arr(i), i5, 8) READ 100, (arr(i), i 9,
12) 100 FORMAT (4F12.3) What would happen if
we wrote READ 100, arr READ 100, (arr(i),
i1, 12) After the READ statement has used the
format to input four real numbers it finds that
the input list is not yet exhausted.
60
8.8 (2) Skip form 61 till page 66
There is only one sensible thing to do at this
stage - namely to read a new record and
interpret its contents using the same format.
Whenever a format is fully used up and there
are still items in the input list awaiting
processing, the format will be repeated.
61
8.8 (3)
  • The rules are
  • If there are no nested parentheses then the
    format is repeated from the beginning.
  • If the format contains any nested parentheses
    then it is repeated from immediately after the
    left parenthesis corresponding to the rightmost
    nested parenthesis.
  • If the left parenthesis defined above is
    preceded by a repeat count
  • then the format is repeated including the repeat
    count.
  • i.e. If no ( ) the format repeated from
    start else from ( of rightmost )

62
8.8 (4)
An arrow ( ) is shown below the point from
which repetition will take place (I6, 10X,
I5, 3F10.2) (I6, 10X, I5, (3F10.2)) (I6,
(10X, I5), 3F10.2) (F6.2, (2F4.1, 2X, I4, 4(I7,
F7.2))) (F6.2, 2(2F4.1, 2X, I4), 4(I7,
F7.2)) (F6.2, (2(2F4.2, 2X, I4), 4(I7,
F7.2))) ((F6.2, 2(2F4.2, 2X, I4), 4(I7,
F7.2))) a format which processes two or more
separate lines, or records, is achieved by the
/ edit descriptor.
63
E.g. On output, a / terminates the current
record and starts a new one. Thus READ
'(3F8.2/3I6)', a, b, c, p, q, r will read three
real numbers from the first record and three
integers from the second.
8.8 (5)
64
8.8 (6)
An example of a multi-line output format E.g.
Using / separates records. WRITE (UNIT6,
FMT201) a, b, ab, ab 201 FORMAT("1", T10,
"Multi-record example"/ "0", "The sum
of", F6.2, " and", F6.2, " is ", F7.2/
1X, "Their product is", F10.3) ! It
skips t0 because of 1 and new line because
of 0 - - - - - - - - - - - - - - - - - - - - -
- - - - (new page) Multi-record example
The sum of 12.25 and 23.50 is 35.75 Their
product is 287.875
65
8.8 (8)
Another example of multi-line output
formatting - - - - - - - - - - - - - - - (new
page) ! Because of 1 ! Because of
1111 Another multi-record example The sum of
12.25 and 23.50 is 35.75 Their product is
287.875
66
8.8 (9)
EXAMPLE 8.2 (1) Problem A piece of experimental
apparatus is monitoring the radioactive decay
of a specimen. At approximately regular
intervals it records the time since the start of
the experiment (in hundredths of a second),
the number of a-particles emitted during the
interval, the number of ß-particles emitted and
the amount of ?-radiation in the same period.
These are output as an eight-digit number (for
the time) and three six-digit numbers.
67
8.8 (10)
There are five spaces between each number. Write
a program to read this data and to print a table
containing the following information a
sequence number for each interval, the length
of the interval, the three readings obtained
and the average emission of a-particles,
ß-particles and ?-rays (per second) during the
interval. After 1000 time intervals print
the time interval which had the highest rate of
emission of ?-radiation.
68
8.8 (11)
(2) Analysis This is a straightforward problem,
which is primarily concerned with the use of
formats to read the data and lay out the results.
We shall use constants (in and out) in which
to store the unit numbers, thus making it much
easier to change these if it should
subsequently be desired to do so.
69
8.8 (13)
Data design Purpose Type Name A
Constants I/0 unit numbers INTEGER
in, out Maximum number of readings INTEGER
max_readings B Variables Time since
start (data) REAL time Experimental
readings (data) INTEGER alpha, beta,
gamma DO variable (and sequence
no.) INTEGER i Time of last reading and
interval REAL last-time, period Average
emissions REAL av_alpha, av_beta,
av_gamma Maximum average gamma REAL
max_av_gamma Interval with max ave.
gamma INTEGER max_interval
70
8.8 (14)
Structure plan 1 Initialize maximum gamma
radiation and interval 2 Print column headings 3
Repeat max_readings times 3.1 Read next set
of data 3.2 Calculate length of interval
and average emissions 3.3 Print details
3.4 If ?-radiation gt max ?-radiation then
3.4.1 Save maximum ?-radiation and
interval number 4 Print details of maximum
?-radiation
71
8.8 (15)
  • Solution (p268.f)
  • PROGRAM Radioactive_decay
  • IMPLICIT NONE
  • ! This program processes experimental data
    relating
  • ! to radioactive decay
  • ! Constant declarations
  • ! max_readings is maximum number of sets of
    data
  • ! in and out are the unit numbers for reading
    and writing
  • INTEGER,PARAMETER max_readings1000, in5,
    out6
  • ! Variable declarations
  • INTEGER alpha, beta,gamma, i,
    max_interval0
  • REAL time, last_time0.0, period,
  • av_alpha, av_beta, av_gamma,
    max_av_gamma0.0

72
8.8 (16)
! Print headings WRITE (UNITout, FMT201)
! Process max_readings sets of data in a
loop DO i1, max_readings READ (UNITin,
FMT101) time, alpha, beta, gamma !
Calculate interval since last readings
period time-last_time last_time time
! Calculate average rates of emission
av_alpha alpha/period av_beta
beta/period av_gamma gamma/period
73
8.8 (17)
! Print statistics for this interval
WRITE (UNITout,FMT202) i,period,alpha,beta,gamma
, av_alpha,
av_beta, av_gamma ! Check for maximum
gamma radiation in this period IF (av_gamma
gt max_av_gamma) THEN max_av_gamma
av_gamma max_interval i END
IF END DO ! Print details of interval with
maximum gamma radiation WRITE
(UNITout,FMT203) max_av_gamma, max_interval
74
8.8 (18)
! Format statements 101 FORMAT (1X,F8.2,
3(5X,I6) ) 201 FORMAT ("1","Interval",T11,"Tim
e",T17,"Alpha", T24,"Beta",T30,"Gamma"
,T37,"Average",T46,"Average",
T55,"Average /
T38,"Alpha",T47,"Beta",T56,"Gamma") 202
FORMAT (I6,F8.2,2I6,I7,2F9.2,F10.2) 203
FORMAT ("0",T3,"Maximum average gamma radiation
was", F7.2," in
interval",I5) END PROGRAM Radioactive_decay The
E edit descriptor will often be better than the
F edit descriptor. Using an E edit descriptor
will enable all to be shown to the same level
of accuracy.
75
8.8 (19)
Results produced by the Radioactive_decay
program Interval Time Alpha Beta Gamma Average
Average Average Alpha
Beta Gamma 990 2.56 175 23 401
68.36 8.98 156.64 991 2.59 168
22 395 64.86 8.49 152.51 992 2.48
181 27 412 72.98 10.89 166.13 993
2.51 177 25 410 70.52 9.96 163.35
994 2.48 166 29 391 66.94 11.69
157.66 995 2.54 181 25 397 71.26
9.84 156.30 996 2.51 169 28 407
67.33 11.16 162.15 997 2.58 159
23 388 61.63 8.91 150.39 998 2.51
177 26 401 70.52 10.36 159.76 999
2.47 173 24 398 70.04 9.72
161.13 1000 2.52 183 28 403 72.62
11.11 159.92 Maximum average gamma radiation
was 174.28 in interval 741
76
8.8 (20)
Results produced by the modified
Radioactive_decay program Int. Time A B
G Average Average
Average Alpha
Beta Gamma 990 2.56 175 23 401
0.68359E02 0.89844E01 0.15664E03 990 2.56
175 23 401 0.68359E02 0.84944E 01
0.15664E03 991 2.59 168 22 395
0.64865E02 0.84942E01 0.15251E03 992 2.48
181 27 412 0.72984E02 0.10887E02
0.16613E03 993 2.51 177 25 410
0.70518E02 0.99602E01 0.16335E03 994 2.48
166 29 391 0.66935E02 0.11694E02
0.15766E03 995 2.54 181 25 397
0.71260E02 0.98425E01 0.15630E03 996 2.51
169 28 407 0.67331E02 0.11155E02
0.16215E03 997 2.58 159 23 388
0.61628E02 0.89147E01 0.15039E03 999 2.51
177 26 401 0.70518E02 0.10359E02
0.15976E03 999 2.47 173 24 398
0.70040E02 0.97166E01 0.16113E03 1000 2.52
193 28 403 0.72619E02 0.11111E02
0.15992E03 Maximum average gamma radiation was
0.17428E03 in interval 741
Write a Comment
User Comments (0)
About PowerShow.com