All further questions about the definition of SPARKS should be addressed to:Chairman, SPARKS Users Group Computer Science, Powell Hall University of Southern California... We see that in
Trang 2This section is meant for people who do most of their programming in
FORTRAN FORTRAN has the distinction of being essentially the earliest
higher level programming language, developed about 1957 by a group at IBM.Since then it and its derivatives have become established as the primary
language for scientific and engineering computation But, with our greater
understanding of the process of creating programs has come a realization of thedeficiencies of FORTRAN Creating a program is properly thought of as taking areal world problem and translating it into a computer solution Concepts in thereal world such as a geneology tree or a queue of airplanes must be translatedinto computer concepts A language is good if it enables one to describe theseabstractions of the real world in a natural way Perhaps because of its very earlydevelopment, FORTRAN lacks many such features In this appendix we explorethe idea of writing a preprocessor for FORTRAN which inexpensively addssome of these missing features
A preprocessor is a program which translates statements written in a language X into FORTRAN In our case X is called SPARKS Such a program is normally
called a compiler so why give it the special name preprocessor? A preprocessor
is distinguished from a compiler in the following way: the source and targetlanguage have many statements in common
Such a translator has many advantages Most importantly it preserves a closeconnection with FORTRAN Despite FORTRAN's many negative attributes, ithas several practical pluses: 1) it is almost always available and compilers areoften good, 2) there is a language standard which allows a degree of portabilitynot obtainable with other languages, 3) there are extensive subroutine libraries,and 4) there is a large labor force familiar with it These reasons give FORTRAN
a strong hold in the industrial marketplace A structured FORTRAN translatorpreserves these virtues while it augments the language with improved syntacticalconstructs and other useful features
Another consideration is that at many installations a nicely structured language
is unavailable In this event a translator provides a simple means for
supplementing an existing FORTRAN capability The translator to be describedhere can be obtained by writing to the address given at the end of this appendix
Trang 4improvements are consistently adopted in a large software project, the resultingcode is bound to be easier to comprehend
We begin by defining precisely the SPARKS language A distinction is madebetween FORTRAN statements and SPARKS statements The latter are
recognized by certain keywords and/or delimiters All other statements areregarded as FORTRAN and are passed directly to the FORTRAN compilerwithout alteration Thus, SPARKS is compatible with FORTRAN and a
FORTRAN program is a SPARKS program SPARKS statements cause thetranslator to produce ANSI FORTRAN statements which accomplish the
equivalent computation Hence, the local compiler ultimately defines the
semantics of all SPARKS statements
Trang 5In the following any reference to the term "statements" is meant to include bothSPARKS and FORTRAN statements There are six basic SPARKS statements,two which improve the testing of cases and four which improve the description
S1,S2, ,S n +1 are arbitrary size groups of statements Cond1, cond2, , condn
are legal FORTRAN conditionals The symbol ELSE surrounded by colonsdesignates that Sn+l will be automatically executed if all previous conditions arefalse This part of the case statement is optional
The four looping statements are:
Trang 6UNTIL cond REPEAT IF(.NOT (cond)) GO TO 100
S and cond are the same as for the while statement immediately preceding.LOOP 100 CONTINUE
Trang 7101 CONTINUE
The three expressions exp1, exp2, exp3 are allowed to be arbitrary FORTRANarithmetic expressions of any type Similarly vble may be of any type However,the comparison test is made against integer zero Since exp2 and exp3 are re-evaluated each time through the loop, care must be taken in its use
EXIT is a SPARKS statement which causes a transfer of control to the firststatement outside of the innermost LOOP-REPEAT statement which contains it.One example of its use is:
The statement CYCLE is also used within any SPARKS looping statement Itsexecution causes a branch to the end of the innermost loop which contains it Atest may be made and if passed the next iteration is taken An example of the use
of EXIT and CYCLE follow
LOOP 100 CONTINUE
S1 S1
Trang 8would be legal in SPARKS To include a semicolon in a hollerith field it should
be followed by a second semicolon This will be deleted in the resulting
FORTRAN
Trang 9approaches are feasible The first is a table-driven method which scans a
program and recognizes keywords This approach is essentially the way a
compiler works in that it requires a scanner, a symbol table (though limited),very limited parsing and the generation of object (FORTRAN) code A secondapproach is to write a general macro preprocessor and then to define each
Trang 10Below is a list of possible extensions for SPARKS Some are relatively easy toimplement, while others require a great deal of effort
Trang 11E.4 Add the capability of profiling a program by determining the number ofexecutions of each loop during a single execution and the value of conditionalexpressions
HINT: For each subroutine declare a set of variables which can be inserted afterencountering a WHILE, LOOP, REPEAT, FOR, THEN or ELSE statement Atthe end of each subroutine a write statement prints the values of these counters.E.5 Add the multiple replacement statement so that
HINT: Mutually recursive programs are gathered together in a module,
MODULE (X(A,B,C)(100)) whose name is X, whose parameters are A,B,C andwhose stack size should be 100
Trang 12down program refinement
E.10 Add an internal procedure capability to aid the programmer in doing top-E.11 Attach sequence numbers to the resulting FORTRAN output which relateseach statement back to the original SPARKS statement which generated it This
is particularly helpful for debugging
E.12 Along with the indented SPARKS source print a number which representsthe level of nesting of each statement
All further questions about the definition of SPARKS should be addressed to:Chairman, SPARKS Users Group
Computer Science, Powell Hall
University of Southern California
Trang 13To receive a complete ANSI FORTRAN version of SPARKS send $20.00 (forpostage and handling) to Dr Ellis Horowitz at the above address
Go to Appendix B Back to Table of Contents
Trang 15APPENDIX B: ETHICAL CODE IN INFORMATION PROCESSING
Trang 16ACM CODE OF PROFESSIONAL CONDUCT
Trang 17Recognition of professional status by the public depends not only on skill anddedication but also on adherence to a recognized code of Professional Conduct.The following Code sets forth the general principles (Canons), professional
to admonition, suspension, or expulsion from the Association as provided by theConstitution and Bylaws The term "member(s)" is used in the Code The
Disciplinary Rules of the Code apply, however, only to the classes of
membership specified in Article 3, Section 4, of the Constitution of the ACM
Trang 18An ACM member shall act at all times with integrity
Ethical Considerations
EC1.1 An ACM member shall properly qualify himself when expressing anopinion outside his areas of competence A member is encouraged to express hisopinion on subjects within his areas of competence
EC1.2 An ACM member shall preface an partisan statements about informationprocessing by indicating clearly on whose behalf they are made
DR1.3.1 An ACM member acting or employed as a consultant shall, prior toaccepting information from a prospective client, inform the client of all factors
of which the member is aware which may affect the proper performance of thetask
DR1.3.2 An ACM member shall disclose any interest of which he is awarewhich does or may conflict with his duty to a present or prospective employer orclient
DR1.3.3 An ACM member shall not use any confidential information from anyemployer or client, past or present, without prior permission
Trang 19An ACM member should strive to increase his competence and the competenceand prestige of the profession
Ethical Considerations
EC2.1 An ACM member is encouraged to extend public knowledge,
understanding, and appreciation of information processing, and to oppose anyfalse or deceptive statements relating to information processing of which he isaware
EC2.2 An ACM member shall not use his professional credentials to
misrepresent his competence
EC2.3 An ACM member shall undertake only those professional assignmentsand commitments for which he is qualified
EC2.4 An ACM member shall strive to design and develop systems that
adequately perform the intended functions and that satisfy his employer's orclient's operational needs
EC2.5 An ACM member should maintain and increase his competence through
a program of continuing education encompassing the techniques, technicalstandards, and practices in his fields of professional activity
EC2.6 An ACM member should provide opportunity and encouragement forprofessional development and advancement of both professionals and thoseaspiring to become professionals
Trang 20adequately competent without acquiring the assistance of a professional who iscompetent to perform the assignment
DR2.4.1 An ACM member shall not represent that a product of his work willperform its function adequately and will meet the receiver's operational needswhen he knows or should know that the product is deficient
Trang 21An ACM member shall accept responsibility for his work
Ethical Considerations
EC3.1 An ACM member shall accept only those assignments for which there isreasonable expectancy of meeting requirements or specifications, and shallperform his assignments in a professional manner
Disciplinary Rules
DR3.1.1 An ACM member shall not neglect any professional assignment whichhas been accepted
DR3.1.2 An ACM member shall keep his employer or client properly informed
on the progress of his assignments
DR3.1.3 An ACM member shall not attempt to exonerate himself from, or tolimit, his liability to his clients for his personal malpractice
DR3.1.4 An ACM member shall indicate to his employer or client the
consequences to be expected if his professional judgement is overruled
Trang 22An ACM member shall act with professional responsibility
Ethical Considerations
EC4.1 An ACM member shall not use his membership in ACM improperly forprofessional advantage or to misrepresent the authority of his statements
EC4.2 An ACM member shall conduct professional activities on a high plane
EC4.3 An ACM member is encouraged to uphold and improve the professionalstandards of the Association through participation in their formulation,
DR4.1.3 An ACM member shall preface partisan statements about informationprocessing by indicating clearly on whose behalf they are made
DR4.2.1 An ACM member shall not maliciously injure the professional
reputation of any other person
DR4.2.2 An ACM member shall not use the services of or his membership inthe Association to gain unfair advantage
DR4.2.3 An ACM member shall take care that credit for work is given to whomcredit is properly due
Trang 23Go to Appendix C Back to Table of Contents
Trang 25APPENDIX C: ALGORITHM INDEX BY CHAPTER
Trang 26Erasing a circular list CERASE 4.4
Circular linked polynomial addition CPADD 4.4Inverting a linked list INVERT 4.5
Concatenating two lists CONCATENATE 4.5Circular list insertion INSERT-FRONT 4.5
Length of a circular list LENGTH 4.5
Finding equivalence classes EQUIVALENCE 4.6Reading in a sparse matrix MREAD 4.7
Trang 28Quicksort QSORT 7.3
Trang 29Two-way merge sort MERGE, MPASS, MSORT, RMSORT 7.5Heapsort ADJUST, HSORT 7.6
Trang 31CHAPTER 1: INTRODUCTION
Trang 32continuum At one end are the languages which are closest to the physical
machine and at the other end are languages designed for sophisticated problemsolving One often distinguishes between two phases of this area: language
design and translation The first calls for methods for specifying the syntax andsemantics of a language The second requires a means for translation into a morebasic set of commands
(iii) foundations of algorithms here people ask and try to answer such questions
as: is a particular task accomplishable by a computing device; or what is theminimum number of operations necessary for any algorithm which performs acertain function? Abstract models of computers are devised so that these
properties can be studied
(iv) analysis of algorithms whenever an algorithm can be specified it makes
sense to wonder about its behavior This was realized as far back as 1830 byCharles Babbage, the father of computers An algorithm's behavior pattern or
performance profile is measured in terms of the computing time and space that
are consumed while the algorithm is processing Questions such as the worst andaverage time and how often they occur are typical
We see that in this definition of computer science, "algorithm" is a fundamentalnotion Thus it deserves a precise definition The dictionary's definition "anymechanical or recursive computational procedure" is not entirely satisfying sincethese terms are not basic enough
Trang 33An algorithm can be described in many ways A natural language such as
English can be used but we must be very careful that the resulting instructionsare definite (condition iii) An improvement over English is to couple its usewith a graphical form of notation such as flowcharts This form places eachprocessing step in a "box" and uses arrows to indicate the next step Differentshaped boxes stand for different kinds of operations All this can be seen infigure 1.1 where a flowchart is given for obtaining a Coca-Cola from a vendingmachine The point is that algorithms can be devised for many common
activities
Have you studied the flowchart? Then you probably have realized that it isn't analgorithm at all! Which properties does it lack?
Returning to our earlier definition of computer science, we find it extremelyunsatisfying as it gives us no insight as to why the computer is revolutionizingour society nor why it has made us re-examine certain basic assumptions about
Trang 34definition even from a technical point of view it is unsatisfying The definitionplaces great emphasis on the concept of algorithm, but never mentions the word
"data" If a computer is merely a means to an end, then the means may be analgorithm but the end is the transformation of data That is why we often hear acomputer referred to as a data processing machine Raw data is input and
algorithms are used to transform it into refined data So, instead of saying thatcomputer science is the study of algorithms, alternatively, we might say that
computer science is the study of data:
(i) machines that hold data;
(ii) languages for describing data manipulation;
(iii) foundations which describe what kinds of refined data can be produced fromraw data;
(iv) structures for representing data
Figure 1.1: Flowchart for obtaining a Coca-Cola
There is an intimate connection between the structuring of data, and the
synthesis of algorithms In fact, a data structure and an algorithm should bethought of as a unit, neither one making sense without the other For instance,
suppose we have a list of n pairs of names and phone numbers (a1,b1)(a2,b2), ,
(a n ,b n), and we want to write a program which when given any name, prints thatperson's phone number This task is called searching Just how we would writesuch an algorithm critically depends upon how the names and phone numbersare stored or structured One algorithm might just forge ahead and examine
names, a1,a2,a3, etc., until the correct name was found This might be fine inOshkosh, but in Los Angeles, with hundreds of thousands of names, it would not
be practical If, however, we knew that the data was structured so that the nameswere in alphabetical order, then we could do much better We could make up asecond list which told us for each letter in the alphabet, where the first name
with that letter appeared For a name beginning with, say, S, we would avoid
having to look at names beginning with other letters So because of this newstructure, a very different algorithm is possible Other ideas for algorithms
Trang 35Therefore, computer science can be defined as the study of data, its
representation and transformation by a digital computer The goal of this book is
to explore many different kinds of data objects For each object, we consider theclass of operations to be performed and then the way to represent this object sothat these operations may be efficiently carried out This implies a mastery oftwo techniques: the ability to devise alternative forms of data representation, andthe ability to analyze the algorithm which operates on that structure The
pedagogical style we have chosen is to consider problems which have arisenoften in computer applications For each problem we will specify the data object
or objects and what is to be accomplished After we have decided upon a
representation of the objects, we will give a complete algorithm and analyze itscomputing time After reading through several of these examples you should beconfident enough to try one on your own
There are several terms we need to define carefully before we proceed Theseinclude data structure, data object, data type and data representation These fourterms have no standard meaning in computer science circles, and they are oftenused interchangeably
A data type is a term which refers to the kinds of data that variables may "hold"
in a programming language In FORTRAN the data types are INTEGER, REAL,LOGICAL, COMPLEX, and DOUBLE PRECISION In PL/I there is the datatype CHARACTER The fundamental data type of SNOBOL is the characterstring and in LISP it is the list (or S-expression) With every programming
language there is a set of built-in data types This means that the language allowsvariables to name data of that type and provides a set of operations which
meaningfully manipulates these variables Some data types are easy to providebecause they are already built into the computer's machine language instructionset Integer and real arithmetic are examples of this Other data types requireconsiderably more effort to implement In some languages, there are featureswhich allow one to construct combinations of the built-in types In COBOL andPL/I this feature is called a STRUCTURE while in PASCAL it is called a
RECORD However, it is not necessary to have such a mechanism All of thedata structures we will see here can be reasonably built within a conventionalprogramming language
Trang 36arithmetic operations +, -, *, / and perhaps many others such as mod, ceil, floor,greater than, less than, etc The data object integers plus a description of how +, -, *, /, etc behave constitutes a data structure definition
To be more precise lets examine a modest example Suppose we want to definethe data structure natural number (abbreviated natno) where natno = {0,1,2,3, }with the three operations being a test for zero addition and equality The
Trang 37a boolean function whose result is either true or false SUCC stands for
successor Using ZERO and SUCC we can define all of the natural numbers as:ZERO, l = SUCC(ZERO), 2 = SUCC(SUCC(ZERO)), 3 =
SUCC(SUCC(SUCC(ZERO))), etc The rules on line 8 tell us exactly how theaddition operation works For example if we wanted to add two and three wewould get the following sequence of expressions:
Definition: A data structure is a set of domains , a designated domain , aset of functions and a set of axioms The triple denotes the data
structure d and it will usually be abbreviated by writing d.
Trang 38The set of axioms describes the semantics of the operations The form in which
we choose to write the axioms is important Our goal here is to write the axioms
in a representation independent way Then, we discuss ways of implementing thefunctions using a conventional programming language
An implementation of a data structure d is a mapping from d to a set of other data structures e This mapping specifies how every object of d is to be
represented by the objects of e Secondly, it requires that every function of d must be written using the functions of the implementing data structures e Thus
we say that integers are represented by bit strings, boolean is represented by zeroand one, an array is represented by a set of consecutive words in memory
In current parlance the triple is referred to as an abstract data type It is called
abstract precisely because the axioms do not imply a form of representation.Another way of viewing the implementation of a data structure is that it is theprocess of refining an abstract data type until all of the operations are expressible
in terms of directly executable functions But at the first stage a data structure
should be designed so that we know what it does, but not necessarily how it will
do it This division of tasks, called specification and implementation, is usefulbecause it helps to control the complexity of the entire process
Trang 39or, more particularly, disliked to use the language X.
Furthermore it is not really necessary to write programs in a language for which
a compiler exists Instead we choose to use a language which is tailored to
describing the algorithms we want to write Using it we will not have to definemany aspects of a language that we will never use here Most importantly, thelanguage we use will be close enough to many of the languages mentioned
before so that a hand translation will be relatively easy to accomplish Moreover,one can easily program a translator using some existing, but more primitivehigher level language as the output (see Appendix A) We call our languageSPARKS Figure 1.2 shows how a SPARKS program could be executed on anymachine
Figure 1.2: Translation of SPARKS
Many language designers choose a name which is an acronym But SPARKSwas not devised in that way; it just appeared one day as Athena sprang from thehead of Zeus Nevertheless, computerniks still try to attach a meaning Severalcute ideas have been suggested, such as