Title: The Exam Structure
1The Exam Structure
- 8 questions plus 1 extra credit question
- Total possible points to be earned 105 (100
regular plus 5 extra credit) - Three questions from your midterm exam!
- Q112 Q212 Q310 Q46 Q510 Q610 Q720
Q820 Q9 (EC)5 (you have seen this one before!) - 5 find/correct the errors in logic and syntax
(similar to questions on the midterm) 2
definition questions (w/examples) 1 write a
program question (similar to hyperbolic sine
question on midterm) 1 predict the output
question
2Focus on These Topics
- Open/close read/write files file status
- Arrays
- Pointers
- Module and subroutine logicrelationship to main
program - Implicit versus explicit interface
- Statistical error and weighted average
- Review your midterm examination very
carefullyyou never know what may show up on the
final!
3Reading and Writing to Files
- Places to store data --avoid reentry each time
program is run - Save and store output
- Fortran files are sequential access
- Key I/O File Statements are
Using Files Some Essential commands
4Function To associate a file with a I/O
numberFormat OPEN (open_list)
The OPEN Statement
- Open_list options
- UNIT int_expr Any integer except 5 or 6
- FILE char_expr Any name you want
- STATUSchar_expr Old, New, Replace
- ACTIONchar_expr Read or Write
- IOSTATint_var Not Input, the computer
returns this one - Open successful, then IOSTAT0
- EOF on READ, then IOSTAT lt 0
- Other Error, IOSTAT gt 0 Error Code
5Using Files with Fortran90
Fortran 90 has excellent file access capability.
One of the most important features is
the IOSTATclause feature which enables you to
trap file access errors and prevent your program
from crashing during execution. An example of
the form for IOSTAT is READ (20,,IOSTATSTATUS)
where the READ statement accesses data from the
disk file associated with the unit number 20, and
free-form I/O is utilized as represented by the
asterisk, . The system returns a value for
IOSTAT that indicates success or failure in
accessing the file.
6Mechanics of Arrays
- Declaration
- Before using, must declare
- Size and Type
- Declaration statement before any executable
statements -
Example Comments REAL, DIMENSION (110)
X (X is a real array with 10 elements) REAL,
DIMENSION (10) X (Same as previous example
but lower limit is1 by default) REAL
X(10) (Shorthand carryover from older
versions of Fortran. Very simple and
easy to use. Same result as previous
two examples) INTEGER, DIMENSION (110)
A, B (A and B are both integer arrays with
10 elements each) REAL X(510),
Y(20) (Declares X and Y as real with 16
and 20 elements, respectively) INTEGER
AMT, M(10) (Can declare scalar variables and
arrays in the same declaration
statement)
7Array Constructors
- For assigning constants to arrays
INTEGER, DIMENSION(8)ARRAYARRAY(/2,4,6,8,10,12
,14,16/)
PROGRAM ARRAY IMPLICIT NONE INTEGERI REAL,DIMENS
ION(10)X X(/(I,I1,50,5)/) WRITE(,)
X STOP END PROGRAM ARRAY
8Multi-Dimensioned Arrays
- Types
- Rows of Data gt 1-D Array -- Discussed
- Tables of Data gt 2-D Array
- Pages of Tables gt 3-D Array
- 2-D Arrays
- Two degrees of freedom in a table
- Row
- Column
Freq (I, J) Number of seniors in Ith major with
J credits of computer science courses
9Two-Dimensional Arrays
- Collecting Data from the Student Array
- Biology students with 6 credits of computer
- FREQ (3,3) FREQ (ROW 3, COLUMN 3) 189
- Total non-programmers
- NPRG FREQ (1, 1) FREQ (2, 1) FREQ (3,1)
- FREQ (4, 1) 155
NPRG0.0DO I 1, 4NPRGNPRGFREQ (I, 1)END DO
10Mechanics of 2-D Arrays
- Declaration
- Similar to declaring one dimensional arrays
- Example
- REALTEMP(15,180), WORK(125, 29)
- INTEGERSTEP(10,10)
- Input/Output with DO Loops -- nested do loops
- DO I1,IMAXVALUE
- DO J1,JMAXVALUE
- WRITE(,) TEMP(I,J)
- END DO
- END DO
- Input/Output with Implied DO Loops
- WRITE(,) ((WORK(I,J),J1,29),I1,125)
11Whats a Pointer?
- A variable that contains the address of another
variable, i.e. one that points to another.
Pointer Variable
Ordinary Variable
Address in Memory of a Variable
Data Value of the Variable
12The Necessary Declarations!
- Implement by declaring Pointers and Targets
- For scalar variables
- REAL, POINTER ALKSER
- For array variables (deferred-shape array
specification) - REAL, DIMENSION(), POINTER ALKSER
- INTEGER, DIMENSION(,), POINTER ALKENE
- Note that rank of array is declared, but extent
is not!
13The Necessary Declarations!
- Pointer targets must be declared
- REAL, TARGET A1 250.
- INTEGER, DIMENSION(,), TARGET JACOBMAT
- Why declare the target??
- In C you dont have to!!
- But, Fortran is faster, via Optimized compiler
function - Some variables get eliminated in optimization
process - TARGET declaration tells compiler not to eliminate
14Pointer Assignments Statement
- General Format
- POINTER gt TARGET
- After assignment, any reference to pointer
variable will actually reference the target
PROGRAM POINTDEMO IMPLICIT NONE ! DECLARE
POINTERS AND TARGETS REAL, POINTER SERC REAL,
TARGET ROOM111 50., ROOM118 75. ! ASSOCIATE
POINTER WITH TARGET SERC gt ROOM111 WRITE (,)
'SERC, ROOM111, ROOM118 ', SERC, ROOM111,
ROOM118 ! PRINT OUT THE VALUES FOR THE POINTER
AND THE TWO TARGETS SERC gt ROOM118 WRITE (,)
'SERC, ROOM111, ROOM118 ', SERC, ROOM111,
ROOM118 STOP END PROGRAM POINTDEMO D\Arutrl\Cl\C
OMPUTER\Lecture\Book\ELFproggtPOINTDEMO SERC,
ROOM111, ROOM118 50.0000 50.0000
75.0000 SERC, ROOM111, ROOM118 75.0000
50.0000 75.0000
15Pointing to Pointers
- You Can Use a Pointer to Point to another Pointer
- POINTER1 gt POINTER2
- After this assignment, pointer1 points to the
same address as pointer2 - If the pointer2 becomes disassociated (NO longer
points to a target), then pointer1 is also
disassociated
16Pointing to Pointers
PROGRAM ASSIGNMENT IMPLICIT NONE ! DECLARE
POINTERS AND TARGETS REAL, POINTER SERC,
ALLISON REAL, TARGET ROOM111 50., ROOM118
75. ! ASSIGN POINTER TO TARGET SERC gt
ROOM111 ! ASSIGN POINTER TO POINTER ALLISON gt
SERC ! PRINT OUT POINTERS AND TARGET VALUES WRITE
(,'(A,4F12.1)') ' SERC, ALLISON, ROOM111,
ROOM118', SERC, ALLISON, ROOM111, ROOM118 SERC
gt ROOM118 WRITE (,'(A,4F12.1)') ' SERC,
ALLISON, ROOM111, ROOM118', SERC, ALLISON,
ROOM111, ROOM118 STOP END PROGRAM
ASSIGNMENT D\Arutrl\Cl\COMPUTER\Lecture\Book\ELF
proggtASSIGNMENT SERC, ALLISON, ROOM111, ROOM118
50.0 50.0 50.0
75.0 SERC, ALLISON, ROOM111, ROOM118 75.0
50.0 50.0 75.0
17Pointing Relationships in Program Assignment
Target ROOM111 Value50
Pointer ALLISON was assigned with pointer SERC
Target ROOM118 Value75
but, this assignment means that both pointers are
associated with the same target independently
so, when SERC is reassigned to target ROOM118,
ALLISON remains associated with ROOM111
Target ROOM111 Value50
Target ROOM118 Value75
See output of program assignment on prior slide
Source Fortran 90/95 for Scientists and
Engineers by Stephen Chapman. Published by
McGraw Hill, 1998, page 593.
18Partitioned Approach to Programming
- A better approach is to divide the task by
partitioning the repetitive elements as a
separate program block.
Main Program
PROGRAM COMBOIMPLICIT NONEINTEGERN,
IREALCOMB1, COMB2, COMB3,COMBTOTAL
INVOKE SUBPROG FOR N! INVOKE SUBPROG FOR
I! INVOKE SUBPROG FOR (N-I)! COMBTOTAL
COMB1 / (COMB2 COMB3) WRITE(,)
COMBTOTALSTOP END PROGRAM COMBO
Subprogram
SUBPROGRAM FACTORIALIMPLICIT NONE!BLOCK
SUBPROGRAM THIS PROGRAM SEGMENT
CONTAINS CODE FOR FACTORIAL CALCULATION RETURN
END SUBPROGRAM FACTORIAL
19Function Subprograms
- General Architecture
- FUNCTION name (list)
- IMPLICIT NONE
- REAL variables, name
- INTEGER variables
-
- RETURN
- END FUNCTION name
20Function Subprograms
Main Program
Function
FUNCTION FACT(N) IMPLICIT NONE REALFACT INTEGER,
INTENT(IN)N INTEGERI FACT 1.0 DO I
2,N FACT FACT I END
DO RETURN END FUNCTION FACT
PROGRAM ROYAL_FLUSH IMPLICIT NONE REALPROB,NUM,
DEN !CALCULATE NUMERATOR NUMFACT(4)/FACT(1)/FACT
(3) !CALCULATE DENOMINATOR DENFACT(52)/FACT(5)/F
ACT(47) PROBNUM/DEN WRITE(,100) NUM, DEN,
PROB, 1.0/PROB 100 FORMAT (' ',4E20.4) STOP END
PROGRAM ROYAL_FLUSH
21A Word About Subroutines
- A subroutine is invoked by naming in call
statement -- that receives its input values and
returns its results through an argument list.
Argument list contains list of variables, arrays
or both that are being passed from the calling
program to the subroutine. These variables are
called dummy arguments since the subroutine does
not actually allocate any memory for them. They
are just placeholders for actual arguments which
will be passed from the calling program unit when
the subroutine is invoked.
22Subroutine Subprograms
- General architecture
- SUBROUTINE name (list)
Source code for computational tasks
RETURN END
23A Word About Implicit None
- Implicit none disables default typing provision
so that the programmer must explicitly declare
the type of every variable in the program. In a
module setting, a procedure compiled within a
module and accessed by USE is said to have an
explicit interface. Make sure, however, you
include the implicit none statement in your
module. With explicit interface the compiler
knows the details of every argument in the
procedure whenever it is used--the compiler also
checks for mistakes.
24Subroutine Subprograms
- The Call Statement
- Passes variables and transfers execution to the
subroutine - Can be used in MAIN or in another subroutine
- Cannot call itself, except in special cases see
recursion - When subroutine is completed, execution control
returns to CALL statement in MAIN. - General Form
CALL name (variable list)
Name -- directs execution to this program
segment Variable list -- data sent or received
from subroutine
25Sharing with MODULE Statement
- MODULE creates a means to share data and
procedures - MODULE is a separate, compiled program
- Contains definitions and initial values of data
- USE statement invokes MODULE data values
MODULE DEMO ! This program demonstrates the use
of a module ! To declare data shared between two
routines. IMPLICIT NONE INTEGER,PARAMETERN11
!Array size REAL, DIMENSION(N) ANGLE
!Array dimensioned END MODULE DEMO
26Sharing with MODULE Statement
- MODULES can contain subroutines and functions
MODULE DEMO ! This program demonstrates the use
of a module IMPLICIT NONE INTEGER,PARAMETERN11
!Array size REAL, DIMENSION(N) ANGLE
!Array dimensioned CONTAINS SUBROUTINE
PRINT(ANG) ! USES THE DATA DECLARED IN DEMO
MODULE IMPLICIT NONE REAL,INTENT(IN)ANG(N) WR
ITE (,) ANG RETURN END SUBROUTINE PRINT END
MODULE DEMO
- But why go to this trouble?
27Explicit vs. Implicit Interfaces
- MODULES provide an explicit interface
- Compiler checks interface and catches errors!
- Number of arguments
- Type of arguments
- INTENT
- Array
- Without MODULES implicit interfaces are active
- Compiler cannot check
- Compiler assumes you know what you are doing
- Programs can fail in peculiar and hard-to-fix ways
28Modular Approach to Programming
- MODULES can find incorrect data transfers between
program segments, a real debugging time saver!
MODULE ARG CONTAINS SUBROUTINE INTARG ( K
) IMPLICIT NONE INTEGER, INTENT(IN) K
! DECLARE INTEGER ARGUMENT. WRITE (,) ' K ',
K ! WRITE OUT K. RETURN END SUBROUTINE
INTARG END MODULE ARG PROGRAM REALCALL ! To
show how modules can catch wrong calling
arguments. USE ARG IMPLICIT NONE REAL Z
100. ! DECLARE REAL VARIABLE Z. CALL INTARG ( Z
) ! CALL SUBROUTINE. STOP END
PROGRAM REALCALL
29Homework Assignment -- Data Analysis
Data have varying degrees of variability. Some
data are very noisy whereas other data are quite
precise. As researchers, the more we work on the
frontiers of knowledge, the lower the
signal/noise ratio and the harder we have to work
to extract information from our experimental
data. When we are confident of our data, we
weigh the actual data point heavily. When we are
not, we tend to give more credence to an average
of neighbors, i.e. a smoothed value. One
technique that allows you to vary the emphasis
given to a data point or to an average is the
Weighted Averaging method, given mathematically
as follows YNEW(I) WY(I)
(1-W)F(X(I)) Where the weighting factor, W, is
between zero and 1. F(X(I)) is a linearly
interpolated value for Y(I) calculated from the
two nearest neighbors, one on each side of X(I),
Y(I). Write a program that smoothes the data
below using this method. Select different values
of W and comment on the results. Consider volts
as the independent variable X and Current as
the dependent variable Y.
30Data - An Imperfect Medium
- Collecting temperature data. What is the true
temperature?
- Sources of Error
- Systematic errors
- Bad thermometer
- Improper meniscus reading methodology
- Thermometer measuring wrong substance
- Bad computation computer programming error or
just plain carelessness - Random errors
- Randomly distributed effects from unknown sources
- Need techniques for managing
31Experimental Errors
- Methods for managing or correcting for error
- Systematic errors
- Identify source of error and correct the
experimental method - Requires repeating the test
- But procedure is improved for future use
- Apply mathematic correction to existing data
- Simple and most cost-effective
- But correction is based on arbitrary
determination - Experiment remains fundamentally flawed
- Bad computation
- Be more careful
- Improve computer programming segments
- Random errors
- Averaging is very powerful
- Always have some random error remaining in all
experiments