CS1001 Lecture 5 - PowerPoint PPT Presentation

About This Presentation
Title:

CS1001 Lecture 5

Description:

PRINT *, output-list where output-list is: output-list can contain multiple items ... So far, free form READ and PRINT -- input from keyboard and output to display ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 23
Provided by: plum1
Learn more at: http://web.cs.wpi.edu
Category:
Tags: cs1001 | lecture | print

less

Transcript and Presenter's Notes

Title: CS1001 Lecture 5


1
CS1001 Lecture 5
  • Completion of Lecture 4
  • Talked about Data Types Arithmetic Errors
  • Continue with Operations and Functions
  • Program Testing Tips
  • File Input and Outputs

2
Data Types In Fortran
  • Integer (0, 137, -2516, 17745)
  • Real or Single Precision (0.0, -0.01536, 123.4,
    1.234E2, -1.536E-2)
  • Double Precision (1.234D-6, 0.1111111D3)
  • Complex (34i)
  • Character Strings (A, JOHN DOE)
  • Logical (.TRUE., .FALSE.)

3
Arithmetic Operations
  • Binary Unary
  • Addition positive
  • - Subtraction - negative
  • Multiplication e.g., 5.0 (-1)
  • / Division
  • Exponentiation
  • Precedence Rules
  • 0. Parenthesis
  • 1. Exponentiation, right to left
  • 2. Multiplication and Division, left to right
  • 3. Addition and subtraction, left to right

4
Operations
  • Operations result in same type as operands
  • 6/5 1 integer operation (Division of integers
    produces an integer)
  • 6.0/5.0 1.2
  • Mixed-Mode expressions -- combine integer and
    real data
  • e.g. 1.0/4 gt 1.0/4.0 gt 0.25
  • 3.08/5 gt 3.01 gt 3.01.0 gt 4.0
  • 3.08.0/5 gt 3.01.6gt4.6
  • Generally poor practice

5
Assignment Statement
  • variable expression
  • e.g., Height 0.5accelerationtime2
  • initialvelocitytimeinit
    ialheight
  • Velocityaccelerationtimeinitialvelocity
  • NOT an algebraic equality, but a replacement
    statement
  • sum sum x means add the value of x to the
    value of sum and store the result in sum.

6
Arithmetic Functions
  • Perform standard operations on variables which
    are referenced as an argument
  • Standard arithmetic functions do not have to be
    declared, merely used (see page 60 of text)
  • Examples COS(X), SQRT(X), LOG(X)
  • PROGRAM MiddleOfRange
  • Print , enter 10 measures
  • Read , x1,x2,x3,x4,x5,x6,x7,x8,x9,x10
  • MidRange ( MAX(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)
  • MIN(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10) )
    / 2.
  • Print , Mid Range is , MidRange
  • END PROGRAM MiddleOfRange

7
PROGRAM QuadraticEquations_1 !--------------------
----------------------- ! Program to solve a
quadratic equation ! using the quadratic
formula. ! Variables used are ! A, B, C
the coefficients of !
the quadratic equation ! Discriminant the
discriminant, ! B2 -
4.0AC ! Root_1, Root_2 the two roots of
the ! equation ! ! Input
The coefficients A, B, and C ! Output The two
roots of the equation or ! the
(negative) discriminant and a ! message
indicating that there are ! no real
roots !-------------------------------------------
--
8
IMPLICIT NONE REAL A, B, C, Discriminant,
Root_1, Root_2 ! Get the coefficients PRINT
, "Enter the coefficients of the
quadratic equation" READ , A, B, C !
Calculate the discriminant Discriminant B2
- 4.0AC
9
Preview of next lecture
Control flow
Sequential execution
Statement_1
Statement_1
Statement_2
If
.
.
.
Statement_n
Statement_2
Statement_3
Statement_4
10
! Check if discriminant is nonnegative. If !
it is, calculate and display the roots. !
Otherwise display the value of the !
Discriminant and a no-real-roots message. IF
(Discriminant gt 0) THEN Discriminant
SQRT(Discriminant) Root_1 (-B
Discriminant) / (2.0 A) Root_2 (-B -
Discriminant) / (2.0 A) PRINT , "The
roots are", Root_1, Root_2 ELSE PRINT ,
"Discriminant is", Discriminant PRINT ,
"There are no real roots" END IF END PROGRAM
QuadraticEquations_1
11
Consider Hungarian Notation
  • iName for INTEGER variables/constants
  • rName for REAL
  • dName for DOUBLE PRECISION
  • xName for COMPLEX
  • cName for CHARACTER strings
  • bName for LOGICAL (boolean)
  • Constants are all caps except type (rVAL)

12
Simple List-Directed Output
  • PRINT , output-list where output-list is
  • output-list can contain multiple items
  • A constant such as ENTER TIME
  • A variable such as OUTPUT
  • A formula or function such as SQRT(INP)
  • The indicates that the output format is
    free-form, or whatever the memory holds for that
    value, which may be truncated
  • e.g., PRINT , Square root of , x, ,
    SQRT(x)
  • Square root of 4.00 2.00

13
Simple List-Directed Input
  • READ , input-list, where input-list is
  • A variable or variables
  • The user should be prompted for all inputs with a
    PRINT statement, and units and/or ranges should
    also be specified to the user inputing the data

14
File I/O (Input/Output)
  • So far, free form READ and PRINT -- input from
    keyboard and output to display
  • For input and output of larger amount of data,
    file I/O may be used.
  • First, must let computer know what the files are
  • OPEN (UNITunit_number,FILEfile_name,STATUSstatu
    s)
  • unit_number is an integer to be used by READ and
    WRITE
  • file_name is string of the name of file
  • status is string OLD for existing files or
    NEW for new file
  • e.g., OPEN(UNIT10,FILEinput_data,STATUSOLD
    )
  • OPEN(UNIT11,FILEout_data,STATUSNEW)

15
File I/O
  • Input
  • READ (unit_number,) input_list
  • unit_number is digit specified in OPEN statement
  • e.g., File input_data contains three resistance
    values and one voltage value
  • 1.0, 1.0, 1.0
  • 6.0
  • OPEN(UNIT10,FILEinput_data,STATUSOLD)
  • .
  • READ(10,) resistance_1, resistance_2,
    resistance_3
  • READ(10,) voltage

16
File I/O
  • Output
  • WRITE (unit_number,) output_list
  • unit_number is digit specified in OPEN statement
  • e.g., File current_data is the output file
  • OPEN(UNIT11,FILEcurrent_data,STATUSNEW)
  • .
  • WRITE(11,) The current is, current, amps
  • File current_data is created and contains the
    line
  • The current is 18.0000000 amps

17
  • PROGRAM Projectile_2
  • !------------------------------------------------
  • ! This program calculates the velocity and height
    ! of a projectile given its initial height,
  • ! initial velocity, and constant acceleration.
  • ! Identifiers used are
  • !InitialHeightinitial height of projectile
    (meters)
  • !Height height at any time (meters)
  • !InitialVelocity initial vertical velocity
    (m/sec)
  • !Velocity vertical velocity at any time
    (m/sec)
  • !Accelerationconstant vertical acceleration
  • ! (m/sec/sec)
  • !Time time since launch (seconds)
  • !
  • ! Input (file) InitialHeight, InitialVelocity,
    Time
  • ! Output (file) Velocity, Height
  • !-------------------------------------------------
    ----

18
Specification
IMPLICIT NONE REAL InitialHeight, Height,
InitialVelocity, Velocity, Time REAL,
PARAMETER Acceleration -9.80665 ! Open
disk files FIG2-6.DAT and FIG2-6.OUT OPEN(UNIT
12, FILE "FIG2-6.DAT", STATUS "OLD")
OPEN(UNIT 13, FILE "FIG2-6.OUT", STATUS
"NEW") ! Read values for InitialHeight,
InitialVeloc, ! and Time READ (12, )
InitialHeight, InitialVelocity, Time !
Calculate the height and velocity Height 0.5
Acceleration Time 2
InitialVelocity Time InitialHeight Velocity
Acceleration Time InitialVelocity
Execution
19
! Write values of Time, Velocity, and Height !
to FIG2-6.OUT WRITE (13, ) "At time", Time,
"seconds" WRITE (13, ) "the vertical velocity
is", Velocity, "m/sec" WRITE (13, )
"and the height is", Height, "meters" ! Signal
user that program is finished PRINT , "
Program is finished " END PROGRAM
Projectile_2
20
Arithmetic Errors
  • Overflow and underflow errors
  • 8 bit exponent restricts range to about 38 to -38

10(-38)
0
-10-38
-1038
1038
overflow
overflow
underflow
21
Roundoff errors
  • Due to binary representation and finite number of
    bits
  • (AB)C (1000. 0.4)0.3 1000.7
  • (AB) gt 1000. 0.4000 gt 1000.
  • The result of (AB) added to C gt 1000. 0.3000
    gt 1000.
  • A (BC) 1000. ( 0.4000 0.3000)
  • (BC) .4000 .3000 gt .7000
  • Add A to (BC) gt 1000. .7000 gt 1001.
    (rounding up)
  • (AB) C A (BC)

22
Program Testing Tips
  • Boundary/bounds check all inputs
  • 1-100 input range, check 1, 100, 101, 0
  • Check upper and lower case if applicable
  • Quadrant test in all four quadrants
  • 45, 135, 225, 315 degrees
  • Type test data inputs
  • Enter 10.5 for an INT, 56 for a REAL, etc.
  • Have someone else test your program, and
    reciprocate
Write a Comment
User Comments (0)
About PowerShow.com