1 More Control over INPUT and OUTPUT (I / O) The interface between the user and the computer Formats and edit descriptors Input editing Output editing.

Slides:



Advertisements
Similar presentations
Introduction to arrays
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Chapter 3 Program Design And Branching Structures.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Programming Logic and Design Fourth Edition, Introductory
Chapter 2 Basic Elements of Fortan
Chapter 2: Input, Processing, and Output
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Introduction to a Programming Environment
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
JavaScript, Fourth Edition
Understanding the Mainline Logical Flow Through a Program (continued)
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.
Chapter 3 Planning Your Solution
String Escape Sequences
Graphical Tree-Based Scientific Calculator: CalcuWiz Will Ryan Christian Braunlich.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
CIS Computer Programming Logic
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Homework Assignment #3 J. H. Wang Oct. 29, 2007.
FORTRAN FORmula TRANslator -Anand Trivedi. HISTORY  Designed and written from scratch in by an IBM team lead by John W. Backus as the first.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Flowcharts.
Introduction to Pascal The Basics of Program writing.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Visual Basic Programming
INPUT / OUTPUT STATEMENTS
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Week 9 More Control over INPUT and OUTPUT (I / O).
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Unit 2 Technology Systems
Arithmetic Expressions Function Calls Output
Chapter 2: Input, Processing, and Output
The Selection Structure
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Introduction to C++ Programming
Midterm Review Programming in Fortran
Problem Solving Skill Area 305.1
An Introduction to Programming with C++ Fifth Edition
Chapter 2: Input, Processing, and Output
DATA TYPES AND OPERATIONS
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

1 More Control over INPUT and OUTPUT (I / O) The interface between the user and the computer Formats and edit descriptors Input editing Output editing read, write and print statements More powerful formats

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 : , 2, 3, 4, 5, 6, 7, 8, and , 456, , 0.45, 67, 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 : read “ ( i 3, i 3, i 3 )”, n1, n2, n3 ! n1 = 123, n2 = 456, n5 = 789 read “ ( t 5, i 2, t 6, i 2, t 2, i 4 )”, x, y, z ! x = 56, y = 67, z = 2345 read “ ( f 9.4 )”, real_num ! real_num = read “ ( f 3.1, f 2.2, f 3.0 )”, r1, r2, r3 ! r1 = 12.3, r2 = 0.45, r3 =

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 s1 contains : s2 contains : s3 contains :

8 EXAMPLEs for formatting of an input real number in different ways using “ a – edit descriptor “ real number is : – e d E

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

12 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

13 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

14 EXAMPLE for “f” edit descriptor : x = y = z = 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## ### OUTPUT EDITING

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

16 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

17 Exercise 2 program test1 integer ::a,b real::c,d print*,"please enter an integer values which is more than 6 digit" read "(i4,t1,i4,t2,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

18 Exercise 3 program test2 character(len=6)::a,b,c Print*,”Please write your name” 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

19 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

20 program multi_record_example real :: a,b a = b = 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

21 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

22 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

23 program tabular_output real, parameter :: third = 1.0 / 3.0 real :: x integer :: i do i = 1, 10 x = i !print "(f15.4, f15.4, f15.4)", x,sqrt(x),x**third print "(3f15.4)", x,sqrt(x),x**third end do end program tabular_output

24 program aa character (len=50) :: a, b, c, d, e integer :: i, j a = "aaa bbbb ccccc dddddd eeeeeee" print *, a i = index(a," ")+1 b = a(:i-2) print *, b c = a(i:) print *, c j = index (c," ") +1 d = c(:j-2) print *, d e = d(j:) print *, e end program aa

25 program character_searc_function !examle for index, you can olso try scan and verify character (len=50) :: a, b, c, d, e integer :: i, j a = "bbb cccc ddddd eeeeee" print *, a i = index(a,"d") !Calculates how many chracter before and including "c" print*,"i",i b = a(:i-2) print *, b c = a(i:) print *, c j = index (c," ") +1 d = c(:j-2) print *, d e = d(j:) print *, e end program character_searc_function

26 ! Assignment statements with constant expressions. program C01 implicit none integer, parameter :: NAME_LENGTH = 21 real :: Avogadros_Number integer :: Many complex :: Z logical :: Flag character (len = NAME_LENGTH) :: Name ! start program C01 Many = ! Integer type Avogadros_Number = e23 ! Real type Z = (0.0, 1.75e8) ! Complex type Flag =.true. Name = " Mustafa " ! Character type, length 21 write (unit = *, fmt = *) Many, Avogadros_Number, Z, Flag, Name stop end program C01

27 ! Assignment statements with variable expressions. module C02M implicit none public :: Assign_2 contains subroutine Assign_2( ) ! Type declarations with initialization: real, save :: Y = 1.23, Pi = integer, save :: Counts = 173 character (len = 8), save :: I = " Optics " logical, save :: Flag =.true. ! Type declarations without initialization: real :: X, Theta integer :: Many character (len = 8) :: K logical :: Done ! start subroutine Assign_2 X = Y ! Real type Y = X Theta = Pi Many = Counts ! Integer type K = I ! Character type, length 8 Done = Flag ! Logical type write (unit = *, fmt = *) Y, Pi, X, Theta, Counts, Many, I, K, Flag, Done return end subroutine Assign_2 end module C02M program C02 use C02M implicit none ! start program C02 call Assign_2( ) stop end program C02

28 ! Assignment statements with arithmetic expressions. program C03 implicit none real, parameter :: GRAVITY = 9.8 real :: Old_Vol, Old_Press, Old_Temp, New_Vol, New_Press, New_Temp real :: Mass, Velocity, Kinetic_Energy, Pressure, Density, Height, Bernoulli integer, parameter :: J = 6 integer :: I ! start program C03 I = J + 1 write (unit = *, fmt = *) I read (unit = *, fmt = *) Old_Vol, Old_Press, Old_Temp, New_Press, New_Temp New_Vol = Old_Vol * (Old_Press / New_Press) * (New_Temp / Old_Temp) write (unit = *, fmt = *) Old_Vol, Old_Press, Old_Temp, New_Vol, New_Press, New_Temp read (unit = *, fmt = *) Mass, Velocity Kinetic_Energy = 0.5 * Mass * Velocity ** 2 write (unit = *, fmt = *) Mass, Velocity, Kinetic_Energy read (unit = *, fmt = *) Pressure, Density, Velocity, Height Bernoulli = Pressure + Density * (0.5 * Velocity ** 2 + GRAVITY * Height) write (unit = *, fmt = *) Pressure, Density, Velocity, Height, Bernoulli stop end program C03

29 program aa character (len=50) :: a, b, c, d, e integer :: i, j a = "aaa bbbb ccccc dddddd eeeeeee" print *, a i = index(a," ")+1 b = a(:i-2) print *, b c = a(i:) print *, c j = index (c," ") +1 d = c(:j-2) print *, d e = d(j:) print *, e end program aa program 0206 ! Asks the hour and the minute values of the ! present time and displays it as a sentence. integer :: hh,mm print *, " Input the hour, in the 24 hours format." read *, hh print *, " Input the minute." read *, mm print *, "" print *, " THE TIME IS ",mm," MINUTES AFTER ",hh end program 0206 ! Assignment statements with constant expressions. program C01 implicit none integer, parameter :: NAME_LENGTH = 21 real :: Avogadros_Number integer :: Many complex :: Z logical :: Flag character (len = NAME_LENGTH) :: Name ! start program C01 Many = ! Integer type Avogadros_Number = e23 ! Real type Z = (0.0, 1.75e8) ! Complex type Flag =.true. Name = " Mustafa Sezer" ! Character type, length 21 write (unit = *, fmt = *) Many, Avogadros_Number, Z, Flag, Name stop end program C01

30 program C02 use C02M implicit none ! start program C02 call Assign_2( ) stop end program C02 ! Assignment statements with variable expressions. module C02M implicit none public :: Assign_2 contains subroutine Assign_2( ) ! Type declarations with initialization: real, save :: Y = 1.23, Pi = integer, save :: Counts = 173 character (len = 8), save :: I = " Optics " logical, save :: Flag =.true. ! Type declarations without initialization: real :: X, Theta integer :: Many character (len = 8) :: K logical :: Done ! start subroutine Assign_2 X = Y ! Real type Y = X Theta = Pi Many = Counts ! Integer type K = I ! Character type, length 8 Done = Flag ! Logical type write (unit = *, fmt = *) Y, Pi, X, Theta, Counts, Many, I, K, Flag, Done return end subroutine Assign_2 end module C02M ! Assignment statements with arithmetic expressions. program C03 implicit none real, parameter :: GRAVITY = 9.8 real :: Old_Vol, Old_Press, Old_Temp, New_Vol, New_Press, New_Temp real :: Mass, Velocity, Kinetic_Energy, Pressure, Density, Height, Bernoulli integer, parameter :: J = 6 integer :: I ! start program C03 I = J + 1 write (unit = *, fmt = *) I read (unit = *, fmt = *) Old_Vol, Old_Press, Old_Temp, New_Press, New_Temp New_Vol = Old_Vol * (Old_Press / New_Press) * (New_Temp / Old_Temp) write (unit = *, fmt = *) Old_Vol, Old_Press, Old_Temp, New_Vol, New_Press, New_Temp read (unit = *, fmt = *) Mass, Velocity Kinetic_Energy = 0.5 * Mass * Velocity ** 2 write (unit = *, fmt = *) Mass, Velocity, Kinetic_Energy read (unit = *, fmt = *) Pressure, Density, Velocity, Height Bernoulli = Pressure + Density * (0.5 * Velocity ** 2 + GRAVITY * Height) write (unit = *, fmt = *) Pressure, Density, Velocity, Height, Bernoulli stop end program C03