INPUT OUTPUT STATEMENTS Input Statements These statements transfer

  • Slides: 19
Download presentation
INPUT / OUTPUT STATEMENTS Input Statements: - These statements transfer the initial data from

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. 1

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

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. 2

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

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 3

2. Unformatted output statement: Write Statement: Purpose : - It tells the computer to

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. 4

1. Formatted Input and Input Field Specifications: The Computer reads data by means of

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 -99999 The 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. 5

Input filed specifications i. e. format specifiers: I-Field: The general field specification for reading

Input filed specifications i. e. format specifiers: I-Field: The general field specification for reading an integer is Iw or r. Iw where I indicates that it is an integer w is an unsigned integer constant giving the field width. r is repeat factor Since 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 = -26 i. e. I has value 4 digits long and J has value 3 digits long, 20 then the READ statement is as follows READ (*, 20) I, J FORMAT (I 4, I 3) 6

F-Field: Real constants and variables in decimal form can be read using F-fields. The

F-Field: Real constants and variables in decimal form can be read using F-fields. The general form is. Fw. d or r. Fw. d where 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 factor e. g. If a number: - 493. 725 is to be read into location A using, READ (*, 20) A 20 FORMAT (F 12. 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. 7

e. g: - -493. 725 , -493. 72500 and -493. 7250000 have the same

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 (F 7. 2) Thus it will be read as 2433. 27 8

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

E-field Real constants in the exponential form are read using E-fields. The general form of field specification is Ew. d or r. Ew. d The 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 50 A = -0. 3457 x 10 -13 then, the READ statement is as follows READ (*, 50)A FORMAT (E 11. 4) 9

e. g. If a number – 4. 03125 10 + 02 is to be

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, 10

In E-field, it is essential that the exponential part be printed rightjustified in the

In E-field, it is essential that the exponential part be printed rightjustified 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. 03125 E+2 and 40. 3125 E 1 respectively. 11

X-fields : Several constants on the same data can be separated with one or

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. X where 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 ( I 7, 3 X, F 7. 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 12

Formatted Write Statement and Carriage Control: The computer outputs data by means of a

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 list Format no. FORMAT (CCC, field specifications) where , device no. : identification of output device format no. : a+ve integer constant between 1 -99999 The variable list contains the variables whose values are to be printed and are separated by commas. CCC : carriage control characters CCC 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 1 x or ‘ b ‘ ‘ 0’ ‘ 1’ ‘+’ 13

In WRITE-FORMAT pair, the numerical value of the variable is always rightjustified in its

In WRITE-FORMAT pair, the numerical value of the variable is always rightjustified 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 (1 x, I 8, I 6, I 8) The output stream is as follows 12345678 901234 56789012 3456 128 72 - 179 If the WRITE statement is WRITE (*, 30) J, K, L 30 FORMAT ( I 8, I 6, I 8) The output stream is as follows 1234 567 890123 45678901 23456 128 72 -179 14

Output filed specification: X-field: It is used to separate several constants on the same

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 w. X or rw. X where w is field width I-Field: Integer constants are printed using I-field. Its general form is I w or r. Iw 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, N 60 FORMAT (1 X, I 6, 3 X, I 4) 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 r. Fw. d where, F : data type w : field width d : The number of decimal digits and thus always accomplished by rounding off. 15

e. g : - Let ID = 315 and AMT = 650. 3262 and

e. g : - Let ID = 315 and AMT = 650. 3262 and WRITE statement is, WRITE (*, 30) ID, AMT 30 FORMAT (1 X, I 8, 3 X, F 10. 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. 2 E-Field: Real constants are printed in exponential form using E-field. The general form of the field specification is, Ew. d or r. Ew. 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 (1 X, E 15. 4, E 12. 3) the output stream is as follows, 16

NOTE: The number of output positions (w) required is at least 7 more than

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 FORMAT If we want to print : ‘INPUT THE DATA’ then, the statement for it is as follows, WRITE (*, 30) 30 format (1 X, ‘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 ’ 17

We may also print character messages together with numerical values, e. g. : If

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, AVG 80 FORMAT(6 X, ‘SUM=’, I 3, 4 X, ‘AVERAGE=’ F 4. 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 (F 7. 2, F 7. 2) To simplify , a repetition factor 4 can be used, i. e. FORMAT (4 F 7. 2) Similarly FORMAT ( I 4, E 15. 7, E 15. 7) and FORMAT (2 I 4, 2 E 15. 7) are the same. A group of field specifications can also be repeated. e. g. : - FORMAT (F 5. 2, F 7. 2, F 5. 2, F 7. 2) and FORMAT (2(F 5. 2, F 7. 2)) are the same. Print Statement : Syntax PRINT label, list of variables label e. . g. 25 FORMAT ( list of instructions ) PRINT 25, A, B FORMAT ( 1 X , 2 F 8. 2 ) 18

Example: Write a program for the average of three numbers. C PROGRAM FOR AVERAGE

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 F 8. 2) SUM = A+B+C AV = SUM/3 WRITE (*, 20) AV 20 FORMAT (5 X , ‘AVERAGE IS’ , F 9. 2) STOP END 19