26
21 April, 2000 CS1001 Lecture 27 Final Examination Review

CS1001 Lecture 27

  • Upload
    murray

  • View
    36

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: CS1001 Lecture 27

21 April, 2000

CS1001 Lecture 27

• Final Examination Review

Page 2: CS1001 Lecture 27

21 April, 2000

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

Page 3: CS1001 Lecture 27

21 April, 2000

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

Page 4: CS1001 Lecture 27

21 April, 2000

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.

Page 5: CS1001 Lecture 27

21 April, 2000

Relational Operators

= =

/=

<=

>=

<

>

.EQ.

.NE.

.LE.

.GE.

.LT.

.GT.

Logical Operators

.NOT.

.AND.

.OR.

.EQV.

.NEQV.

Negation

Conjunction

Disjunction

Equivalence

Nonequivalence

Page 6: CS1001 Lecture 27

21 April, 2000

Selective Execution

• Normal IF Structure• Simple IF Structure

• Normal IF StructureIF (log_exp1 .AND. log_exp2) THEN

statements to be performed

ELSE IF (log_ex3 .OR. log_ex4) THENstatements to be performed

ELSEstatements to be performed

END IF

Page 7: CS1001 Lecture 27

21 April, 2000

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) THENEXIT

END IF

Page 8: CS1001 Lecture 27

21 April, 2000

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

Page 9: CS1001 Lecture 27

21 April, 2000

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_sizestatements to be performed

END DO

Page 10: CS1001 Lecture 27

21 April, 2000

DO-EXIT (While) Loop

DO

statements to be performed

IF (logical expression) EXIT

statements to be performed

END DO

Page 11: CS1001 Lecture 27

21 April, 2000

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

Page 12: CS1001 Lecture 27

21 April, 2000

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

Page 13: CS1001 Lecture 27

21 April, 2000

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)

Page 14: CS1001 Lecture 27

21 April, 2000

Internal Subroutines

• Invoked with a CALL StatementCALL MySubRoutine(iArg, rArg)

• Defined after CONTAINS statementSUBROUTINE MySubRoutine(iArg, rArg)

INTEGER, INTENT(IN) :: iArg

REAL, INTENT(OUT) :: rArg

INTEGER :: iLocalVariable

REAL :: rLocalVariable

statements to be performed

END SUBROUTINE MySubRoutine

Page 15: CS1001 Lecture 27

21 April, 2000

External Subroutines

• Invoked with a CALL StatementCALL MySubRoutine(iArg, rArg)

• Defined after END PROGRAM statementSUBROUTINE MySubRoutine(iArg, rArg)

INTEGER, INTENT(IN) :: iArg

REAL, INTENT(OUT) :: rArg

INTEGER :: iLocalVariable

REAL :: rLocalVariable

statements to be performed

END SUBROUTINE MySubRoutine

Page 16: CS1001 Lecture 27

21 April, 2000

Internal Functions

• Invoked with an equation or statementrValue = rMyFunction(iValue)

• Defined after the CONTAINS statementFUNCTION rMyFunction(iValue)

REAL :: rMyFunction

INTEGER, INTENT(IN) :: iValue

statements to be performed

rMyFunction = some value to return

END FUNCTION rMyFunction

Page 17: CS1001 Lecture 27

21 April, 2000

External Functions

• Invoked with an equation or statementrValue = rMyFunction(iValue)

• Declared as EXTERNALREAL, EXTERNAL :: rMyFunction

• Defined after the END PROGRAM statementFUNCTION rMyFunction(iValue)

REAL :: rMyFunction

INTEGER, INTENT(IN) :: iValue

statements to be performed

rMyFunction = some value to return

END FUNCTION rMyFunction

Page 18: CS1001 Lecture 27

21 April, 2000

Structures (not in exam)• Declare a structure

TYPE sVariable

REAL :: rVar1, rVar2

INTEGER :: iVar1, iVar2

END TYPE sVariable

• Declare an instance of the structureTYPE(sVariable) :: sInstance

• Reference the variables in itiItem = sInstance%iVar1

rPrice = sInstance%rVar2

Page 19: CS1001 Lecture 27

21 April, 2000

One-Dimension Array

• Declare and dimensionINTEGER, DIMENSION(1:9) :: iVar1, iVar2

REAL, DIMENSION(0:25) :: rVar1, rVar2

• Initialize whole arrayiVar1 = 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 subscriptiVar(8) = 25

rVar(19) = 17.49

Page 20: CS1001 Lecture 27

21 April, 2000

Multi-Dimension Array

• Declare and dimensionINTEGER,DIMENSION(1:9,1:3):: iVar1, iVar2

REAL, DIMENSION(0:25,1:5) :: rVar1, rVar2

• Initialize whole arrayiVar1 = 0

rVar1 = 9.99

• Reference elements using subscriptiVar(8,2) = 25

rVar(19,5) = 17.49

Page 21: CS1001 Lecture 27

21 April, 2000

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

Page 22: CS1001 Lecture 27

21 April, 2000

Creating a File

• The file is opened as a new file for writingOPEN(UNIT=12, FILE=“input.dat”, “NEW”, “WRITE”)

• Formatted data is written to the fileWRITE(12,40) iVar, rVar, cVar

40 FORMAT(1X, I5, F8.3, A25)

• The file is closedCLOSE(12)

Page 23: CS1001 Lecture 27

21 April, 2000

Reading from a File

• The file is opened as an old file for readOPEN(UNIT=13, FILE=“input.dat”, “OLD”, “READ”)

• Formatted data is read from the fileREAD(13,40,IOSTAT=EOF) iVar, rVar, cVar

40 FORMAT(1X, I5, F8.3, A25)

• The file is closed– CLOSE(13)

Page 24: CS1001 Lecture 27

21 April, 2000

Internal Files

• Used to convert character form to integergiven 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

Page 25: CS1001 Lecture 27

21 April, 2000

Text Processing

• Getting the length of a stringVar = LEN(String)

Var = LEN(“Hello, my name is”) => 17

• Finding text within a stringFound = INDEX(String, Text)

Found = INDEX(“IMAGINE”, “IN”) => 5

Page 26: CS1001 Lecture 27

21 April, 2000

Implied DO Loops

• Used to read data into an arrayREAD (10, *) (iInArr(I), I=1, iLastNum)

• Equivalent toDO I = 1, iLastNum

READ (10, *) iInArr(I)

END DO