Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 9 More Control over INPUT and OUTPUT (I / O).

Similar presentations


Presentation on theme: "Week 9 More Control over INPUT and OUTPUT (I / O)."— Presentation transcript:

1 Week 9 More Control over INPUT and OUTPUT (I / O)

2 More Control over INPUT and OUTPUT (I / O) The input and output facilities of any programming language are extremely important, because it is through these features of the language that communication between the user and the program is carried out. F, therefore, provides facilities for input and output at two quite different levels. As are given before, list-directed input and output statements that provide the capability for straightforward input from the keyboard and also the output to the display or printer. These list-directed “i/o” - statements, however, allow the user very little control over the source or layout of the input data.

3 F allows the programmer, to specify exactly how the data will be presented and interpreted, from which of the available input units it is to be read how the results are to be displayed to which of the available output units the results are to be sent. More Control over INPUT and OUTPUT (I / O)

4 INTERFACE between the USER and the COMPUTER Considering the following 9 – digit input data there are enormous number of possible interpretations : Data : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 - 1, 2, 3, 4, 5, 6, 7, 8, and 9 - 123, 456, 789 - 12345. 6789 - 1.23, 0.45, 67, 8900 - etc.

5 FORMATS and EDIT DESCRIPTORS An input statement must contain three distinct types of information where the data is to be found where it is to be stored in the computer’s memory how it is to be interpreted Similarly, an output statement must define where the results are currently stored where they are to be sent in what form they are to be displayed.

6 INPUT EDITING EXAMPLES of INPUT DATA : 123456789 read “ ( i 3, i 3, i 3 )”, n1, n2, n3 ! n1 = 123, n2 = 456, n5 = 789 read “ ( t 4, i 2, t 8, i 2, t 2, i 4 )”, x, y, z ! x = 45, y = 89, z = 2345 read “ ( f 9.4 )”, real_num ! real_num = 12345.6789 read “ ( f 3.1, f 2.2, f 3.0 )”, r1, r2, r3 ! r1 = 12.3, r2 = 0.45, r3 = 678.0 123456789

7 EXAMPLE for formatting of real numbers using “ f – edit descriptor “ depending on the two different INPUT DATA : read “ ( f 3.1, f 2.2, f 3.0 )”, s1, s2, s3 DATA 1 DATA 2 123456789.23.56.8 ---------------------- ---------------- - s1 contains :12.3 0.23 s2 contains :0.45 0.5 s3 contains :678.0 6.8

8 EXAMPLEs for formatting of an input real number in different ways using “ a – edit descriptor “ real number is : 361.764 361.764 3.61764+2 361764–3 0.0361764e4 3617.64d-1 3.61764 E +2 361.764+0

9 EXAMPLEs for formatting of an input – character type using “ a – edit descriptor “ character ( len = 10 ) : : ch1 character ( len = 6 ) : : ch2 character ( len = 15 ) : : ch3 Depending on the above-given declaration the formats may have the following forms in read statements : read “ ( a10, a6, a15 )”, ch1, ch2, ch3 or read “ ( a, a, a )”, n1, n2, n3

10 LOGICAL DATA - INPUT EDITING The edit descriptor is used with logical data, and takes the following form, where upper case “ L” is used to avoid the potential confusion with the digit “ 1 “ that can be caused to human readers by using the lower-case form : L w “ w “ caharacters are used to derive either a “ true “ value, a “ false “ value or an error.

11 Any of the following are acceptable as representing true :  t  true .T .true.  truthful while the following will all be interpreted as false :  F  False .f.  futile LOGICAL DATA - INPUT EDITING

12 OUTPUT EDITING The following figure shows the main edit descriptor that are available for output. EXAMPLE for “i” edit descriptor tom = 23 dick = 715 harry = -12 print *, “( i 5, i 5, i 5 )”, tom, dick, harry This statement produce following line of output (where the symbol # represents a space ) : ###23##715##-12

13 EXAMPLE for “f” edit descriptor : x = 3.14159 y = -275.3024 z = 12.9999 print *, “( f 10.3, f 10.3, f 10.3 )”, x, y, z This statement produce following line of output (where the symbol # represents a space ) : #####3.142##-275.302####-13.00 OUTPUT EDITING

14 EXAMPLE : program tabular_output real, parameter : : third = 1.0 / 3.0 real : : x integer : : i do i = 1, 10 x = 1.0 print “( f 15.4, f 15.4, f 15.4 )”, x, sqrt (x), x**third end do end program tabular_output OUTPUT EDITING

15 In the above-given program the same edit descriptor has been repeated several times. A number, called a “repeat count”, may be placed before the “ i, f, a or L “ – edit descriptors to indicate how many times they are to be repeated. Depending on this, the format could be written more succinctly and İt is possible to write it more clearly. EXAMPLE : print “( i 5, i 5, i 5, f 6.2, f 6.2, f 6.2, f 6.2 )”, x, y, z, d, e, f, g or better print “( 3 i 5, 4 f 6.2 )”, x, y, z, d, e, f, g OUTPUT EDITING

16 Exercise 2 program test1 integer ::a,b real::c,d read "(i4,t1,i4,t1,f4.1,t1,f4.2)",a,b,c,d print "(i4,a,i5,a,i5,a,f6.2,a,f6.2,a,f8.3)",a,& " minus",b," is",a-b,";",c," minus",d, " is",c-d end program test1

17 Exercise 3 program test2 character(len=6)::a,b,c read "(a8,t1,a4,t1,a)",a,b,c print "(a10,tr12,a4,tr30,a)",a,b,c print "(a,t10,a,t52,a)",a,b,c end program test2

18 program five_digit_numbers integer::i integer,dimension(12)::arr arr = (/12345, 23456, 34567, 45678,& 90123, 12340, 24680, 46802,& 13579, 35791, 57913, 69649/) !output for single column format example print*,"Single column of numbers" do i = 1, 12 print "(t8,i2,1x,i5)",i,arr(i) end do !output for four rows of three numbers print "(2/t3,a/)","Four rows of three numbers " print "(t8,i2,1x,3(i5,1x))", 1,arr(1:3) print "(t8,i2,1x,3(i5,1x))", 2,arr(4:6) print "(t8,i2,1x,3(i5,1x))", 3,arr(7:9) print "(t8,i2,1x,3(i5,1x))", 4,arr(10:12) ! a single line of numbers print "(2/t3,a/)", "A single line of numbers " print "(t2,12(i5,1x))", arr end program five_digit_numbers

19 program multi_record_example real :: a,b a = 12.25 b = 23.50 write(unit=6,fmt="(t10,a,3/,a,f6.2,a,f6.2,a"//"& f7.2,2/,a,f10.3)") & "Multi-record example",& " The sum of ",a," and",b," is", a+b,& " Their product is",a*b end program multi_record_example

20 program railway_time_table integer,parameter::n=5 integer::i real,dimension(n)::arrival,departure integer,dimension(n)::platform character(len=10),dimension(n)::destination arrival=(/9.23, 9.28, 10.41, 10.48, 11.15/) departure=(/9.25, 9.32, 10.53, 10.53, 11.18/) platform=(/2, 1, 3, 2, 1/) destination=(/"Edinburgh"," London","Sheffield",& "Newcastle"," London"/) write(unit=6,fmt="(/a/2x,7('='),3x,9('='),3x,8('='),3x,11('='))") & " Arrival Departure Platform Destination" do i = 1, n write(unit=6,fmt="(3x,f5.2,6x,f5.2,8x,i1,8x,a10)") & arrival(i),departure(i),platform(i),destination(i) end do end program railway_time_table

21 program test2 character(len=6)::a,b,c print *, "........................ STEPPER... Entering PROGRAM. Press Enter" print*,"program test2" read * print *, "........................ STEPPER... Before READ. Press Enter" print*,"read ""(a8,t1,a4,t1,a)"",a,b,c" read * read "(a8,t1,a4,t1,a)",a,b,c print *, "........................ STEPPER... After READ. Press Enter" read * print "(a8,tr2,a4,tr3,a)",a,b,c print*,"print ""(a8,tr2,a4,tr3,a)"",a,b,c" print *, "........................ STEPPER... After PRINT. Press Enter" read * print "(a,t10,a,tr2,a)",a,b,c print*,"print ""(a,t10,a,tr2,a)"",a,b,c" print *, "........................ STEPPER... After PRINT. Press Enter" read * print *, "........................ STEPPER... Before ENDPROGRAM. Press Enter" print*,"end program test2" read * end program test2


Download ppt "Week 9 More Control over INPUT and OUTPUT (I / O)."

Similar presentations


Ads by Google