![Page 1: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/1.jpg)
21 April, 2000
CS1001 Lecture 27
• Final Examination Review
![Page 2: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/2.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/3.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/4.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/5.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/6.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/7.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/8.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/9.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/10.jpg)
21 April, 2000
DO-EXIT (While) Loop
DO
statements to be performed
IF (logical expression) EXIT
statements to be performed
END DO
![Page 11: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/11.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/12.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/13.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/14.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/15.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/16.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/17.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/18.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/19.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/20.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/21.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/22.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/23.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/24.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/25.jpg)
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: 21 April, 2000 CS1001 Lecture 27 Final Examination Review](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7d5503460f94a6052b/html5/thumbnails/26.jpg)
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