Nash - Scientific Computing with PCs (523165), страница 17
Текст из файла (страница 17)
It isworthwhile choosing carefully and discarding unsuitable editors, compilers, debuggers or programming"environments". One great nuisance of our own work, involving the testing and evaluation of many suchproducts, is that each differs in the commands, control mechanisms, presentation, and other interfacedetails. Continual change between different interfaces is inevitably inefficient and error-prone.6.2Cross-Reference Listing and Data DictionaryWhen developing a program, it is inevitable that one chooses names for variables in a way that suits ourown convenience and taste. After some time, or in merging two programs, we may have conflicting orinappropriate uses of a single variable name. An aid in sorting out such problems is a cross-referencelisting.A cross-reference listing should itemize variables, functions and labels (line numbers) according to wherethey occur and their usage in a program.
In Figure 6.2.1 we give a typical example for a program writtenin FORTRAN and compiled with the Lahey F77L compiler. Here we have set a compiler switch togenerate a cross-reference listing. Such a feature is quite common in compilers, but hardly universal.Alternatively, we probably can find utility programs to create cross-reference listings. In the past, we haveused the now-dated PFORT Verifier (Ryder and Hall, 1979) that also checks conformance of the programwith the FORTRAN 66 standard. FORTRAN programming tools are also available from commercialvendors such as the Numerical Algorithms Group (NAG).
In our own work we have not felt justified inspending money on such tools, since we have had the benefit of compilers that will generate a form ofthe listing required.The same cannot be said of BASIC or PASCAL. We extensively modified a program for preparing crossreference listings for BASIC programs to polish the code published in Nash and Walker-Smith (1987).Similarly, in checking the PASCAL code in Nash J C (1990d), a program called TXREF, attributed toMichael Roberts, was obtained via colleagues from the Turbo (Pascal) Users’ Group.The main assistance of such programs may come from incidental features.
In PASCAL, for example, wenoted our most common error was that comment braces would not be closed, so that part of the programthat we wanted to execute would then be inside a comment block. TXREF displays the comment level sosuch errors are easy to detect.Similarly, C programmers frequently are concerned that the begin.end braces for blocks are unbalanced.Some programming editors, such as BRIEF, will format the display to show the balancing of such blocks.A similar problem exists with the begin and end constructs in PASCAL, and we have found that theTXREF program also helped in this regard.Cross-reference listings help us to check that the data we are using is correctly named. Where possible,we should also check that it has the correct type. Furthermore, we can track the variables and/or theinformation they carry from routine to routine within the whole program.
Such tracking is useful in44Copyright © 1984, 1994 J C & M M NashSCIENTIFIC COMPUTING WITH PCsNash Information Services Inc., 1975 Bel Air Drive, Ottawa, ON K2C 0X1 CanadaFigure 6.2.1Copy for:Dr. Dobb’s JournalListing with cross-reference table for a FORTRAN program to compute cube roots byNewton’s method. The listing was edited to fit the page.F77L - Lahey FORTRAN 77, Version 3.0110/28/92 14:45:07Options:/N0/N2/N7/NA/NB/NC/ND/NE/NF/H/NI/NL/P/R/S/NT/NU/Source file Listing123456789101112131415161718192021222324252627283031323334353637383940414243444546474849505152CCCCCCCCUBEROOT.FORPROGRAM _MAIN-- A FORTRAN PROGRAM TO COMPUTE CUBE ROOTSUSING A SAFEGUARDED NEWTON’S METHODSOLVE Y = X**3 BY SOLVINGF(X) == X**3 - Y = 0REAL X,Y,XOLD,FVAL,DVALX, XOLD HOLD OUR ITERATES, Y = NUMBER TO FIND ROOT FORFVAL IS FUNCTION VALUE, DVAL ITS DERIVATIVEINTEGER ITERTO COUNT THE ITERATIONSCCCCCGET THE NUMBER YWRITE(*,950)FORMAT(’0 ENTER THE NUMBER FOR WHICH ROOT IS DESIRED ’)READ(*,900) YFORMAT(F10.0)WRITE(*,951) YFORMAT(’0 Y = ’,1PE20.12)950900951CCCPROVIDE AN INITIAL APPROXIMATION FOR THE ROOTX=Y/3.0IF (ABS(Y).LT.1.0) X=3.0*YCWRITE(*,952)XFORMAT(’0 INITIAL APPROXIMATION TO CUBE ROOT = ’,1PE20.12)SAVE ’OLD’ VALUE952CC10953CCCC95420955ITER = 0XOLD = XFVAL = (X*X*X - Y)DVAL = (3.0*X*X)WRITE(*,953)ITER,FVAL,DVALFORMAT(’ ITN.’,I3,’ (X**3-Y) = ’,1PE16.8,’ DERIV =’,E16.8)SAFETY CHECKIF(FVAL.EQ.0.0) GOTO 20CHECK ON DERIVATIVE AND SAFETY SETTINGIF (DVAL.EQ.0.0) DVAL=YDVAL = 0.0 IFF X HAS BECOME ZERO.
CUBE ROOT HAS SAMESIGN AS NUMBER Y.ITER=ITER+1X = X - FVAL/DVALWRITE(*,954) XFORMAT(’NEW X = ’,1PE16.8)IF (X.NE.XOLD) GOTO 10WRITE(*,955)X,ITERFORMAT(’0 CONVERGED TO ’,1PE16.8,’ AFTER ’,I3,’ ITNS’)STOPENDSymbol Cross-Reference Listing Reference types: =-Assigned; /-DATA; d-DO Index; f-FORMATUsage: i-Input; o-Output; r-Argument; s-Specified; u-Used---------------------------------------------------------------NameTypeClassLine <reference type - see above>AbsDvalFvalIterXREALREALINTEGERREALGENERICVARIABLEVARIABLEVARIABLEVARIABLEXoldYREALREALVARIABLEVARIABLE25u8s8s11s8s34u46o8s8s34u35=34=32=24=34u48u33=17i41u36o36o36o25=35u49o48u19o41u39u44=27o35u41=45u44u33u45=45u49o34u45u24u25u25u29CCompiling6: PROGRAMMING45Figure 6.2.1 (continued)Label Cross-Reference Listing Reference types: a-ASSIGN; d-DO Label; @-FORMAT Statement; f-FORMATUsage: g-GOTO; i-IF; s-Specified; r-Argument-------------------------------------------------------------------LabelLine <reference type - see above>00010000200090000950009510095200953009540095533s39g17f15f19f27f36f46f49fFigure 6.2.2SymbolDVALFVALITERXXOLDY48g49s18@16@20@28@37@47@50@Data dictionary for the cube root finder of Figure 6.2.1Meaning and commentsTemporary variable to hold the value of the derivative of the function f(x) = (x**3 - y); thisis f’(x) = 3 x**2Temporary variable to hold value of f(x) = (x**3 - y)Iteration counterApproximation to the cube root of Y which is refined by the Newton iteration.The last approximant for the cube root of Y.
Used for the convergence test.Number for which cube root is wanted.verifying that the correct data is used throughout the calculations and is an aid in documenting the codefor users or those who must maintain or upgrade the code later.When the cross-reference feature is not available in a compiler nor in a separate program, we must do thejob "manually". This is most likely when we are trying to document scripts for special purpose packages,e.g., Stata or MATLAB. While we would not recommend doing the full cross-reference task by hand, formost programming or command script languages it is straightforward to list variables by routines(functions or procedures). Such a listing is sufficient for following the data flow within programs.
To buildthe list we could use a text editor to add the function or procedure name to the variable declarations, thensort on the variable name after some stylistic cleanup.Extending the idea of a cross-reference listing is a data dictionary. This literally gives the meaning of eachdata element, though it is also useful in recording the structure and format of such elements. A typicaldata dictionary lists the name, purpose and structure of each variable or array. Clearly, the cross-referencelisting can serve as the basis and even the recording sheet for such documentation.6.3Flowchart or Other Algorithmic DescriptionThe previous section concerned data structures, where they occur in programs (cross-reference) and why(data dictionary).
We firmly believe this is the correct precedence, that is, we should understand the databefore considering the procedures that such data must undergo. Understanding the algorithmic processesimplies that we have a way of writing them down. In sum, the listing of the working program representssuch a record. Unfortunately, even modern structured high level programming languages generally areimperfect as tools for description of algorithms to humans rather than computers.What is desirable is a description of what we want the PC to do in words we can understand.
To this end,many programmers use some simple method for describing the algorithmic processes. Two approaches46Copyright © 1984, 1994 J C & M M NashNash Information Services Inc., 1975 Bel Air Drive, Ottawa, ON K2C 0X1 CanadaSCIENTIFIC COMPUTING WITH PCsCopy for:Dr. Dobb’s Journalare the flowchart and the step-and-description pseudocode.Flowcharts are diagrams with arrows showing the control transfers within the program. Variously shapedgeometric figures are used to denote different types of processes within the algorithm. The use offlowcharts seems less common now than previously, especially in describing scientific computations.The pseudocode, or step-and-description approach uses simple numbered or labelled steps and statementsin plain language to explain what is going on. Comments should be liberally used, particularly if thereason for a given statement is not obvious. A number of authors have used various pseudocodes havinga formal syntax, for example, the introductory text for beginners by Dyck, Lawson and Smith (1979).
Inmathematical computations, different forms have been employed by Lawson and Hanson (1974) and NashJ C (1979a). Nash J C (1978b) gives some reasons why such a informal presentation of an algorithm maybe preferred to a program listing. In passing, it is worth noting that two pseudocodes have becomefull-fledged programming languages, APL (Iverson, 1962) and PASCAL (Wirth, 1971). In our own work,we generally use descriptions close to PASCAL. MATLAB is becoming common as a way to describenumerical algorithms.6.4Structure and ModularityIn recent years, structure in programs has become a much publicized virtue. Good programmers havealways kept their programs tidy and well-arranged so that they can be easily documented, debugged,altered or maintained.