1 / 22

CS1001 Lecture 5

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. Data Types In Fortran. Integer (0, 137, -2516, +17745)

kmaselli
Télécharger la présentation

CS1001 Lecture 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 (3+4i) • 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 => 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

  5. 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 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, ! 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 !---------------------------------------------

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

  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 >= 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-listwhere 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 (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”)

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

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

  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) -10**38 -10**-38 0 10**38 overflow overflow underflow

  21. 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)

  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

More Related