1 / 26

CS1001 Lecture 27

CS1001 Lecture 27. Final Examination Review. From Before the Midterm. Basic Program Elements Declaration of Variables Declaration of Parameters Initialization of Variables Logical Operators Selective Execution CASE Statements Repetitive Execution How to Prompt for an Input.

murray
Download Presentation

CS1001 Lecture 27

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 27 • Final Examination Review

  2. From Before the Midterm • Basic Program Elements • Declaration of Variables • Declaration of Parameters • Initialization of Variables • Logical Operators • Selective Execution • CASE Statements • Repetitive Execution • How to Prompt for an Input

  3. Basic Program Elements • Every program must have: • PROGRAM ProgramName • IMPLICIT NONE • END PROGRAM ProgramName Declaration of Variables • REAL :: rVar1, rVar2 • INTEGER :: iVar1, iVar2 • CHARACTER(LEN) :: cVar1, cVar2*LEN • LOGICAL ::bVar

  4. Declaration of Parameters • REAL, PARAMETER :: rCONS = 3.14159 • INTEGER, PARAMETER :: iCONS = 5240 Initialization of Variables • rVar1 = 1.23 • iVar2 = 2240 • cVar3 = “Hello world” • bVar4 = .TRUE.

  5. Relational Operators = = /= <= >= < > .EQ. .NE. .LE. .GE. .LT. .GT. • Logical Operators • .NOT. • .AND. • .OR. • .EQV. • .NEQV. • Negation • Conjunction • Disjunction • Equivalence • Nonequivalence

  6. Selective Execution • Normal IF Structure • Simple IF Structure • Normal IF Structure IF (log_exp1 .AND. log_exp2) THEN statements to be performed ELSE IF (log_ex3 .OR. log_ex4) THEN statements to be performed ELSE statements to be performed END IF

  7. Simple IF Statement IF (logical_expression) stmt e.g., IF (logical_expression) EXIT Used to get out of an infinite DO loop Equivalent to: IF (logical_expression) THEN EXIT END IF

  8. CASE Constructs SELECT CASE (iItem) CASE (:0) statements to perform if iItem <= 0 CASE (1) statements to perform if iItem = 1 CASE (2:5) statements to perform if iItem is 2-5 CASE (9:) statements to perform if iItem >= 9 CASE DEFAULT statements to perform for other values END SELECT

  9. Repetitive Execution • Counter-controlled loops perform an operation a given number of times • DO-EXIT or while or until loops perform an operation until a condition becomes true that causes the loop to end • Counter-Controlled DO iLoop = init_val, final_val, step_size statements to be performed END DO

  10. DO-EXIT (While) Loop DO statements to be performed IF (logical expression) EXIT statements to be performed END DO

  11. Prompting for Input PRINT *, “Enter an integer from 1 to 9: ” READ *, iInput PRINT *, “Enter a value 0.0 or greater: ” READ *, rInput PRINT *, “Enter Q to quit: ” READ *, cInput

  12. Stuff Since the Midterm • More Basic Program Elements • Subprograms - Subroutines, Functions • Structures • One-Dimension Arrays • Implied DO Loops • Multi-Dimension Arrays • Formatted Output • Text Processing • Internal Files • How to Create a File • How to Read from a File

  13. More Basic Program Elements • Programs containing subprograms must contain the following: PROGRAM MyProgram : CONTAINS subprograms go here (internal) : END PROGRAM MyProgram subprograms go here (external)

  14. Internal Subroutines • Invoked with a CALL Statement CALL MySubRoutine(iArg, rArg) • Defined after CONTAINS statement SUBROUTINE MySubRoutine(iArg, rArg) INTEGER, INTENT(IN) :: iArg REAL, INTENT(OUT) :: rArg INTEGER :: iLocalVariable REAL :: rLocalVariable statements to be performed END SUBROUTINE MySubRoutine

  15. External Subroutines • Invoked with a CALL Statement CALL MySubRoutine(iArg, rArg) • Defined after END PROGRAM statement SUBROUTINE MySubRoutine(iArg, rArg) INTEGER, INTENT(IN) :: iArg REAL, INTENT(OUT) :: rArg INTEGER :: iLocalVariable REAL :: rLocalVariable statements to be performed END SUBROUTINE MySubRoutine

  16. Internal Functions • Invoked with an equation or statement rValue = rMyFunction(iValue) • Defined after the CONTAINS statement FUNCTION rMyFunction(iValue) REAL :: rMyFunction INTEGER, INTENT(IN) :: iValue statements to be performed rMyFunction = some value to return END FUNCTION rMyFunction

  17. External Functions • Invoked with an equation or statement rValue = rMyFunction(iValue) • Declared as EXTERNAL REAL, EXTERNAL :: rMyFunction • Defined after the END PROGRAM statement FUNCTION rMyFunction(iValue) REAL :: rMyFunction INTEGER, INTENT(IN) :: iValue statements to be performed rMyFunction = some value to return END FUNCTION rMyFunction

  18. Structures (not in exam) • Declare a structure TYPE sVariable REAL :: rVar1, rVar2 INTEGER :: iVar1, iVar2 END TYPE sVariable • Declare an instance of the structure TYPE(sVariable) :: sInstance • Reference the variables in it iItem = sInstance%iVar1 rPrice = sInstance%rVar2

  19. One-Dimension Array • Declare and dimension INTEGER, DIMENSION(1:9) :: iVar1, iVar2 REAL, DIMENSION(0:25) :: rVar1, rVar2 • Initialize whole array iVar1 = 0 iVar2 = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9 /) rVar1 = 9.99 rVar2 = (/1.0, 1.1, 1.2, 1.3, …, 1.25 /) • Reference elements using subscript iVar(8) = 25 rVar(19) = 17.49

  20. Multi-Dimension Array • Declare and dimension INTEGER,DIMENSION(1:9,1:3):: iVar1, iVar2 REAL, DIMENSION(0:25,1:5) :: rVar1, rVar2 • Initialize whole array iVar1 = 0 rVar1 = 9.99 • Reference elements using subscript iVar(8,2) = 25 rVar(19,5) = 17.49

  21. Formatted Output PRINT 20, iVar, rVar, cVar 20 FORMAT(1X, I2.2, 5X, F8.3, T20, A) • 1X, 5X - horizontal spaces • I2.2 - Integer, width of 2, minimum 2 digits • F8.3 - Real, width of 8 (including sign and decimal point), with 3 decimal places • T20 - Tab to absolute column position - column 20 • A - Alphabetic string

  22. Creating a File • The file is opened as a new file for writing OPEN(UNIT=12, FILE=“input.dat”, “NEW”, “WRITE”) • Formatted data is written to the file WRITE(12,40) iVar, rVar, cVar 40 FORMAT(1X, I5, F8.3, A25) • The file is closed CLOSE(12)

  23. Reading from a File • The file is opened as an old file for read OPEN(UNIT=13, FILE=“input.dat”, “OLD”, “READ”) • Formatted data is read from the file READ(13,40,IOSTAT=EOF) iVar, rVar, cVar 40 FORMAT(1X, I5, F8.3, A25) • The file is closed • CLOSE(13)

  24. Internal Files • Used to convert character form to integer given cDate = “November 25,1997” then cYear = cDate(14:17) and READ (UNIT = cYear, FMT = ‘(I4)’) iYear • Assigns the integer value 1997 to integer variable iYear

  25. Text Processing • Getting the length of a string Var = LEN(String) Var = LEN(“Hello, my name is”) => 17 • Finding text within a string Found = INDEX(String, Text) Found = INDEX(“IMAGINE”, “IN”) => 5

  26. Implied DO Loops • Used to read data into an array READ (10, *) (iInArr(I), I=1, iLastNum) • Equivalent to DO I = 1, iLastNum READ (10, *) iInArr(I) END DO

More Related