5.1 Introduction to Read
5.2 Character Formats
5.3 Logical Formats
5.4 How to deal with Formatting Problems
5.5 Mixing Strings
5.6 What is Carriage Control
5.7 How to debug a program
PROGRAM P22
!
! INTRODUCTION TO READ
!
IMPLICIT NONE
INTEGER :: I,J,AGE,POUNDS,GRAMS
REAL :: DAYS
CHARACTER (LEN=15) :: NAME,ADDRESS
LOGICAL :: SINGLE
!
PRINT *, 'This is Program >> P22 - Read in some data'
!
! READ DATA
!
READ *,I
!
DO J=1,I
READ * ,AGE,POUNDS
READ * ,NAME,ADDRESS
READ * ,SINGLE
!
! EXECUTABLE STATEMENTS
!
GRAMS=POUNDS*254
DAYS=AGE*365.25
!
! PRINT DATA
!
PRINT *,'NAME AND ADDRESS :'
PRINT *, NAME,ADDRESS
PRINT *,'AGE AND NUMBER OF DAYS :'
PRINT *, AGE,DAYS
PRINT *,'POUNDS AND GRAMS :'
PRINT *, POUNDS,GRAMS
PRINT *,'SINGLE :'
PRINT *, SINGLE
PRINT *,' '
!
END DO
STOP
END PROGRAM P22
!
PROGRAM COMPARISON
!
IMPLICIT NONE
LOGICAL :: COMPAR
INTEGER :: I
PRINT *,2+2 = = 4, 5<=3, 1>=1
PRINT *,-1.4>-1.9, I /= 3, I = = 3.0
PRINT *, 'A' < 'B', 'JOHN ROE' < 'JOHN DOE'
COMPAR='LONG' >= 'LONGEST'
PRINT *,COMPAR
STOP
END PROGRAM COMPARISON
PROGRAM OUTPUT :
T F T
T F T
T F
F
2. FORMAT STATEMENT
The General form is :
label
FORMAT ( layout information )
where
label
is
a statement number from 1 to 99999
FORMAT
Keyword defining this nonexecutable
statement which can be placed anywhere in the program
layout information
is any combination of a number
of conversion types or edit descriptors which specify the kind
of variable and constant data and its location on the input record
(card) or output record (line).
The most used conversion types
can be summarized as follows.
General Conversion
Form | Type | |
nIw | INTEGER | |
nFw.d | REAL Fixed point form | |
nEw.d | REAL Exponent form | |
nGw.d | REAL or INTEGER (General form) | |
nLw | LOGICAL | |
nAw | CHARACTER | |
nX | Skip a number of columns | |
Tn | TAB to a given column | |
`...' | Output CHARACTER constant | |
r(...) | Repeat section in brackets |
...
READ 44,INTl,INT2,INT3
44 FORMAT (3I5)
PRINT 44,INTl,INT2,INT3
STOP
END PROGRAM READING
/DATA
432 333 1
OUTPUT :
432 333 1
...
READ 44,INTl,INT2,INT3
44 FORMAT (3(' ',I3))
PRINT 44,INTl,INT2,INT3
STOP
END PROGRAM READING2
/DATA
432 333 1
OUTPUT :
432 33 10
MISCELLANEOUS FORMATTING
1. Repeats
PRINT 15,2,'ABC',4.4,'DEF',2.13
15 FORMAT (' ',I3,2(' ',A3,'=',F7.2))
2 ABC= 4.40 DEF= 2.13
2. Multiple lines
PRINT 20,444,2.131
20 FORMAT (' ',I3/' ',F7.2)
444
2.13
3. Wrong number of arguments
PRINT 30,'SEP'
PRINT 30
PRINT 30,'OCT',31,'NOV',30
30 FORMAT (' ',A3, '=', I2)
SEP=
OCT=31
NOV=30
Operation | Result | ||
x = y | Left half of y => x | ||
x = y | y + 10 spaces => x | ||
x = 'HELLO' | HELLO' + 5 spaces => x | ||
PRINT *,x | 10 chars printed | ||
x == y (>, etc) | x compared to y + spaces | ||
READ 10,X | x only gets left | ||
10 FORMAT (Ay) | half of input string | ||
READ 10,X | x gets 10 characters | ||
10 FORMAT (Ay) | of input data + 10 spaces | ||
READ 10,X | 10 spaces + x | ||
10 FORMAT (' ',Ay) | printed | ||
READ 10,X | Leftmost 10 characters | ||
10 FORMAT (' ',Ay) | of x printed |
New page | ||
Single spacing | ||
Double spacing | ||
No spacing |
1) READ COMPLETE PROGRAM SLOWLY
FROM TOP TO BOTTOM
- Properly structured
- Proper header
- Proper syntax
- Variables appropriately declared
-
Blocks indented
2) COMPILE PROGRAM
- Any errors
- Any warnings
- Refer to ISN (Internal Statement Numbers)
- Clean compile
-
Missing routines at link
3) EXECUTION TIME ERRORS
- No output, add print statement
- ''Stuck in a loop'' = too much output !
1) GENERAL CONCEPT
- Your program is available in source form (e.g. FORTRAN, C, PASCAL, etc. ) and in BINARY.
- Your program is run under the control of debugger.
- Debugger allows :
- Inspection of data input/output
- Single step execution
- Breakpoints
- Tracing
- Animation
- Full speed execution
-
Array subscript checking
2) FORTRAN :
ELF90 - Essential LAHEY FORTRAN 90
3) C language :
A) Turbo C
B) Turbo Pascal version 6
Go back to lecture menu
Go back to main page