Introduction to Computers for Engineers - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Introduction to Computers for Engineers

Description:

Carriage Control Character. Function: Initializes Printer for output ... Keep for portability and to avoid problems if carriage control is encountered! ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 32
Provided by: Richard1096
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Computers for Engineers


1
Basic Fortran I/O Concepts
  • LECTURE OUTLINE
  • Free versus Directed I/O
  • Edit Descriptors
  • Carriage Control
  • Numeric Control
  • Character Control
  • Spacing Control
  • Repeat Specifier
  • Read and Write to Files
  • Examples !!
  • Assignment

2
Free vs. List Directed I/O
Free Formatting
  • A simplified method for input and output without
    detailed formatting instructions
  • Output variables and strings are sent to the
    output device without instructions. An asterisk
    is used in place of the format specifier
  • The computer formats the output according to
    internal rules
  • You specify the output, the computer specifies
    the format
  • Simple, but it provides NO flexibility
  • Free-format example

WRITE(,) 'PROJECTILE VELOCITY ', VF
3
List Directed Input and Output
  • Provides the flexibility the free formatting
    doesn't have
  • You specify the exact format of the output
  • Method is a bit tedious, but you have complete
    control
  • Utilizes a group of format descriptors
  • Example of List Directed output

WRITE(,20) VO, VF 20 FORMAT (' ', T15, 'INITIAL
PROJECTILE VELOCITY ', F10.4/ T15, 'FINAL
PROJECTILE VELOCITY ', F10.4)
4
Formatted Write Statements
Examples
WRITE (,25) Q, LOAD 100 FORMAT (' THE MASS AND
LOAD VALUES ARE ',F8.1,' AND ',F8.1)
  • You are not limited to the format statement, the
    specifier can occur in numerous configurations

WRITE (,30) Q, LOAD !STANDARD FORMAT
STATEMENT 30 FORMAT (T20,2F15.3) WRITE (,
'(T20,2F15.3)') Q, LOAD !FORMAT IN CHARACTER
CONSTANT CHARACTER(LEN35)TEXT !FORMAT IN
CHARACTER VARIABLE TEXT'(T20,2F15.3)' WRITE
(,TEXT) Q, LOAD
5
Format Descriptors
What do they describe?
  • The alignment of the output
  • The number of decimal places to be written
  • Spacing between the output, both horizontal and
    vertical
  • Horizontal alignment

Carriage Control
  • Carriage control is a nearly obsolete feature of
    Fortran, but is kept for maximum portability of
    your code.
  • What it does
  • Resets printer and announces a new line of text
  • Controls vertical spacing, i.e. single space,
    double space, new page.

6
Function Initializes Printer for
outputPosition First character in line to be
printed determines line spacing action.
Carriage Control Character
Start New Line (' ') Double Space ('0') New
Page ('1') No New Line ('')
Use on line printers to control paper feed and
vertical spacing.Laser printers ignore this
convention!!Keep for portability and to avoid
problems if carriage control is encountered!
7
Numeric Descriptors
Other Descriptors
8
c column numberd of digits to right of decimal
pointm min. of digits to be displayedn of
spaces to skipr repeat count-- of times to use
a descriptorw field width-- of characters to
use for I/O
Symbols Used with Format Descriptors
9
Function Provide format for all integer
outputFormat rIw or rIw.m
Integer Output -- The I Descriptor
Example Source Code
INTEGER A-25, B5, C-55, D11 WRITE (, 110)
A,A25,B,C,D WRITE (, 120) A,A25,B,C,D WRITE
(, 130) A,A25,B,C,D 110 FORMAT (' ',
5I8) !40 spaces total 120 FORMAT (' ', 2I8.0,
2I8, I10.6 ) !42 spaces total 130 FORMAT (' ',
2I8.3, 2I8, I5 ) !37 spaces total
Output
-25 0 5 -55 11
-25 5 -55 000011
-025 000 5 -55
11 Fiel
d Spacing Illustrator 1234567890123456789012345678
90123456789012
10
Function Provide format for all REAL
outputFormat rFw.d
Real Output -- The F Descriptor
Example Source CodeNote that space is required
for decimal points and negative signs. And, if
you want to separate data on the same row, best
to allow a few extra spaces!
REAL P-198.3, Q9.43, R787.4 WRITE (, 110)
P, Q, R WRITE (, 120) P, Q, R 110 FORMAT (' ',
3F7.3 ) 120 FORMAT (' ', 3F10.2 )
Output
9.430787.400 -198.30
9.43 787.40 F
ield Spacing Illustrator 1234567890123456789012345
678901
11
Function Provide exponential notation for all
REAL outputFormat rEw.d w ? d 7
Real Output -- The E Descriptor
Example Source Code
REAL N6.023E23, BW2.4E10, LIGHT3.0E8,NEG-8.
0 WRITE (, 100) N, BW, LIGHT, NEG WRITE (, 101)
N, BW, LIGHT, NEG WRITE (, 102) N, BW, LIGHT,
NEG 100 FORMAT (' ', 4E14.4 ) 101 FORMAT (' ',
4E10.4 ) 102 FORMAT (' ', 4E9.4 )
Output
0.6023E24 0.2400E11 0.3000E09
-0.8000E01 0.6023E240.2400E110.3000E09-.8000
E01 .6023E24.2400E11.3000E09

123456789012345678901234567890123456789012
34567890123456
12
Function Provide SCIENTIFIC notation for all
REAL outputFormat rESw.d w ? d 7
Real Output -- The ES Descriptor
Example Source Code
REAL N6.023E23, BW2.4E10, LIGHT3.0E8,NEG-8.
0 WRITE (, 100) N, BW, LIGHT, NEG 100 FORMAT ('
', 4ES14.4 )
Output
6.0230E23 2.4000E10 3.0000E08
-8.0000E00
123456789012345678901234
56789012345678901234567890123456
13
Function Provide horizontal spacing, one space
at a time. Does not work on ELF90Format
nX
Horizontal Positioning -- The X and T Descriptor
The X Descriptor
The T Descriptor
Function To advance to a specified field
space, similar to a tab character in word
processingFormat Tc ccolumn number
14
Horizontal Positioning -- The X and T Descriptor
X Descriptor Example
REAL MASS1.529, VOLUME0.65 DENSITYMASS/VO
LUME WRITE(, 110) WRITE(, 120) MASS, VOLUME,
DENSITY 110 FORMAT (' TEST RESULTS FROM
LABORATORY MEASUREMENT'/ 'MASS, KG VOLUME,
CC DENSITY,G/CC) 120 FORMAT ('
',F7.2,5X,F10.2,7X,F12.2) !Or, a better, easier,
and ELF90 compatible approach is below 120
FORMAT (' ',F7.2,F15.2,F19.2)
Output
TEST RESULTS FROM LABORATORY MEASUREMENT MASS,
KG VOLUME, CC DENSITY,G/CC 1.53
0.65 2.35
1234567890123456
7890123456789012345678901234567890
15
Horizontal Positioning -- The X and T Descriptor
T Descriptor Example
WRITE(,23) WRITE(,19) TEMP1, X1,
EXPAN1 WRITE(,19) TEMP2, X2, EXPAN2 WRITE(,19)
TEMP3, X3, EXPAN3 WRITE(,19) TEMP4, X4,
EXPAN4 WRITE(,19) TEMP5, X5, EXPAN5 23 FORMAT ('
', T4,'TEMP (oC)', T15, 'LENGTH OF BAR (CM)',
T35, 'EXPANSION ()') 19 FORMAT (' ', T5, F6.1,
T15, F9.3, T33, F9.3) STOP END
Output
TEMP (oC) LENGTH OF BAR (CM) EXPANSION ()
485.0 3.569 -0.000 492.0
3.521 -0.010 510.0
3.516 -0.015 516.0 3.501
-0.019 525.0 3.498
-0.020
12345678901234567890123456789012345678901
234567890
16
Function Provide format for all Character
outputFormat rAw
Character Output -- The A Descriptor
Example Source Code
CHARACTER(LEN50)COURSE COURSE 'INTRO TO
COMPUTING FOR ENG' WRITE(,100) COURSE 100
FORMAT (' ', A13) !PARTIAL
VARIABLE WRITE(,110) COURSE 110 FORMAT (' ',
A) !FULL VARIABLE
Output
INTRO TO COMP INTRO TO COMPUTING FOR
ENG Field Spacing
Illustrator 123456789012345678901234567890
17
Reading and Writing to Files
  • Places to store data --avoid reentry each time
    program is run
  • Save and store output
  • Fortran files are sequential access
  • Key I/O File Statements are

Using Files Some Essential commands
18
Unit Numbers
  • Unit Number Conventions and Practices
  • I/O number 5 is reserved for input from
    keyboard Fortran convention, Used at Rutgers
  • I/O number 6 is reserved for output to
    screen Fortran convention, Used at Rutgers
  • Pick any other number you like to name your
    files. 3, 28, 64, 156...
  • Your file will be named accordingly, i.e DATA.F03

19
Function To associate a file with a I/O
numberFormat OPEN (open_list)
The OPEN Statement
  • UNIT int_expr Any non-neg. integer except
    5 or 6
  • This indicates the I/O number to associate with
    this file
  • FILE char_expr Any name you want
  • STATUS char_expr Old, New, Replace, Scratch
  • ACTION char_expr Read and/or Write
  • IOSTAT int_var Not Input, the computer
    returns this one

20
Using Files with Fortran90
Fortran 90 has excellent file access capability.
One of the most important features is the
IOSTATclause feature, which enables you to trap
file access errors and prevent your program from
crashing during execution. An example of the
form for IOSTAT is READ (20,,IOSTATSTATUS) wh
ere the READ statement accesses data from a disk
file associated with the unit number 20, and
free-form I/O is utilized as represented by the
asterisk, . The system returns a value for
IOSTAT that indicates success or failure in
accessing the file.
21
  • ALWAYS include the IOSTATclause when reading
    from a disk file. This clause provides a graceful
    way to detect end-of-data conditions on the input
    file.
  • If the IOSTAT variable lt 0 after READ, then EOF
  • If the IOSTAT variable 0 after READ, then OK
  • If IOSTAT variable gt 0, then READ error

22
Using Files with Fortran90
  • The use of this construction is shown well in an
    example given by Chapman in the textbook. Refer
    to page 191, example 4-3.
  • The problem has two objectives
  • To read real values from a data file
  • To detect any errors and the end of file when it
    occurs.

23
Using Files with Fortran90
  • PSEUDOCODE!!
  • Get file name from user
  • Assign unit number and open the file
  • Check for errors on open
  • If no open error then read data
  • Continue to check IOSTAT for error flag as data
    is read
  • If error, exit with error message
  • If end of file, exit with completion message

24
Initialize nvals to 0 Prompt user for file
name Get the name of the input file OPEN the
input file Check for errors on OPEN IF no OPEN
error THEN ! Read input data WHILE
25
READ value IF status / 0 EXIT nvals lt-- nvals
1 WRITE valid data to screen END of WHILE !
Check to see if the WHILE terminated due to EOF !
Or READ error IF status gt 0 WRITE READ error
occurred on line, nvals
26
ELSE WRITE number of valid input values
nvals END of IF (no OPEN error) END PROGRAM
27
Using Files with Fortran90
THE ALGORITHM -- In Flowchart Format
Source Fortran 90/95 for Scientists and
Engineers by Stephen Chapman. Published by
McGraw Hill, 1998, page 193.
28
Example Problem 4-3, page 191
THE PROGRAM -- Setup program
PROGRAM READ IMPLICIT NONE ! DECLARE
VARIABLES CHARACTER(LEN20) FILENAME ! NAME
OF FILE TO OPEN INTEGER NVALS 0 ! NUMBER
OF VALUES READ INTEGER STATUS ! I/O
STATUS REAL VALUE ! THE REAL VALUE READ
IN ! GET THE FILE NAME AND ECHO IT BACK TO THE
USER. WRITE (,) 'PLEASE ENTER INPUT FILE NAME
' READ (,) FILENAME WRITE (,1000)
FILENAME 1000 FORMAT (' ','THE INPUT FILE NAME
IS ', A)
29
Example Problem 4-3, page 191
THE PROGRAM -- Open file and read values
! OPEN THE FILE AND CHECK FOR ERRORS ON
OPEN. OPEN (UNIT3, FILEFILENAME, STATUS' OLD
', ACTION'READ', IOSTATSTATUS ) OPENIF IF
( STATUS 0 ) THEN ! OPEN WAS OK. READ
VALUES. READLOOP DO READ (3,,IOSTATSTATUS)
VALUE ! GET NEXT VALUE IF ( STATUS / 0 )
EXIT ! EXIT IF NOT VALID NVALS NVALS
1 ! VALID INCREASE WRITE (,1010) NVALS,
VALUE ! ECHO TO SCREEN 1010
FORMAT (' ','LINE ', I6, ' VALUE ',F10.4) END
DO READLOOP
30
Example Problem 4-3, page 191
THE PROGRAM -- Read has terminated, report
why --Finish up
READIF IF ( STATUS gt 0 ) THEN !
A READ ERROR OCCURRED. TELL USER WRITE (,1020)
NVALS 1 1020 FORMAT ('0','AN ERROR OCCURRED
READING LINE ', I6) ELSE ! THE END OF THE DATA
WAS REACHED. TELL USER. WRITE (,1030)
NVALS 1030 FORMAT ('0','END OF FILE REACHED.
THERE WERE ', I6, ' VALUES IN THE
FILE.') END IF READIF ELSE OPENIF WRITE
(,1040) STATUS 1040 FORMAT (' ',' ERROR OPENING
FILE IOSTAT ', I6 ) END IF OPENIF !CLOSE
FILE CLOSE ( UNIT3 ) STOP END PROGRAM READ
31
Homework Assignment -- Week 4
  • Read Chapter 4 -- pp 156-206
  • Do problems
  • 3 (p. 209) -- Topic Getting Started
  • 9 (p. 210) -- Topic Real variable format
  • 13 (p. 211) -- Topic 24 h clock program
  • Assignments are due next week in lecture
Write a Comment
User Comments (0)
About PowerShow.com