input / output statements

19
1 INPUT / OUTPUT STATEMENTS Input Statements:- These statements transfer the initial data from one of the input units to the memory of Computer. Output Statements:- These statements transfer computed data from the memory of the Computer to one of the output units. There are two approaches to perform I/O in FORTRAN I Unformatted Input/Output also known as Format free. II Formatted Input/Output. The only difference between the two is that i) Formatted input statement permits input from a standard input device as per requirement. ii) Formatted output statement sends output to a standard output device as per requirement.

Upload: prescott-adams

Post on 30-Dec-2015

37 views

Category:

Documents


0 download

DESCRIPTION

INPUT / OUTPUT STATEMENTS Input Statements:- These statements transfer the initial data from one of the input units to the memory of Computer. Output Statements:- These statements transfer computed data from the memory of the Computer to one of the output units. - PowerPoint PPT Presentation

TRANSCRIPT

1

INPUT / OUTPUT STATEMENTS Input Statements:- These statements transfer the initial data from one of

the input units to the memory of Computer.

Output Statements:- These statements transfer computed data from the memory of the Computer to one of the output units.

There are two approaches to perform I/O in FORTRAN

I Unformatted Input/Output also known as Format free.

II Formatted Input/Output.

The only difference between the two is that

i) Formatted input statement permits input from a standard input device as per requirement.

ii) Formatted output statement sends output to a standard output device as per requirement.

2

FORMAT STATEMENT : It specifies the form of the data being read or written. Syntax of the statement is:

Statement No. FORMAT (sets of specification)

The Format statement consists :

(i) Statement No. (ii) The word FORMAT (iii) Sets of specification separated by commas and enclosed in parenthesis

Each specification set consists of three elements:

1. Edit descriptor : It gives the type of data.

For Numeric data edit descriptors are : I for integer type data, F for real type data , E for exponenet type data.

For Character data the descriptor is A.

For Logical data the descriptor is L.

2. Field Size : It is used to define the total positions reserved for the mentioned variables.

3. Decimal Location : The decimal location is expressed as the number of places from the right of the field. It is used only for F and E descriptors.

3

I. Unformatted Input/Output statements (Also known as list directed I/O statements)

1. Unformatted Input Statements READ Statement Purpose A READ statement tells the computer to read the data from an input

device and store it in the variables of the input list.

Syntax:Read (*,*) list of input variables separated by commas

where first ‘*’in the parenthesis is for default input device i.e. the key board of VDU (Visual Display Unit) and second ‘*’is used when input format is not

specified. e.g. Read (*, *) A,B,C

4

2. Unformatted output statement:Write Statement:Purpose :- It tells the computer to output the information /data stored in the variables of the output list.

Syntax:Write (*, *) list of output variables separated by commas

i) where first ‘*’in the parenthesis is for default output device i.e. VDS (Visual Display Screen) and second ‘*’is predefined format in the system e.g. Write (*,*) A,B,C.

ii) Print Statement:Purpose :-This statement tells the computer to send the output directly to the Printer.Syntax Print *, list of variables separated by commas.

II. Formatted Input/Output:Formats are used with R/W statements to restrict the size of values given to the variable name. In input, the format statement provides the computer with the type of information and their respective locations within an input record. In output, the FORMAT statement also informs the computer where the information is to be printed.

The format statement is a non-executable statement.

5

1. Formatted Input and Input Field Specifications:The Computer reads data by means of a READ statement according to an accompanying FORMAT. The general form (syntax) of each statement is as follows,READ (device no, format no) variable list

where device no : identification of input device to put the data in the computer. e.g.

02 for card no. 03 for line printer

05 for visual terminal and format no : a statement number which is a positive integer

constant between 1-99999The variable list contains the names of memory locations, separated by commas, in which input data is be stored.The format list contains the field specifications separated by commas.The order of field specifications agree with the order of variable list and they should also agree in types.While reading with the numerical fields specifications, the following two rules apply

i) Blank spaces are interpreted as zeros.ii) Unsigned values are taken as positive.

6

Input filed specifications i.e. format specifiers:I-Field:

The general field specification for reading an integer isIw or rIw

where I indicates that it is an integer w is an unsigned integer constant giving the field width. r is repeat factorSince blanks in numerical fields are taken as zeros, therefore an integer is printed right-justified in its field, so that the last digit of the integer appears in the last column of the given filed.

e.g If we want to read, I=2507 and J = -26i.e. I has value 4 digits long

and J has value 3 digits long,

then the READ statement is as follows READ (*, 20) I,J

20 FORMAT (I 4, I 3)

7

F-Field:Real constants and variables in decimal form can be read using F-fields.The general form is.Fw.d or rFw.dwhere F indicates that the data type is real and the number is written in decimal form.w is an unsigned integer constant denoting the field width d is an unsigned integer representing the number of decimal digits.

r is repeat factore.g. If a number: - 493.725 is to be read into location A using,

READ (*,20) A20 FORMAT (F12.4)

Since the field width given is 12 therefore the first 12 columns are reserved for A. In this case the value of A did not have to be printed right-justified in its field because the number is in decimal form. The blank spaces in numerical fields are interpreted as zero and adding zeros to either of the ends of a number in decimal form does not change its value.

8

e.g:- -493.725 , -493.72500 and -493.7250000 have the same value. The value of A can be read in the following way

Specifically, if there is no decimal point present in the specified field, then the number read will have exactly d decimal digits.

A decimal point is placed between the d and (d+1) columns counting leftward from the right end of the field.

e.g. To read a number x having value, x = 2433.27 if x is inputted as 243327 then READ statement is as follows READ (*, 50)x

50 FORMAT (F7.2) Thus it will be read as 2433.27

9

E-field Real constants in the exponential form are read using E-fields. The

general form of field specification is Ew.d or rEw.dThe letter E indicates that the data type is real and the constant is

written in the exponential form.w is an unsigned integer denoting the field width,d is an unsigned integer denoting the number of decimal digits.r is repeat factor

e.g. if we input a number in the form

A = -0.3457x10 -13then, the READ statement is as follows READ (*, 50)A

50 FORMAT (E 11.4)

10

e.g. If a number – 4.03125 10 + 02 is to be read into location B using READ (*, 30) B

30 FORMAT (E 18.5) then any one of the following ways can be used to input B,

11

In E-field, it is essential that the exponential part be printed right-justified in the field.

If no decimal point is present in the specified field (to the left of E), then a decimal point is assumed to be between d and (d+1) columns, counting leftward beginning with first column of the left of E.

e.g. if the same format is used to read last two inputted values of variable B then the values assigned to it are -4.03125E+2 and-40.3125E1 respectively.

12

X-fields : Several constants on the same data can be separated with one or more blank spaces. These blanks can be skipped by using X-field. An X-field specification has the form, w Xwhere w is an unsigned integer denoting the field width, and X means that corresponding fields of w columns should be skipped.e.g. If a program has statement READ (*, 75) ID, AMT

75 FORMAT ( I7, 3X,F7.2)then ID is assigned the integer in the first numerical field. i.e. first seven columns and columns 8-10 are skipped and then AMT is assigned the real number in the second numerical fields i.e. columns 11-17.Let ID = 13579 and AMT = 150.75, then the input is given as

13

Formatted Write Statement and Carriage Control:-The computer outputs data by means of a WRITE statement and an accompanying FORMAT statement, which have the following form, WRITE (device no, format no) Variable listFormat no. FORMAT (CCC, field specifications)where , device no. : identification of output device format no. : a+ve integer constant between 1-99999The variable list contains the variables whose values are to be printed and are separated by commas.CCC : carriage control charactersCCC are used to activate the output unit to print the value at appropriate positions.It resets a printer by mooving printer head to column 1.

If it is not defined in the FORMAT, then computer will automatically pick up the first character of output as CCC.The first character in the output stream controls the movement of the carriage according to the following instructions,

First character Instruction Format entry

b (blank)

0 (zero)

1

+

Advance one line (i.e. write on new line)

Advance two lines (skip a line, double spacing)

Advance to top of next page

Do not advance

1x or ‘ b ‘

‘0’

‘1’

‘+’

14

In WRITE-FORMAT pair, the numerical value of the variable is always right-justified in its field i.e. the number exits in the last space of the field.

e.g. If we want to print the values of J, K and L,Where, J = 128, K = 72 and L = -179, then the WRITE statement

can be given as,WRITE (*, 20) J,K,L

20 FORMAT (1x, I 8, I 6, I 8)The output stream is as follows

If the WRITE statement is WRITE (*, 30) J,K,L 30 FORMAT ( I 8, I 6, I 8)The output stream is as follows

1 2 3 4 5 6 7 8

1 2 8

9 0 1 2 3 4

7 2

5 6 7 8 9 0 1 2

- 1 7 9

3 4 5 6

1 2 3 4 5 6 7

1 2 8

8 9 0 1 2 3

7 2

4 5 6 7 8 9 0 1

- 1 7 9

2 3 4 5 6

15

Output filed specification:X-field:

It is used to separate several constants on the same line by blank spaces. The general form of field specification is wX or rwX

where w is field widthI-Field:

Integer constants are printed using I-field .Its general form is I w or rIw

where I is integer type and w is field width e.g :- Let M = 280 and N = 46 and WRITE statement is as,

WRITE (*, 60) M,N60 FORMAT (1X, I 6, 3X, I4)then, the output stream is as follows,

Field width must be sufficiently large to accommodate all possible values of the variable.F-Filed : F-field is used to print real constants in decimal form. The general form of field specification is,

Fw.d or rFw.dwhere, F : data type

w : field width d : The number of decimal digits and thus always accomplished by rounding off.

16

e.g :- Let ID = 315 and AMT = 650.3262 and WRITE statement is, WRITE (*, 30) ID, AMT

30 FORMAT (1X, I8, 3X, F10.2) The output stream is,

The second data i.e. 650.3262 is rounded off upto two decimal places, because the field specification for it is F 10.2E-Field:-

Real constants are printed in exponential form using E-field. The general form of the field specification is,Ew.d or rEw.d

where E : denotes the data type w : field width d : accomplished by rounding off. e.g.:- Let, A = 221.465 and B = 0.0071526 and the WRITE statement is,

WRITE (*, 33) A, B 33 FORMAT (1X, E15.4, E12.3)

the output stream is as follows,

17

NOTE: The number of output positions (w) required is at least 7 more than size of the fraction part (1 for sign, 1 for 0 to the left of decimal point, 1 for decimal & 4 for exponent i.e. one for E (exponent), one for + or –sign after E, two for two digits in exponent

w > (d+7)

Literal Fields: Messages can also be printed using FORMATIf we want to print : ‘INPUT THE DATA’then, the statement for it is as follows,WRITE (*,30)

30 format (1X, ‘INPUT THE DATA’)then, the output stream is INPUT THE DATA

NOTE: Messages can also be printed using unformatted write statement e.g.

Write (*,*) ‘ODD NUMBERS ARE’

18

We may also print character messages together with numerical values,

e.g. : If we have calculated the sum and average of three numbers such that SUM = 30 and AVERAGE =10 then, the WRITE statements is as follows,

WRITE (*, 80) SUM, AVG80 FORMAT(6X, ‘SUM=’, I3, 4X, ‘AVERAGE=’ F4.2)

The output stream is, NOTE: Unformatted form of this statement is WRITE(*,*) ‘SUM=‘, SUM, ’AVERAGE=‘, AVG

Multiple Records :- If numbers A, B, C, D are read/printed using the same specification, say, F 7.2 then the statement would be

READ (*,20) A,B,C,D 20 FORMAT (F7.2, F7.2, F7.2, F7.2)

To simplify , a repetition factor 4 can be used, i.e. FORMAT (4 F 7.2)Similarly FORMAT ( I4, I4, E15.7, E 15.7)and FORMAT (2I4, 2E 15.7)are the same.A group of field specifications can also be repeated.e.g.:- FORMAT (F 5.2, F7.2, F5.2, F7.2)and FORMAT (2(F5.2, F7.2))are the same.

Print Statement : Syntax PRINT label, list of variables label FORMAT ( list of instructions )e..g. PRINT 25, A, B 25 FORMAT ( 1X , 2 F8.2 )

19

Example:

Write a program for the average of three numbers.

C PROGRAM FOR AVERAGE OF THREE NUMBERS

READ (*, 10) A, B, C

10 FORMAT (3 F8.2)

SUM = A+B+C

AV = SUM/3

WRITE (*,20) AV

20 FORMAT (5 X ,‘AVERAGE IS’ , F9.2)

STOP

END