Complete Download A Computer Science Tapestry Exploring Programming and Computer Science with C 2nd edition Owen L. Astrachan PDF All Chapters
Complete Download A Computer Science Tapestry Exploring Programming and Computer Science with C 2nd edition Owen L. Astrachan PDF All Chapters
https://ebookultra.com/download/cambridge-igcse-computer-science-
programming-book-1st-edition-richard-morgan/
ebookultra.com
https://ebookultra.com/download/python-programming-an-introduction-to-
computer-science-john-m-zelle/
ebookultra.com
https://ebookultra.com/download/mathematical-structures-for-computer-
science-6th-edition-judith-l-gersting/
ebookultra.com
https://ebookultra.com/download/introduction-to-lattice-theory-with-
computer-science-applications-1st-edition-garg/
ebookultra.com
https://ebookultra.com/download/the-art-and-science-of-computer-
animation-2nd-edition-stuart-mealing/
ebookultra.com
https://ebookultra.com/download/great-jobs-for-computer-science-
majors-2nd-edition-jan-goldberg/
ebookultra.com
https://ebookultra.com/download/computer-science-illuminated-revised-
nell-b-dale/
ebookultra.com
https://ebookultra.com/download/great-ideas-in-computer-science-with-
java-1st-edition-alan-w-biermann/
ebookultra.com
A Computer Science Tapestry Exploring Programming
and Computer Science with C 2nd edition Owen L.
Astrachan Digital Instant Download
Author(s): Owen L. Astrachan
ISBN(s): 9780072465365, 0072465360
File Details: PDF, 4.37 MB
Language: english
June 7, 1999 10:10 owltex Sheet number 1 Page number i magenta black
ii
Second Edition
Owen L. Astrachan
Duke University
Boston Burr Ridge,IL Dubuque,IA Madison,WI New York San Francisco St. Louis
Bankok Bogotá Caracas Lisbon London Madrid
Mexico City Milan New Delhi Seoul Singapore Sydney Taipei Toronto
June 7, 1999 10:10 owltex Sheet number 3 Page number iii magenta black
iii
Front matter
June 7, 1999 10:10 owltex Sheet number 4 Page number iv magenta black
iv
Copyright information
June 7, 1999 10:10 owltex Sheet number 5 Page number v magenta black
vi
To my teachers, colleagues, and friends, especially to those who are all three, for
educating, arguing, laughing, and helping.
Preface
The Tapestry Viewed from Afar
This book is designed for a first course1 in computer science that uses C++ as the language
by which programming is studied. My goal in writing the book has not been to cover
the syntax of a large language like C++, but to leverage the best features of the language
using sound practices of programming and pedagogy in the study of computer science
and software design. My intent is that mastering the material presented here will provide:
In particular, this is a book designed to teach programming using C++, not a book de-
signed to teach C++. Nevertheless, I expect students who use this book will become
reasonably adept C++ programmers. Object-oriented programming is not a program-
mer’s panacea, although it can make some jobs much easier. To mix metaphors, learning
to program is a hard task, no matter how you slice it—it takes time to master, just as
bread takes time to rise.
The material here is grounded in the concept that the study of computer science
should be part of the study of programming. I also want students to use classes before
writing them, so a library of useful classes is integrated into the text. Students will
better appreciate good design by seeing it in practice than by simply reading about
it. This requires studying and using classes that actually do something and that are
easy for novice programmers to use. For example, I don’t use any examples about
bank accounts or Automated Teller Machines. These traditional examples work well in
explaining concepts, but it’s not possible to implement a real bank account class or an
ATM in the first programming course. I do supply classes for calendar dates, unbounded
integers, timing program segments, reading directories, random numbers, and several
others. These classes can be used early (or late) in a semester, allowing students to write
more interesting programs without writing more code. For example, using the Date
class students can write a three-line program to determine how many days old they are
whenever they run the program, an eight-line program to find out what day Thanksgiving
falls on in any year, and a forty-line program to print a calendar for any year. Using
1
This first course has traditionally been called CS1 after early ACM guidelines.
vii
June 7, 1999 10:10 owltex Sheet number 8 Page number viii magenta black
viii
the classes for reading directories makes it possible to write a twenty-line program for
finding all files that are large, or were last modified yesterday, or a host of other problems.
Most importantly, this book takes the view that the study of computer science should
involve hands-on activity and be fun. The study of programming must cover those areas
that are acknowledged as fundamental to computer science, but the foundation that is
constructed during this study must be solid enough to support continued study of a rapidly
changing programming world, and the process of studying should make students want
to learn more. Support for this position can be found in several places; I offer two quotes
that express my sentiments quite well.
Having surveyed the relationships of computer science with other disciplines, it
remains to answer the basic questions: What is the central core of the subject?
What is it that distinguishes it from the separate subjects with which it is related?
What is the linking thread which gathers these disparate branches into a single
discipline? My answer to these questions is simple—it is the art of programming
a computer. It is the art of designing efficient and elegant methods of getting
a computer to solve problems, theoretical or practical, small or large, simple or
complex. It is the art of translating this design into an effective and accurate
computer program. This is the art that must be mastered by a practising computer
scientist; the skill that is sought by numerous advertisements in the general and
technical press; the ability that must be fostered and developed by computer science
courses in universities.
C. A. R. Hoare
Computer Science (reprinted in [Hoa89])
ix
neither a book that adopts what some have called a breadth-first approach to computer
science, nor is it a book whose only purpose is to teach object-oriented programming in
the first course (although glimpses of both approaches will be evident).
Introductory courses are evolving to take advantage of new and current trends in
software engineering and programming language design, specifically object-oriented
design and programming. Some schools will adopt the approach that learning object-
oriented design principles should be the focus of a first programming course. Although
this approach certainly has some merit, students in the first course traditionally have
a very difficult time with the design of loops, functions, and programs. I believe that
attempting to cover object-oriented design in addition to these other design skills will not
be as conducive to a successful programming experience as will using object-oriented
concepts in the context of learning to program by reading and using classes before writing
them. This may seem a subtle distinction, but if the focus of the course is on learning
about the design and use of objects, there may be a tendency to delve too quickly and
too deeply into the details of C++.
The approach taken in this book is that C++ and OOP permit students with little
or no programming background to make great strides toward developing foundational
knowledge and expertise in programming. In subsequent courses students will hone the
skills that are first learned in the study of the material in this book and will expand the
coverage of computer science begun here. Computer science is not just programming,
and students in a first course in computer science must be shown something of what the
discipline is about. At the same time, programming provides a means of relating the
subdisciplines that compose compter science. Many of the examples and programs in
this book rely on classes, code, and libraries that are documented and supplied with the
book.
A major tenet of the approach used here is that students should read, modify, and
extend programs in conjunction with designing and writing from scratch. This is enabled
to a large extent by using the object-oriented features of C++ whenever appropriate. I
view C++ as a tool to be used rather than studied. One of the most important ideas
underlying the use of classes and objects in C++, and one of the most important concepts
in computer science, is the idea of abstraction.
Its [computer science’s] study involves development of the ability to abstract the
essential features of a problem and its solution, to reason effectively in the abstract
plane, without confusion by a mass of highly relevant detail. The abstraction must
then be related to the detailed characteristics of computers as the design of the
solution progresses; and it must culminate in a program in which all the detail
has been made explicit; and at this stage, the utmost care must be exercised to
ensure a very high degree of accuracy. … The need for abstract thought together
with meticulous accuracy, the need for imaginative speculation in the search for a
solution, together with a sound knowledge of the practical limitations of the tools
available for its implementation, the combination of formal rigour with a clear
style for its explanation to others—these are the combinations of attributes which
should be inculcated and developed in a student … and which must be developed
in high degree in students of computer science.
C. A. R. Hoare (reprinted in [Hoa89])
June 7, 1999 10:10 owltex Sheet number 10 Page number x magenta black
Students and teachers of computer science are not obliged to understand the IEEE
standards for floating-point numbers in order to write code that uses such numbers. Al-
though at one time a deep understanding of machine architecture was necessary in order
to write programs, this is no longer the case. Just as Hoare exhorts the programmer to be
articulate about his or her activity, this book is designed to bring the novice programmer
and student of computer science and program design to a point where such behavior is
possible. The use of C++ provides a mechanism for doing so in which details can be
revealed if and when it is appropriate to do so and hidden otherwise.
Programming in C++
Although this book uses C++ as a tool to be used rather than studied, students coming
out of a first course must be well prepared for subsequent courses in computer science
and other disciplines. Therefore, the essential features of C++ must be used, studied,
and mastered. The syntactic and semantic features of C++ sufficient for an introductory
course are thoroughly covered. At Duke, we teach our first courses using C++, and then
we move to Java. We have had great success with this approach. This book uses C++,
not C. In particular, there is no coverage of I/O using printf and scanf, there is no
coverage of C-style (char *) strings, and the coverage of C-style arrays is minimal and
included only because initializing an array with several values shortens code. Instead,
we use streams for I/O, the standard C++ class string, and a modification of the STL
vector class called tvector that performs range-checking on all vector accesses.
Many thought and programming exercises are integrated in the text, particularly in
the pause and reflect sections. These exercises are designed to make students think
about what they’re doing and to cover some of the messier language details in thought-
provoking and interesting ways. On-line materials accessible via the World Wide Web
provide supporting programming lab assignments.
Functions are introduced very early, but in a natural way that makes programming
with functions easier than without.
Strings are used before ints or doubles, though all are introduced early in the text
so that numerical examples can be mixed with text and string examples.
Whenever possible, the computer is exploited—small programs do not necessarily
equate with toy programs. The classes included in the text make this possible.
A large number of classes, programs, and libraries are supplied with the book.
Students will use the classes first, studying only their interfaces, before delving
into implementation and design issues.
Features of C++ that simplify programming are used, but not all features of C++
are emphasized. For example, since we use string and vector classes rather than
June 7, 1999 10:10 owltex Sheet number 11 Page number xi magenta black
xi
xii
Thanks
Many people have contributed to this book and the material in it, and I hope that many
more will. I must single out several people who have offered criticisms and suggestions
June 7, 1999 10:10 owltex Sheet number 13 Page number xiii magenta black
xiii
that have been extremely useful during the development of this project: Rich Pattis
(Carnegie Mellon University) and Dave Reed (Dickinson College). At Duke, Susan
Rodger taught using a draft of the first edition, waited patiently while chapters were
revised, and offered a nearly uncountable number of exercises, improvements, and pro-
grams. Her efforts have been very important in the development of this material. Greg
Badros (then at Duke) reviewed the entire manuscript of the first edition and offered abso-
lutely wonderful suggestions; he astonished me with his perspicacity. In the fall of 1995
David Levine used the first edition at Gettysburg College and made many constructive
suggestions based on this use. In the fall of 1996 Dee Ramm learned and taught using the
final draft, and made many useful suggestions. Through the auspices of McGraw-Hill,
Marjorie Anderson offered wonderful suggestions for improving the quality of the first
edition. Although I haven’t vanquished the passive voice, any progress is due to her
diligence, and all stylistic blunders are my own. Among the users of the first edition,
Beth Katz at Millersville University stands out for providing feedback that I’ve tried to
incorporate into this second edition.
The folks from McGraw-Hill involved with the second edition have been absolutely
wonderful. Betsy Jones, Emily Gray, and Amy Hill have helped with time, patience, and
support throughout the development of the second edition. John Rogosich at Techsetters
created LATEX macros and supplied support for those macros with great alacrity. Pat
Anton was my contact about the artwork at Techsetters; if it looks good it’s due to her,
and if it doesn’t it’s because I originated it all.
In addition, the following people have reviewed the material and offered many useful
suggestions both for the first edition and for this second edition (if I’ve left someone
out, I apologize): Robert Anderson, Deganit Armon, John Barr, Gail Chapman, Mike
Clancy, Robert Duvall, Arthur Farley, Sarah Fix, Donald Gotterbarn, Karen Hay, Andrew
Holey, Judy Hromcik, Beth Katz, David Kay, Joe Kmoch, Sharon Lee, Henry Leitner,
David Levine, Clayton Lewis, John McGrew, Jerry Mead, Judy Mullins, David Mutchler,
Richard Nau, Jeff Naughton, Chris Nevison, Bob Noonan, Richard Pattis, Robert Plantz,
Richard Prosl, Dave Reed, Margaret Reek, Stuart Reges, Stephen Schach, David Teague,
Beth Weiss, Lynn Zeigler
Development
The ideas and exercises in this book have been tested in the first course for majors at
Duke since 1993. Many people using the first edition contributed thoughts and ideas.
I’m grateful to all of them, especially students at Duke who saw many versions of the
material before it was a book.
Versions of all the programs used in the book are available for Windows, Unix, and
Macintosh operating systems. The software is currently available via anonymous ftp
from ftp.cs.duke.edu in pub/ola/book/ed2/code. It is also accessible via
the web at:
http://www.cs.duke.edu/csed/tapestry.
Although the first edition of the book went through extensive classroom testing, there are
undoubtedly errors that persist and new ones introduced with this edition. Nevertheless,
June 7, 1999 10:10 owltex Sheet number 14 Page number xiv magenta black
xiv
all code has been compiled and executed and is reproduced directly from the sources; it
is not retyped.
I will respond to all email regarding errors and will attempt to fix mistakes in subse-
quent printings. I would be ecstatic to hear about suggestions that might improve certain
sections, or comments about sections that caused problems even without suggestions for
improvement. Of course I love to hear that something worked well.
Please send all comments by email to
I will try to acknowledge all mail received. Materials for the book are also accessible
via the World Wide Web from the URL
http://www.cs.duke.edu/csed/tapestry/
A mailing list is available for discussing any aspects of the book or the course. To
subscribe, send email with the message
subscribe tapestry
unsubscribe tapestry
to the same address. To send mail to the list, use the address
Details
The second edition of the book was prepared using the LATEX package from Y & Y, Inc.
Macros and LATEX support were supplied by Techsetters, Inc. I used hardware donated
by Intel to Duke University running Windows NT donated by Microsoft. I also used
RedHat Linux 5.1 running on a (now old) Pentium 100. I tested all programs using
Codewarrior donated by Metrowerks, Visual C++ donated by Microsoft, and egcs C++
under Linux which is free from Cygnus Software. I used Emacs running under Windows
NT and the Unix-like shell for NT created by Cygnus; both were indispensable (I could
not survive without grep, for example). Screen images were captured using Snagit/32
and processed using SmartDraw Professional running under Windows NT. I also used
XV and Xfig running under Linux to create drawings that were ultimately massaged by
Techsetters using Adobe Photoshop. I printed preliminary versions of the manuscript
on a Tektronix Color Laser/Phaser 740 and used Adobe Distiller to create pdf files from
postscript.
June 7, 1999 10:10 owltex Sheet number 15 Page number xv magenta black
xv
Acknowledgments
To paraphrase Newton, the work in this book is not mine alone; I have stood on the
shoulders of giants. Of course Newton paraphrased Robert Burton, who said, “A dwarf
standing on the shoulders of a giant may see farther than a giant himself.” The styles used
in several books serve as models for different portions of this text. In particular, Eric
Roberts’ The Art and Science of C [Rob95] provided style guidelines for formatting;
the book A Logical Approach to Discrete Math [GS93] by David Gries and Fred B.
Schneider motivated the biographies; books by Bjarne Stroustrup [Str94, Str97] and
Scott Meyers [Mey92, Mey96] were indispensable in delving into C++. The way I
think about programming was changed by [GHJ95] and other work from the patterns
community. I’ve borrowed ideas from almost all of the textbooks I’ve read in 21 years
of teaching, so I acknowledge them en masse.
Thanks to Duke University and the Computer Science Department for providing an
atmosphere in which teaching is rewarded and this book is possible.
The research that led to the inclusion of patterns and the apprentice style of learning
used in this book was supported by the National Science Foundation under grant CCR-
9702550. This second edition was written during a sabbatical year in Vancouver, Canada
where the salmon is great, the city is wonderful, and the rain isn’t nearly as bad as people
lead you to believe.
Finally, thanks to Laura for always understanding.
Owen Astrachan
Vancouver, Canada 1999
June 7, 1999 10:10 owltex Sheet number 16 Page number xvi magenta black
xvi
June 7, 1999 10:10 owltex Sheet number 17 Page number xvii magenta black
Contents
1 Computer Science and Programming 3
1.1 What Is Computer Science? . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.1 The Tapestry of Computer Science . . . . . . . . . . . . . . . . . . 4
1.2 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.1 Arranging 13 Cards . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.2 Arranging 100,000 exams . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Computer Science Themes and Concepts . . . . . . . . . . . . . . . . . . 8
1.3.1 Theory, Language, Architecture . . . . . . . . . . . . . . . . . . . 8
1.3.2 Abstractions, Models, and Complexity . . . . . . . . . . . . . . . . 9
1.4 Language, Architecture, and Programs . . . . . . . . . . . . . . . . . . . 12
1.4.1 High- and Low-level Languages . . . . . . . . . . . . . . . . . . . 12
1.5 Creating and Developing Programs . . . . . . . . . . . . . . . . . . . . . 15
1.6 Language and Program Design . . . . . . . . . . . . . . . . . . . . . . . 18
1.6.1 Off-the-Shelf Components . . . . . . . . . . . . . . . . . . . . . . 19
1.6.2 Using Components . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.7 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
xvii
June 7, 1999 10:10 owltex Sheet number 18 Page number xviii magenta black
xviii
xix
xx
xxi
xxii
xxiii
xxiv
xxv
xxvi
List of Programs
Program 2.1: hello.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Program 2.2: hello2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Program 2.3: drawhead.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Program 2.4: parts.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Program 2.5: bday.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Program 2.6: bday2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Program 2.7: oldmac1.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Program 2.8: oldmac2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Program 2.9: order.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Program 2.10: order2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Program 3.1: macinput.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Program 3.2: fahrcels.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Program 3.3: daysecs.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Program 3.4: express.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Program 3.5: pizza.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Program 3.6: gfly.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Program 3.7: gballoonx.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Program 3.8: gfly2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Program 4.1: change.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Program 4.2: change2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Program 4.3: broccoli.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Program 4.4: noindent.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Program 4.5: monthdays.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Program 4.6: usemath.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Program 4.7: pizza2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Program 4.8: isleap.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Program 4.9: isleap2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Program 4.10: numtoeng.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Program 4.11: strdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Program 4.12: strfind.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Program 4.13: datedemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Program 5.1: revstring.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Program 5.2: fact.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Program 5.3: bigfact.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Program 5.4: primes.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Program 5.5: digits.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Program 5.6: threeloops.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Program 5.7: digitloops.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Program 5.8: windchill.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Program 5.9: multiply.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
xxvii
June 7, 1999 10:10 owltex Sheet number 28 Page number xxviii magenta black
xxviii
xxix
xxx
xxxi
Bibliography
823
June 7, 1999 10:10 owltex Sheet number 35 Page number 824 magenta black
824
[Emm93] Michele Emmer, ed. The Visual Mind: Art and Mathematics. MIT Press,
1993.
[Gar95] Simson Garfinkel. PGP: Pretty Good Privacy. O’Reilly & Associates, 1995.
[GHJ95] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design
Patterns: Elements of Reusable Object-Oriented Programming Addison-
Wesley, 1995
[Gol93] Herman H. Goldstine. The Computer from Pascal to von Neumann. Princeton
University Press, 1993.
[GS93] David Gries and Fred B. Schneider. A Logical Approach to Discrete Math.
Springer-Verlag, 1993.
[Har92] David Harel. Algorithmics, The Spirit of Computing 2nd. ed. Addison-Wesley,
1992.
[Hoa89] C.A.R. Hoare. Essays in Computing Science. ed. C.B. Jones. Prentice-Hall,
1989.
[Hod83] Andrew Hodges. Alan Turing: The Enigma. Simon & Schuster, 1983.
[JW89] William Strunk Jr. and E.B. White. The Elements of Style. 3rd. ed. MacMillan
Publishing Co., 1989.
[Knu98b] Donald E. Knuth. The Art of Computer Programming, vol. 3, Sorting and
Searching. 3rd ed. Addison-Wesley, 1998.
825
[McC79] Pamela McCorduck. Machines Who Think. W.H. Freeman and Company,
1979.
[MGRS91] Albert R. Meyer, John V. Gutag, Ronald L. Rivest, and Peter Szolovits, eds.
Research Directions in Computer Science: An MIT Perspective. MIT Press,
1991.
[Pat96] Richard E. Pattis. Get A-Life: Advice for the Beginning Object-Oriented
Programmer. Turing TarPit Press, 2000.
[Per87] Alan Perlis. The synthesis of algorithmic systems. In ACM Turing Award
Lectures: The First Twenty Years. ACM Press, 1987.
[Rob95] Eric S. Roberts. “Loop Exits and Structured Programming: Reopening the
Debate.” In Papers of the Twenty-Sixth SIGCSE Technical Symposium on
Computer Science Education, ACM Press, March 1995. SIGCSE Bulletin V.
27 N 1, pp. 268–272.
[Str94] Bjarne Stroustrup. The Design and Evolution of C++. Addison-Wesley, 1994.
[Str97] Bjarne Stroustrup. The C++ Programming Language. 3rd. ed. Addison-
Wesley, 1997.
[Wei94] Mark Allen Weiss. Data Structures and Algorithm Analysis in C++. Benjamin
Cummings, 1994.
[Wil56] M.V. Wilkes. Automatic Digital Computers. John Wiley & Sons, Inc., 1956.
June 7, 1999 10:10 owltex Sheet number 37 Page number 826 magenta black
826
[Wil87] Maurice V. Wilkes. Computers then and now. In ACM Turing Award Lectures:
The First Twenty Years, ACM Press, 1987, pp. 197–205.
Science and technology, and the various forms of art, all unite humanity in a single and
interconnected system.
Zhores Medvedev The Medvedev Papers
In this chapter we introduce you to computer science. Ideally, we would begin with a
simple definition that could be expanded and refined throughout the book. Unfortunately,
computer science, like other disciplines, has no simple definition. For example, we might
say that biology is the study of life. But that doesn’t explain much about the content
of such subdisciplines as animal behavior, immunology, or genetics—all of which are
part of biology. Nor does it explain much about the contributions that these disciplines
make to biology in general. Similarly, is English the study of grammar and spelling, the
reading of Shakespeare’s plays, or the writing of poems and stories? In many cases it
is easier to consider the subfields within an area of study than it is to define the area of
study. So it is with computer science.
3
June 7, 1999 10:10 owltex Sheet number 23 Page number 4 magenta black
This book will guide you through the study of the design, building, and analysis of
computer programs. Although you won’t become an expert by working through this
book, you will lay a foundation on which expertise can be built. Wherever possible,
the programming examples will solve problems that are difficult to solve without a
computer: a program might find the smallest of 10,000 numbers, rather than the smallest
of 2 numbers. Longer examples are taken from various core areas of computer science.
As this is a book about the design and analysis of computer programs, it must be used
in conjunction with a computer. Reading alone cannot convey the same understanding
that using, reading, and writing programs can.
This chapter introduces computer science using a tapestry metaphor. A tapestry has
much in common with computer science. A tapestry has many intricate scenes that
form a whole. Similarly, computer science is a broad discipline with many intricate
subdisciplines. In studying a tapestry, we can step back and view the work as a whole,
move closer to concentrate on some particularly alluring or colorful region, and even
study the quality of the fabric itself. We’ll similarly explore computer science—studying
some things in detail, but stepping back to view the whole when appropriate. We’ll
view programs as tapestries too. You’ll study programs written by others, add to these
programs to make them more useful, and write your own programs. You’ll see that
creating and developing programs is not only useful but is immensely satisfying, and
often entertaining as well.
Several unifying threads run through a tapestry, and the various scenes and sections
originate from and build on these threads. Likewise in computer science, we find basic
themes and concepts on which the field is built and that we use to write programs and
solve problems. In this chapter we introduce the themes of computer science, which are
like the scenes in a tapestry, and the concepts, which are like the unifying threads.
Contexture is a word meaning both “an arrangement of interconnected parts” and
“the act of weaving (assembling) parts into a whole.” It can apply to tapestries and to
computer programming. This book uses a contextural approach in which programming
is the vehicle for learning about computer science. Although it is possible to study
computer science without programming, it would be like studying food and cooking
without eating, which would be neither as enjoyable nor as satisfying.
Computer science is not just programming. Too often this is the impression left after
an initial exposure to the field. I want you to learn something of what a well-read and
well-rounded computer scientist knows. You should have an understanding of what has
been done, what might be done, and what cannot be done by programming a computer.
After a brief preview of what is ahead, we’ll get to it.
June 7, 1999 10:10 owltex Sheet number 24 Page number 5 magenta black
1.2 Algorithms 5
Alan Turing was one of the founders of computer science, studying it before there
were computers! To honor his work, the highest achievement in the field of com-
puter science—and the equivalent in stature to a Nobel prize—is the Turing award,
given by the Association for Computing Ma-
chinery (the ACM).
In 1937, Turing published the paper On
Computable Numbers, with an Application
to the Entscheidungsproblem. In this paper
he invented an abstract machine, now known
as a Turing Machine, that is (theoretically)
capable of doing any calculation that today’s
supercomputers can. He used this abstract
machine to show that there are certain prob-
lems in mathematics whose proofs cannot
be found. This also shows that there are
certain problems that cannot be solved with
any computer. In particular, a program can-
not be written that will determine whether
an arbitrary program will eventually stop. This is called the halting problem.
During World War II, Turing was instrumental in breaking a German coding
machine called the Enigma. He was also very involved with the design of the first
computers in England and the United States. During this time, Turing practiced
one of his loves—long-distance running. A newspaper account said of his second-
place finish (by 1 foot) in a 3-mile race in a time of 15:51: “Antithesis of the popular
notion of a scientist is tall, modest, 34-year-old bachelor Alan M. Turing.…Turing
is the club’s star distance runner…[and] is also credited with the original idea for
the Automatic Computing Engine, popularly known as the Electronic Brain.”
Turing was also fond of playing “running-chess,” in which each player alter-
nated moves with a run around Turing’s garden. Turing was gay and, unfortunately,
the 1940s and 50s were not a welcome time for homosexuals. He was found guilty
of committing “acts of gross indecency” in 1952 and sentenced to a regimen of
hormones as a “cure.” More than a year after finishing this “therapy,” and with no
notice, Turing committed suicide in 1954.
For a full account of Turing’s life see[Hod83].
1.2 Algorithms
To develop an initial understanding of the themes and concepts that make up the computer
science tapestry, we’ll work through an example. Consider two similar tasks of arranging
objects into some predetermined order:
June 7, 1999 10:10 owltex Sheet number 25 Page number 6 magenta black
Arranging Cards
Arrange cards into four groups by suit: spades, hearts, clubs, diamonds
Sort each group. To sort a group:
For each rank (2, 3, 4, …, 10, J, Q, K, A) put the 2 first, followed
by the 3, the 4, …, followed by the 10, J, Q, K, A (if any rank is
missing, skip it)
Card players often do the first task because it makes playing much simpler than if
the cards in their hands are arranged in a random order. The second task is part of
the administration of the Advanced Placement exams given each year to high school
students. Many people are hired to sort the exam booklets by student ID number before
the scores are entered into a computer. In both cases people are doing the arranging. The
differences in the scale of the tasks and the techniques used to solve them will illuminate
the study of computer science and problem solving.
1.2 Algorithms 7
sorted hand of cards or a loaf of bread). Although this analogy is apt, cooking often
allows for a larger margin of error than do algorithms that are to be implemented on a
computer. Phrases like “beat until smooth,” “sauté until tender,” and “season to taste”
are interpreted differently by cooks. A more appropriate analogy may be seen with
the instructions that are used to knit a sweater or make a shirt. In such tasks, precise
instructions are given and patterns must be followed or else a sweater with a front larger
than the back or a shirt with mismatched buttons and buttonholes may result.
You can easily determine that the hand below is sorted correctly, in part because
there are so few cards in a hand and because grouping cards by suit makes it easier to see
if the cards are sorted. Verifying that the algorithm is correct in general is much more
difficult than verifying that one hand of cards is sorted.
7 8 9 7 10 K 3 5 Q A 8 J Q
7 8 9 7 10 K 3 5 Q A 8 J Q
For example, suppose that an algorithm correctly sorts 1,000 hands of cards. Does
this guarantee that the algorithm will sort all hands? No, it’s possible that the next hand
might not be sorted even though the first 1,000 hands were. This points out an important
difference between verifying an algorithm and testing an algorithm. A verified algorithm
has been proved to work in all situations. A tested algorithm has been rigorously tried
with many examples to establish confidence that it works in all situations.
These represent a small fraction of the number of exam booklets that must be arranged.
Consider the algorithmic description in Figure 1.2. If this algorithm is implemented
correctly, it will result in 32 numbers arranged from smallest to largest. If we had a
computer to assist with the task, this might be an acceptable algorithm. (We’ll see later
that there are more efficient methods for use on a computer but that this is a method that
works and is simple to understand.) We might be tempted to use it with 32 exams, but
with 100,000 exams it would be extremely time-consuming and would make inefficient
use of the resources at our disposal since using 40 people to find the smallest exam
number is a literal waste of time.
June 7, 1999 10:10 owltex Sheet number 27 Page number 8 magenta black
Sorting Exams
Repeat the following until all 32 numbers (exams) have been arranged
scan the list of numbers (exams) looking for the smallest exam
move the smallest number (exam) to another pile of exams that are maintained and
arranged from smallest to largest
Theory Language
Architecture
Levels Conceptual
of and formal
abstraction models
Efficiency
and
complexity
tapestry we are studying. In programming and computer science, these terms concern
how difficult a problem is and the computational resources, such as time and memory,
that a problem requires.
We have avoided many of the details inherent in these examples that might be of
concern as rough ideas evolve into detailed algorithms. If 40 people are sorting exams
we might be concerned, for example, with how many are left-handed. This might
affect the arrangement of the exams as they are physically moved about during the
sorting process. Some playing cards are embellished with beautiful designs; it might
be necessary to explain to someone who has never played cards that these designs are
irrelevant in the arrangement process. In general these levels of detail are examples of
levels of abstraction (Figure 1.4). In one sense this entire chapter mirrors the fact that
we are viewing the computer science tapestry at a very high level of abstraction, with
few details. Each subsequent chapter of this book involves a study of some aspect of the
tapestry at a level of greater detail.
Finally, both these tasks involve numbers. We all have an idea of what a number is,
although the concept of number may be different to a mathematician and to an accountant.
In computer science conceptual ideas must often be formalized to be well understood.
For example, telling someone who is playing hide-and-go-seek to start counting from
1 and to stop when they reach the “last number” is an interesting way to teach the
concept of infinity. The finite memory of computers, however, imposes a limit on the
largest number that can be represented. This difference between conceptual and formal
models is a concept that will recur and that completes the three concepts in Figure 1.4,
forming common threads of the computer science tapestry.
Pause to Reflect 1.1 The New Hacker’s Dictionary defines bogo-sort as described here.
Using this “algorithm,” what is the minimum number of “throws” that yields a
sorted hand? What is the danger of using this algorithm?
1.2 In the algorithm for sorting cards, nothing is stated about forming a hand from
each of the separate suits. Does something need to be stated? Is too much left
as “understood by the player” so that someone unfamiliar with cards couldn’t use
the algorithm?
1.3 Write a concise description of the method or algorithm you use to sort a hand of
cards.
1.4 Suppose that the 32 student ID numbers listed in the text are sorted. Is it a simple
matter to verify that the numbers are in the correct order? Consider the same
question for 100,000 numbers.
Discovering Diverse Content Through
Random Scribd Documents
— Katerina Ivanovna ymmärtää kaiken, — lausui äkkiä Aljoša
juhlallisesti, — ymmärtää kaiken tämän murheen koko syvyyden ja
sopii. Hän on erittäin älykäs ja hän näkee itse, että ei voi olla
onnettomampi kuin sinä.
— Mitä?
— Isän luo?
— Grušenjkaa?
— Ihmeeseen?
— Odotan. Minä ymmärrän, ettei se käy pian, ettei voi tulla noin
vain ja mennä oikopäätä asiaan. Hän on nyt humalassa. Minä
odotan kolme tuntia taikka neljä taikka viisi taikka kuusi taikka
seitsemän, mutta tiedäkin, että tänään, vaikkapa sydänyöllä, menet
Katerina Ivanovnan luo rahojen kanssa tai ilman rahoja ja sanot:
käski sanoa teille terveisiä. Tahdon nimenomaan, että sanot tämän
lauselman: »Käski näet sanoa terveisiä.»
— Mitja! Entä jos yht'äkkiä Grušenjka tulee tänään… tai jos ei
tänään, niin huomenna tai ylihuomenna?
— Entä jos…
6.
Smerdjakov
Hän tapasi isänsä todellakin vielä pöydässä. Pöytä oli, kuten aina,
katettu saliin, vaikka talossa oli myös varsinainen ruokasali. Tämä
sali oli talon suurin huone ja vanhanaikaisen komeasti kalustettu.
Kalusto oli hyvin vanha, valkoinen, ja sen vanha, punainen päällys
oli puolisilkkinen. Seinille ikkunoitten väliin oli asetettu kuvastimia,
joitten koreat kehykset oli koristettu vanhanaikaisilla leikkauksilla.
Nekin olivat valkeat ja kullalla koristetut. Seiniä peittivät valkoiset,
monin paikoin jo halkeilleet seinäpaperit, ja niitä koristi kaksi isoa
muotokuvaa. Toinen esitti jotakuta ruhtinasta, joka
kolmisenkymmentä vuotta sitten oli ollut sen seudun
kenraalikuvernöörinä, toinen jotakuta niinikään jo aikoja sitten
kuollutta piispaa. Etunurkassa oli muutamia pyhimyskuvia, joitten
eteen yöksi sytytettiin lamppu… ei niin paljon hartaudesta kuin sen
vuoksi, että huone olisi yöllä valaistu. Fjodor Pavlovitš kävi
tavallisesti kovin myöhään yöllä makaamaan, kello kolme tai neljä
aamulla, ja siihen asti hän tavallisesti kaiken aikaa käveli huoneessa
tai istui nojatuolissa mietiskellen. Se oli tullut hänelle tavaksi. Hän
vietti yönsä usein aivan yksin talossa ja lähetti palvelijat
sivurakennukseen, mutta enimmäkseen hänen luokseen yöksi jäi
palvelija Smerdjakov, joka nukkui eteisessä laatikkopenkillä. Aljošan
tullessa sisälle oli päivällinen jo lopussa ja pöytään oli tuotu hilloa ja
kahvia. Fjodor Pavlovitš nautti mielellään päivällisen jälkeen jotakin
imelää konjakin kanssa. Ivan Fjodorovitš oli myös pöydän ääressä ja
joi kahvia. Palvelijat, Grigori ja Smerdjakov, seisoivat pöydän luona.
Sekä herrat että palvelijat olivat selvästi erittäin hyvällä tuulella.
Fjodor Pavlovitš nauraa hohotti kovalla äänellä. Aljoša kuuli jo
eteiseen hänen vinkuvan, ennestään niin tutun naurunsa ja päätteli
heti naurun sävystä, että isä ei vielä ollut sanottavasti humalassa,
vaan toistaiseksi vain hellällä mielellä.
— Kas siinä on hänkin, siinä on hänkin! — uikutti Fjodor Pavlovitš
ilostuen yht'äkkiä suuresti Aljošan tulosta. — Yhdy joukkoomme, käy
istumaan, ota kahvia, — sehän on paastoruokaa, mutta kuumaa ja
hyvää! Konjakkia en tarjoa, sillä sinä paastoat, mutta ehkäpä tahdot?
Et, minä annan mieluummin sinulle likööriä, sinä oiva poika! —
Smerdjakov, käy kaapilla, toisella hyllyllä oikealla, tässä ovat
avaimet, pian!
— Olen syönyt, — vastasi Aljoša, joka itse asiassa oli syönyt vain
palan leipää ja juonut lasin kaljaa igumenin keittiössä. — Kuumaa
kahvia minä juon mielelläni.
— Vastaa, hölmö.
7.
Kiista
Mutta Bileamin aasintamma oli äkkiä alkanut puhua. Sattui
omituinen aihe: ollessaan aamulla kauppias Lukjanovin puodissa
ostoksilla kuuli Grigori tältä kertomuksen eräästä venäläisestä
sotamiehestä, joka jossakin kaukana rajalla joutui aasialaisten
vangiksi ja jota nämä pakottivat uhkaamalla kidutusta ja kuolemaa
luopumaan kristinuskosta ja kääntymään islamiin, mutta joka ei
suostunut luopumaan uskostaan, vaan antautui kärsimyksiin, antoi
nylkeä itseltään nahan ja kuoli kiittäen ja ylistäen Kristusta. Tästä
urotyöstä oli kerrottu juuri sinä päivänä saapuneessa
sanomalehdessä. Grigori rupesi puhumaan siitä pöydässä. Fjodor
Pavlovitš oli ennenkin joka kerta aterioituaan ja jälkiruokaa
nauttiessaan mielellään naureskellut ja puhellut, jos ei muiden, niin
vaikkapa Grigorin kanssa. Tällä kertaa hän oli kevyellä ja hauskasti
vilkkaalla mielellä. Maistellessaan konjakkiaan hän kuultuaan
kertomuksen huomautti, että sellaisesta sotamiehestä pitäisi heti
tehdä pyhimys ja nyljetty nahka olisi vietävä johonkin luostariin:
»Sinnepä vasta tulvisi väkeä ja rahaa.» Grigori rypisti kulmiaan
huomatessaan, ettei Fjodor Pavlovitš ollut ollenkaan tullut
liikutetuksi, vaan alkoi ainaisen tapansa mukaisesti pilkata. Silloin
äkkiä oven luona seisova Smerdjakov naurahti. Smerdjakovin oli
sangen usein ennenkin sallittu seisoa pöydän läheisyydessä
päivällisen lopulla. Siitä asti kuin Ivan Fjodorovitš oli saapunut
kaupunkiimme, oli Smerdjakov alkanut tulla päivälliselle melkein joka
kerta.
— Konna hän on, se hän on! — lausui äkkiä Grigori. Hän katsoi
vihaisesti Smerdjakovia suoraan silmiin.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookultra.com