23 march, 2000 cs1001 lecture 5 completion of lecture 4 –talked about data types & arithmetic...

22
23 March, 2000 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

Post on 19-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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

Page 2: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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 (3+4i)• Character Strings (‘A’, ‘JOHN DOE’)• Logical (.TRUE., .FALSE.)

Page 3: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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

Page 4: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

Operations

• Operations result in same type as operands6/5 = 1 integer operation (Division of integers produces

an integer)

6.0/5.0 = 1.2

• Mixed-Mode expressions -- combine integer and real datae.g. 1.0/4 => 1.0/4.0 => 0.25

3.0+8/5 => 3.0+1 => 3.0+1.0 => 4.0

3.0+8.0/5 => 3.0+1.6=>4.6

• Generally poor practice

Page 5: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

Assignment Statement

variable = expression

e.g., Height = 0.5*acceleration*time**2 &

+ initialvelocity*time+initialheight

Velocity=acceleration*time+initialvelocity• NOT an algebraic equality, but a replacement

statementsum = sum + x means add the value of x to the value of

sum and store the result in sum.

Page 6: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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

Page 7: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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, ! B**2 - 4.0*A*C! 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!---------------------------------------------

Page 8: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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 = B**2 - 4.0*A*C

Page 9: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

Preview of next lecture

Sequential execution

Statement_1

Statement_2

Statement_n

.

..

Statement_1

Control flow

If

Statement_2 Statement_3

Statement_4

Page 10: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

! 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 >= 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

Page 11: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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)

Page 12: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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

Page 13: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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

Page 14: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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 (UNIT=unit_number,FILE=file_name,STATUS=status)

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(UNIT=10,FILE=“input_data”,STATUS=“OLD”)

OPEN(UNIT=11,FILE=“out_data”,STATUS=“NEW”)

Page 15: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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(UNIT=10,FILE=“input_data”,STATUS=“OLD”)

….

READ(10,*) resistance_1, resistance_2, resistance_3

READ(10,*) voltage

Page 16: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

File I/O

• OutputWRITE (unit_number,*) output_list

unit_number is digit specified in OPEN statement

e.g., File “current_data” is the output file

OPEN(UNIT=11,FILE=“current_data”,STATUS=“NEW”)

….

WRITE(11,*) “The current is”, current, “amps”

File current_data is created and contains the line

The current is 18.0000000 amps

Page 17: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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:

!InitialHeight:initial height of projectile (meters)

!Height :height at any time (meters)

!InitialVelocity : initial vertical velocity (m/sec)

!Velocity :vertical velocity at any time (m/sec)

!Acceleration:constant vertical acceleration

! (m/sec/sec)

!Time : time since launch (seconds)

!

! Input (file): InitialHeight, InitialVelocity, Time

! Output (file): Velocity, Height

!-----------------------------------------------------

Page 18: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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

Specification

Execution

Page 19: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

! 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

Page 20: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

Arithmetic Errors

• Overflow and underflow errors– 8 bit exponent restricts range to about 38 to -38

0-10**-38-10**38

overflow overflow

10**3810**(-38)

underflow

Page 21: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

Roundoff errors

• Due to binary representation and finite number of bits(A+B)+C = (1000. + 0.4)+0.3 = 1000.7

(A+B) => 1000. + 0.4000 => 1000.

The result of (A+B) added to C => 1000. + 0.3000 => 1000.

A + (B+C) = 1000. +( 0.4000 +0.3000)

(B+C) = .4000 + .3000 => .7000

Add A to (B+C) => 1000. + .7000 => 1001. (rounding up)

(A+B) + C = A + (B+C)

Page 22: 23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing

23 March, 2000

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