CSCE150 Fortran Lab 7 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CSCE150 Fortran Lab 7

Description:

of digits after the point. 5. format descriptors. integer output: I ... Write a program to get two 3-digit integers from the user, print the sum and ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 16
Provided by: cse92
Category:
Tags: csce150 | digits | fortran | lab

less

Transcript and Presenter's Notes

Title: CSCE150 Fortran Lab 7


1
CSCE150 Fortran Lab 7
2
outline
  • Basic I/O
  • formatted output-WRITE
  • formatted input-READ
  • file processing
  • exercise 1
  • exercise 2 optional
  • exercise 3

3
Basic I/O
  • Formatted output
  • WRITE statement

4
Formatted output - WRITE
  • Example 2
  • INTEGERnum17, num25
  • WRITE(,200) num1,num2
  • 200 FORMAT(2I5)
  • Example 1
  • INTEGERnum7
  • WRITE(,100) num
  • 100 FORMAT (I5)

repeat count
integer descriptor
width
FORMAT statement holds the formatting info
  • Example 3
  • INTEGERnum17 REALnum25.31
  • WRITE(,300) num1,num2
  • 300 FORMAT(num1, I5, num2,F5.2)

real descriptor
of digits after the point
5
format descriptors
  • integer output I
  • real output F or E (exponential notation)
  • true scientific notation ES
  • logical output L
  • character output A
  • horizontal positioning X and T
  • page 193-new textbook
  • page 182-old textbook

5X insert 5 spaces
T6 go to column 5
6
Basic I/O
  • Formatted input
  • READ statement

7
Formatted input - READ
  • Example 2
  • INTEGERnum1, num2
  • READ(,200) num1,num2
  • 200 FORMAT(2I5)
  • Example 1
  • INTEGERnum
  • READ(,100) num
  • 100 FORMAT (3X,I3)

repeat count
width
skip the first 3
integer descriptor
FORMAT statement holds the formatting info
  • Example 3
  • INTEGERnum1 REALnum2
  • READ(,(I5, F5.2)) num1,num2

real descriptor
of digits after the decimal point
8
format descriptors
repeat count
field width
  • integer input I rIw
  • real input F rFw.d
  • logical input L rLw (T or F)
  • character input A rA rAw
  • horizontal positioning X and T
  • page 212-new textbook
  • page 200-old textbook

of digits after the decimal point
5X skip 5 columns
read the same data twice in two different
formats CHARACTER(len6)string INTEGERinput R
EAD(,(I6,T1,A6)) input, string
9
File processing
  • three steps of file processing
  • open associates a file with an i/o unit number
  • read or write
  • close closes a file and releases the i/o unit
    number

10
  • OPEN statement
  • open a file for input
  • OPEN(UNITunit_num, FILEfilename,
    STATUS'OLD',
  • ACTION'READ', IOSTATierror)
  • open a file for output
  • Open (UNITunit_num, FILEfilename, STATUS'NEW
    ',
  • ACTION'WRITE', IOSTATierror)
  • Open (UNITunix_num, FILEfilename,
    STATUS'REPLACE',
  • ACTION'WRITE', IOSTATierror)
  • unit_num an integer used to identify the file
  • STATUS
  • OLD file exists before opening (file reading)
  • NEW create a new file (file doesnt exist before
    opening)
  • REPLACE create a new file (if the file with the
    same name exist, then overwrite it)
  • ierror an integer error code
  • 0 success non-zero error

11
  • READ and WRITE to disk files
  • READ
  • OPEN(UNIT8, FILEinput.dat, STATUSOLD,
    IOSTATierror)
  • READ(8,) x,y,z
  • WRITE
  • OPEN(UNIT7, FILEoutput.dat, STATUSREPLACE,
  • IOSTATierror)
  • WRITE(7,100) x,y,z
  • 100 FORMAT(X,F10.2,Y,F10.2,Z,F10.2)
  • CLOSE
  • CLOSE(UNITunit_num)

12
exercise 1
  • PROGRAM table
  • IMPLICIT NONE
  • INTEGERi, square
  • WRITE(,100)
  • 100 FORMAT(Table of square)
  • WRITE(,200)
  • 200 FORMAT(number, 9X, square)
  • WRITE(,300)
  • 300 FORMAT(, 9X, )
  • DO i1,10
  • squareii
  • WRITE(,400) i, square
  • END DO
  • 400 FORMAT(I4, 12X, I4)

13
  • Rewrite the above program to add two new columns
    which are square_root and cube.

14
exercise 2-optional
  • Write a program to get two 3-digit integers from
    the user, print the sum and difference in a
    pretty way.

15
exercise 3
  • get 5 integer values from the user
  • calculate the cube of each value
  • write the cube values to a file (output.txt)
Write a Comment
User Comments (0)
About PowerShow.com