100% found this document useful (3 votes)
16 views

Complete Download A Computer Science Tapestry Exploring Programming and Computer Science with C 2nd edition Owen L. Astrachan PDF All Chapters

Exploring

Uploaded by

ciriagagyor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
16 views

Complete Download A Computer Science Tapestry Exploring Programming and Computer Science with C 2nd edition Owen L. Astrachan PDF All Chapters

Exploring

Uploaded by

ciriagagyor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Visit https://ebookultra.

com to download the full version and


explore more ebooks

A Computer Science Tapestry Exploring Programming


and Computer Science with C 2nd edition Owen L.
Astrachan

_____ Click the link below to download _____


https://ebookultra.com/download/a-computer-science-
tapestry-exploring-programming-and-computer-science-
with-c-2nd-edition-owen-l-astrachan/

Explore and download more ebooks at ebookultra.com


Here are some recommended products that might interest you.
You can download now and explore!

Cambridge IGCSE Computer Science Programming Book 1st


Edition Richard Morgan.

https://ebookultra.com/download/cambridge-igcse-computer-science-
programming-book-1st-edition-richard-morgan/

ebookultra.com

Python Programming An Introduction to Computer Science


John M. Zelle

https://ebookultra.com/download/python-programming-an-introduction-to-
computer-science-john-m-zelle/

ebookultra.com

Mathematical Structures for Computer Science 6th Edition


Judith L. Gersting

https://ebookultra.com/download/mathematical-structures-for-computer-
science-6th-edition-judith-l-gersting/

ebookultra.com

Satisficing Games and Decision Making With Applications to


Engineering and Computer Science 1st Edition Wynn C.
Stirling
https://ebookultra.com/download/satisficing-games-and-decision-making-
with-applications-to-engineering-and-computer-science-1st-edition-
wynn-c-stirling/
ebookultra.com
Introduction to Lattice Theory with Computer Science
Applications 1st Edition Garg

https://ebookultra.com/download/introduction-to-lattice-theory-with-
computer-science-applications-1st-edition-garg/

ebookultra.com

The Art and Science of Computer Animation 2nd Edition


Stuart Mealing

https://ebookultra.com/download/the-art-and-science-of-computer-
animation-2nd-edition-stuart-mealing/

ebookultra.com

Great Jobs for Computer Science Majors 2nd Edition Jan


Goldberg

https://ebookultra.com/download/great-jobs-for-computer-science-
majors-2nd-edition-jan-goldberg/

ebookultra.com

Computer Science Illuminated Revised Nell B. Dale

https://ebookultra.com/download/computer-science-illuminated-revised-
nell-b-dale/

ebookultra.com

Great ideas in computer science with java 1st Edition Alan


W. Biermann

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

A Computer Science Tapestry

Exploring Programming and Computer Science


with C++
June 7, 1999 10:10 owltex Sheet number 2 Page number ii magenta black

ii

A Computer Science Tapestry

Exploring Programming and Computer Science


with C++

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

About the Author


Owen L. Astrachan is Associate Professor of the Practice of Computer Science at Duke
University and the department’s Director of Undergraduate Studies for Teaching and
Learning. After receiving his A. B. degree from Dartmouth College, he taught high
school for seven years before returning to graduate school. He received his Ph.D. in
Computer Science from Duke in 1992. Professor Astrachan was a member of the Duke
programming team that placed fourth in the world in the ACM programming contest
in 1989 and coached the third place team in 1994. He was the chief Reader for the
Advanced Placement Computer Science Exam from 1990 to 1994. Professor Astra-
chan has written many technical and pedagogical articles and has been the Principal
Investigator in three NSF-sponsored educational projects: “The Applied Apprenticeship
Approach: An Object-Oriented/Object-Based Framework for CS2,” “CURIOUS: Cen-
ter for Undergraduate Education and Research: Integration through Performance and
Visualization,” and “Using and Developing Design Patterns.” A well-regarded teacher,
Professor Astrachan received the 1995 Robert B. Cox Distinguished Teaching in Science
Award.
June 7, 1999 10:10 owltex Sheet number 6 Page number vi magenta black

vi

To my teachers, colleagues, and friends, especially to those who are all three, for
educating, arguing, laughing, and helping.

To Laura and Ethan


June 7, 1999 10:10 owltex Sheet number 7 Page number vii magenta black

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:

A strong grounding in the analysis, construction, and design of programs and


programming.
A means for honing problem-solving skills associated with the study of computer
programming and a taste of both the science and engineering aspects of program-
ming.
An introduction to computer science that gives the student more of an idea of what
the discipline is about than most introductory programming texts.

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])

A supporting view is expressed in the following quote:


Programming is unquestionably the central topic of computing.
In addition to being important, programming is an enormously exciting in-
tellectual activity. In its purest form, it is the systematic mastery of complexity.
For some problems, the complexity is akin to that associated with designing a fine
mechanical watch, i.e., discovering the best way to assemble a relatively small
number of pieces into a harmonious and efficient mechanism. For other problems,
the complexity is more akin to that associated with putting a man on the moon,
i.e., managing a massive amount of detail.
In addition to being important and intellectually challenging, programming is
a great deal of fun. Programmers get to build things and see them work. What
could be more satisfying?
John V. Guttag
Why Programming Is Too Hard and What to Do about It in [MGRS91]

Programming and Computer Science


This is more than a book about programming. Although its principal focus is on pro-
gramming using C++, this is also a book about computer science. However, this is
June 7, 1999 10:10 owltex Sheet number 9 Page number ix magenta black

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.

A Closer View of the CS Tapestry


This book is different from most other introductory programming contexts in several
ways:

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

pointer-based C-style objects, there is no reason to cover copy constructors or as-


signment operators. These topics are covered in the text, but there’s no compelling
reason to cover them.

How to Use the Book


I do not cover every section of the book in my courses, and instructors who used the first
edition indicated that they skip some sections as well. I’ll provide an overview of how
chapters can be covered, but the best recommendations will be your own after looking
at the material. I’ll also post sample syllabi on the book’s web site as people using the
book send the syllabi to me.

The How to Sections


The How to sections are new to this second edition. One of the common complaints from
users of the first edition was that it was not an ideal reference. Material on language-
specific features of C++ was introduced as needed so that related material was not
always found together. To address this valid concern, I have created How to sections
that condense C++ specific topics into a series of appendices, making it easier to use the
book as a reference as well as a textbook. The How to sections are referenced in the text
by a flying carpet icon as shown to the left, with the relevant How to referenced in the text.
For example, How to B provides detailed information on using streams and formatting
output. By including the material in the How to appendix, it can be found quickly and it
doesn’t clutter a more general discussion of computer science and programming design
with C++ specifics.

Chapter Coverage and Dependencies


Chapter 1 is an overview of computer science and programming. None of the material
is used in subsequent chapters, though covering Chapter 1 doesn’t take much time and
sets a tone for using the book.

Part 1: Foundations of C++ Programming


Chapters 2 through 5 cover material essential to what is covered in the rest of the text.
However certain sections in this part can be skipped or treated less thoroughly since the
material is repeated in other contexts later. The Balloon class used in Section 3.4
introduces a simple and compelling class, but the section can be skipped since the
material on classes is studied again in Section 5.4. It’s also possible to cover all the
control statements early, then use the examples and classes introduced in Chapters 2
through 5. Chapters 2 through 5 should take less time to cover than Chapters 6 through
8. In general, the chapters later in the book take more time to digest than the earlier
chapters, but offer more material.
June 7, 1999 10:10 owltex Sheet number 12 Page number xii magenta black

xii

Part 2: Program and Class Construction: Extending the Foun-


dation
The material in Chapters 6 through 8, combined with earlier material, will form the basis
of many first courses. It’s possible to use sections of chapters from Part 3 to augment
the material in the first eight chapters as noted below.
For those who prefer to cover vectors early, it’s possible to cover Sections 6.1, 6.2
and 7.4, then cover Chapter 8. The material in Section 8.4 on built-in arrays is completely
optional. The class tvector is modeled after the STL class vector, but performs
range-checking for the overloaded indexing operator. The discussion of tvector relies
on the method push_back for adding elements to a vector so that the vector resizes
itself as needed. The differences between size and capacity for vectors are emphasized
in Chapter 8.
The class WordStreamIterator introduced in Section 6.3 can be omitted each
time it’s used, though it’s much easier to use the class to read a file more than once
within the same program than using the stream functions described in How to B to reset
a stream. The material on sets of strings in Section 6.5 is used in later chapters, but it
can be skipped each time it’s covered. The random walk classes discussed in Section 7.3
can be skipped, though they’re used later in discussing inheritance and pointers.

Part 3: Design, Use, and Analysis: Building on the Foundation


Chapters 9 through 13 provide a wealth of material. It’s unlikely that all the chapters
can be covered in a single semester.
In general, most of the chapters in this part are independent of each other, though not
completely. The material in Chapter 13 can be covered early, though it uses pointers.
A quick discussion of allocation using new can finesse the use of pointers since the
pointers are used to store vectors of elements in an inheritance hierarchy, not for linked
structures.
Chapter 9, which covers getline, string streams, and overloaded operators, and
Chapter 10 on recursion, can be covered in any order. Most of the material is not used
in subsequent chapters, though the getline function is used in several examples and
recursion is used in quick sort. These chapters could be covered before Chapter 8 on
vectors except that the example of recursion in Section 10.3.3 that permutes the elements
in a vector. The material on immutable lists in Section 10.5 can be skipped though it is
used in a few examples in later chapters.
Most of material in Chapter 11 can be skipped entirely or covered immediately after
covering vectors. Section 11.3 on function objects is optional, though it’s the right way
of sorting by several criteria and function objects are important in the STL and the Java
Collections classes.

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

[email protected]

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

as the message body to

[email protected]

To unsubscribe, send the message

unsubscribe tapestry

to the same address. To send mail to the list, use the address

[email protected]

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

I Foundations of C++ Programming 27


2 C++ Programs: Form and Function 29
2.1 Simple C++ Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.1.1 Syntax and Semantics . . . . . . . . . . . . . . . . . . . . . . . . 31
2.2 How a Program Works . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.2.1 Flow of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.3 What Can Be Output? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.4 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.5 Functions with Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.5.1 What Is a Parameter? . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.5.2 An Example of Parameterization: Happy Birthday . . . . . . . . . 45
2.5.3 Passing Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.6 Functions with Several Parameters . . . . . . . . . . . . . . . . . . . . . 51
2.7 Program Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.7.1 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.8 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

xvii
June 7, 1999 10:10 owltex Sheet number 18 Page number xviii magenta black

xviii

3 Program Design and Implementation 67


3.1 The Input Phase of Computation . . . . . . . . . . . . . . . . . . . . . . 68
3.1.1 The Input Stream, cin . . . . . . . . . . . . . . . . . . . . . . . . 69
3.1.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.2 Processing Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
3.2.1 Numeric Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.2.2 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . 77
3.2.3 Evaluating Expressions . . . . . . . . . . . . . . . . . . . . . . . . 79
3.2.4 The type char . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.3 Case Study: Pizza Slices . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.3.1 Pizza Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.4 Classes and Types: An Introduction . . . . . . . . . . . . . . . . . . . . 86
3.4.1 Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.4.2 Reading Programs . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.4.3 Private and Public . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.5 Compiling and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.6 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
3.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

4 Control, Functions, and Classes 99


4.1 The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . 100
4.2 Choices and Conditional Execution . . . . . . . . . . . . . . . . . . . . . 103
4.2.1 The if/else Statement . . . . . . . . . . . . . . . . . . . . . . . 105
4.3 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
4.3.1 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . 108
4.3.2 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
4.3.3 Short-Circuit Evaluation . . . . . . . . . . . . . . . . . . . . . . . 112
4.3.4 Arithmetic Assignment Operators . . . . . . . . . . . . . . . . . . 113
4.4 Block Statements and Defensive Programming . . . . . . . . . . . . . . . 114
4.4.1 Defensive Programming Conventions . . . . . . . . . . . . . . . . 116
4.4.2 Cascaded if/else Statements . . . . . . . . . . . . . . . . . . . 119
4.5 Functions That Return Values . . . . . . . . . . . . . . . . . . . . . . . . 124
4.5.1 The Math Library <cmath> . . . . . . . . . . . . . . . . . . . . . 127
4.5.2 Pre- and Post-conditions . . . . . . . . . . . . . . . . . . . . . . . 129
4.5.3 Function Return Types . . . . . . . . . . . . . . . . . . . . . . . . 130
4.6 Class Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 138
4.6.1 string Member Functions . . . . . . . . . . . . . . . . . . . . . 138
4.6.2 Calling and Writing Functions . . . . . . . . . . . . . . . . . . . . 141
4.6.3 The Date class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
4.7 Using Boolean Operators: De Morgan’s Law . . . . . . . . . . . . . . . . 145
4.8 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
June 7, 1999 10:10 owltex Sheet number 19 Page number xix magenta black

xix

5 Iteration with Programs and Classes 155


5.1 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
5.1.1 Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
5.1.2 Loops and Mathematical Functions . . . . . . . . . . . . . . . . . 159
5.1.3 Computing Factorials . . . . . . . . . . . . . . . . . . . . . . . . . 160
5.1.4 Computing Prime Numbers . . . . . . . . . . . . . . . . . . . . . 164
5.1.5 Kinds of Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
5.1.6 Efficiency Considerations . . . . . . . . . . . . . . . . . . . . . . 168
5.1.7 Exponentiation: A Case Study in Loop Development . . . . . . . . 169
5.1.8 Numbers Written in English . . . . . . . . . . . . . . . . . . . . . 175
5.1.9 Fence Post Problems . . . . . . . . . . . . . . . . . . . . . . . . . 177
5.2 Alternative Looping Statements . . . . . . . . . . . . . . . . . . . . . . . 179
5.2.1 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
5.2.2 The Operators ++ and −− . . . . . . . . . . . . . . . . . . . . . 181
5.2.3 The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . 182
5.2.4 Pseudo-Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . 183
5.2.5 Choosing a Looping Statement . . . . . . . . . . . . . . . . . . . . 185
5.2.6 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
5.2.7 Defining Constants . . . . . . . . . . . . . . . . . . . . . . . . . . 190
5.3 Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
5.4 Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
5.4.1 The Date Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
5.4.2 The Dice Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
5.4.3 Testing the Dice Class . . . . . . . . . . . . . . . . . . . . . . . . 200
5.5 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
5.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

II Program and Class Construction


Extending the Foundation 213
6 Classes, Iterators, and Patterns 215
6.1 Classes: From Use to Implementation . . . . . . . . . . . . . . . . . . . 215
6.1.1 Class Documentation: The Interface (.h File) . . . . . . . . . . . . 215
6.1.2 Comments in .h Files . . . . . . . . . . . . . . . . . . . . . . . . . 216
6.1.3 Class Documentation: the Implementation or .cpp File . . . . . . . 218
6.1.4 Member Function Implementation . . . . . . . . . . . . . . . . . . 221
6.1.5 Scope of Private Variables . . . . . . . . . . . . . . . . . . . . . . 222
6.2 Program Design with Functions . . . . . . . . . . . . . . . . . . . . . . . 224
6.2.1 Evaluating Classes and Code: Coupling and Cohesion . . . . . . . 226
6.2.2 Toward a Class-based Quiz Program . . . . . . . . . . . . . . . . . 227
6.2.3 Reference parameters . . . . . . . . . . . . . . . . . . . . . . . . . 228
6.2.4 Pass by Value and Pass by Reference . . . . . . . . . . . . . . . . . 231
6.2.5 const Reference Parameters . . . . . . . . . . . . . . . . . . . . 233
6.3 Reading Words: Stream Iteration . . . . . . . . . . . . . . . . . . . . . . 236
6.3.1 Recommended Problem-solving and Programming Steps . . . . . . 237
June 7, 1999 10:10 owltex Sheet number 20 Page number xx magenta black

xx

6.3.2 A Pseudocode Solution . . . . . . . . . . . . . . . . . . . . . . . . 237


6.3.3 Solving a Related Problem . . . . . . . . . . . . . . . . . . . . . . 240
6.3.4 The Final Program: Counting Words . . . . . . . . . . . . . . . . . 242
6.3.5 Streams Associated with Files . . . . . . . . . . . . . . . . . . . . 245
6.3.6 Type Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
6.3.7 A Word-Reading Class Using ifstream . . . . . . . . . . . . . . 249
6.4 Finding Extreme Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
6.4.1 Largest/Smallest Values . . . . . . . . . . . . . . . . . . . . . . . 253
6.4.2 Initialization: Another Fence Post Problem . . . . . . . . . . . . . 254
6.4.3 Word Frequencies . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
6.4.4 Using the CTimer class . . . . . . . . . . . . . . . . . . . . . . . 258
6.5 Case Study: Iteration and String Sets . . . . . . . . . . . . . . . . . . . . 261
6.5.1 Iterators and the strutils.h Library . . . . . . . . . . . . . . . . . . 263
6.5.2 The Type ofstream . . . . . . . . . . . . . . . . . . . . . . . . 263
6.5.3 Sets and Word Counting . . . . . . . . . . . . . . . . . . . . . . . 265
6.6 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
6.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
7 Class Interfaces, Design, and Implementation 279
7.1 Designing Classes: From Requirements to Implementation . . . . . . . . 279
7.1.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
7.1.2 Nouns as Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
7.1.3 Verbs as Member Functions (Methods) . . . . . . . . . . . . . . . 281
7.1.4 Finding Verbs Using Scenarios . . . . . . . . . . . . . . . . . . . . 281
7.1.5 Assigning Responsibilities . . . . . . . . . . . . . . . . . . . . . . 283
7.1.6 Implementing and Testing Classes . . . . . . . . . . . . . . . . . . 284
7.1.7 Implementing the Class Quiz . . . . . . . . . . . . . . . . . . . . 287
7.1.8 Implementing the Class Question . . . . . . . . . . . . . . . . . 289
7.1.9 Sidebar: Converting int and double Values to strings . . . . 291
7.2 A Conforming Interface: a new Question Class . . . . . . . . . . . . . 302
7.2.1 Using the New Question Class . . . . . . . . . . . . . . . . . . 302
7.2.2 Creating a Program . . . . . . . . . . . . . . . . . . . . . . . . . . 303
7.2.3 The Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
7.2.4 The Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
7.2.5 The Linker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
7.2.6 A New Question Class . . . . . . . . . . . . . . . . . . . . . . 308
7.3 Random Walks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
7.3.1 One-Dimensional Random Walks . . . . . . . . . . . . . . . . . . 312
7.3.2 Selection with the switch Statement . . . . . . . . . . . . . . . . 314
7.3.3 A RandomWalk Class . . . . . . . . . . . . . . . . . . . . . . . . 316
7.3.4 A Two-Dimensional Walk Class . . . . . . . . . . . . . . . . . . . 323
7.3.5 The Common Interface in RandomWalk and RandomWalk2D . . 329
7.4 structs as Data Aggregates . . . . . . . . . . . . . . . . . . . . . . . 331
7.4.1 structs for Storing Points . . . . . . . . . . . . . . . . . . . . . 333
7.4.2 Operators for structs . . . . . . . . . . . . . . . . . . . . . . . 335
7.5 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
June 7, 1999 10:10 owltex Sheet number 21 Page number xxi magenta black

xxi

7.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337


8 Arrays, Data, and Random Access 341
8.1 Arrays and Vectors as Counters . . . . . . . . . . . . . . . . . . . . . . . 342
8.1.1 An Introduction to the Class tvector . . . . . . . . . . . . . . . 345
8.1.2 Counting with tvectors . . . . . . . . . . . . . . . . . . . . . . 346
8.2 Defining and Using tvectors . . . . . . . . . . . . . . . . . . . . . . . . . 349
8.2.1 tvector Definition . . . . . . . . . . . . . . . . . . . . . . . . . 349
8.2.2 tvector Initialization . . . . . . . . . . . . . . . . . . . . . . . 350
8.2.3 tvector Parameters . . . . . . . . . . . . . . . . . . . . . . . . 350
8.2.4 A tvector Case Study: Shuffling CD Tracks . . . . . . . . . . . 354
8.3 Collections and Lists Using tvectors . . . . . . . . . . . . . . . . . . 359
8.3.1 Size and Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
8.3.2 Using push_back, resize, and reserve . . . . . . . . . . . 360
8.3.3 Vector Idioms: Insertion, Deletion, and Searching . . . . . . . . . . 365
8.3.4 Insertion into a Sorted Vector . . . . . . . . . . . . . . . . . . . . . 368
8.3.5 Deleting an Element Using pop_back . . . . . . . . . . . . . . . 370
8.3.6 Searching a Vector . . . . . . . . . . . . . . . . . . . . . . . . . . 371
8.3.7 Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
8.3.8 Comparing Sequential and Binary Search . . . . . . . . . . . . . . 377
8.4 Built-in Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
8.4.1 Defining an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
8.4.2 Initializing an Array . . . . . . . . . . . . . . . . . . . . . . . . . 383
8.4.3 Arrays as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 384
8.5 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
8.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390

III Design, Use, and Analysis


Extending the Foundation 397
9 Strings, Streams, and Operators 399
9.1 Characters: Building Blocks for Strings . . . . . . . . . . . . . . . . . . 400
9.1.1 The Type char as an Abstraction . . . . . . . . . . . . . . . . . . 400
9.1.2 The Library <cctype> . . . . . . . . . . . . . . . . . . . . . . . 403
9.1.3 Strings as char Sequences . . . . . . . . . . . . . . . . . . . . . . 405
9.2 Streams and Files as Lines and Characters . . . . . . . . . . . . . . . . . 408
9.2.1 Input Using getline() . . . . . . . . . . . . . . . . . . . . . . 409
9.2.2 Parsing Line-Oriented Data Using istringstream . . . . . . . 413
9.2.3 Output Using ostringstream . . . . . . . . . . . . . . . . . . 415
9.2.4 Strings, Streams, and Characters . . . . . . . . . . . . . . . . . . . 416
9.3 Case Study: Removing Comments with State Machines . . . . . . . . . . 419
9.3.1 Counting Words . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
9.3.2 Problem Specification: What Is a Comment? . . . . . . . . . . . . 421
9.3.3 A State Machine Approach to I/O . . . . . . . . . . . . . . . . . . 421
9.3.4 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . 426
9.4 Case Study: Overloaded Operators and the ClockTime Class . . . . . . 428
June 7, 1999 10:10 owltex Sheet number 22 Page number xxii magenta black

xxii

9.4.1 Throw-Away Code vs. Class Design . . . . . . . . . . . . . . . . . 430


9.4.2 Implementing the ClockTime Class . . . . . . . . . . . . . . . . 431
9.4.3 Class or Data Invariants . . . . . . . . . . . . . . . . . . . . . . . 434
9.4.4 Overloaded Operators . . . . . . . . . . . . . . . . . . . . . . . . 435
9.4.5 Friend Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
9.4.6 Overloaded operator << . . . . . . . . . . . . . . . . . . . . 436
9.4.7 Overloaded Relational Operators . . . . . . . . . . . . . . . . . . . 437
9.4.8 Overloaded operator + and += . . . . . . . . . . . . . . . . . 437
9.4.9 Testing the ClockTime Class . . . . . . . . . . . . . . . . . . . . 438
9.4.10The Final Program . . . . . . . . . . . . . . . . . . . . . . . . . . 440
9.5 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443

10 Recursion, Lists, and Matrices 451


10.1Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
10.1.1Similar and Simpler Functions . . . . . . . . . . . . . . . . . . . . 451
10.1.2General Rules for Recursion . . . . . . . . . . . . . . . . . . . . . 456
10.1.3Infinite Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
10.2Recursion and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . 460
10.2.1Classes for Traversing Directories . . . . . . . . . . . . . . . . . . 461
10.2.2Recursion and Directory Traversal . . . . . . . . . . . . . . . . . . 462
10.2.3Properties of Recursive Functions . . . . . . . . . . . . . . . . . . 468
10.3Comparing Recursion and Iteration . . . . . . . . . . . . . . . . . . . . . 469
10.3.1The Factorial Function . . . . . . . . . . . . . . . . . . . . . . . . 469
10.3.2Fibonacci Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 473
10.3.3Permutation Generation . . . . . . . . . . . . . . . . . . . . . . . 476
10.4Scope and Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
10.4.1Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
10.4.2Hidden Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
10.4.3Static Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
10.4.4Static or Class Variables and Functions . . . . . . . . . . . . . . . 486
10.5Case Study: Lists and the Class Clist . . . . . . . . . . . . . . . . . . 488
10.5.1What Is a CList Object? . . . . . . . . . . . . . . . . . . . . . . 489
10.5.2Tail-ing Down a list . . . . . . . . . . . . . . . . . . . . . . . . . . 491
10.5.3Cons-ing Up a List . . . . . . . . . . . . . . . . . . . . . . . . . . 493
10.5.4Append, Reverse, and Auxiliary Functions . . . . . . . . . . . . . 495
10.5.5Polynomials Implemented with Lists . . . . . . . . . . . . . . . . . 501
10.5.6CList and Sparse, Sequential Structures . . . . . . . . . . . . . . 502
10.6The class tmatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
10.6.1A Simple tmatrix Program . . . . . . . . . . . . . . . . . . . . 506
10.6.2Case Study: Finding Blobs . . . . . . . . . . . . . . . . . . . . . . 508
10.7Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
10.8Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
June 7, 1999 10:10 owltex Sheet number 23 Page number xxiii magenta black

xxiii

11 Sorting, Templates, and Generic Programming 527


11.1Sorting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
11.1.1Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
11.1.2Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
11.2Function Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
11.2.1Printing a tvector with a Function Template . . . . . . . . . . . 538
11.2.2Function Templates and Iterators . . . . . . . . . . . . . . . . . . . 541
11.2.3Function Templates, Reuse, and Code Bloat . . . . . . . . . . . . . 545
11.3Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
11.3.1The Function Object Comparer . . . . . . . . . . . . . . . . . . 546
11.3.2Predicate Function Objects . . . . . . . . . . . . . . . . . . . . . . 551
11.4Analyzing Sorts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
11.4.1O Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
11.4.2Worst Case and Average Case . . . . . . . . . . . . . . . . . . . . 558
11.4.3Analyzing Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . 559
11.5Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
11.5.1The Partition/Pivot Function . . . . . . . . . . . . . . . . . . . . . 563
11.5.2Analysis of Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . 566
11.6Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
11.7Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
12 Dynamic Data, Lists, and Class Templates 573
12.1Pointers as Indirect References . . . . . . . . . . . . . . . . . . . . . . . 573
12.1.1What is a Pointer? . . . . . . . . . . . . . . . . . . . . . . . . . . 573
12.1.2Heap Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
12.1.3Sharing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
12.1.4Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 583
12.1.5Pointers for Sharing . . . . . . . . . . . . . . . . . . . . . . . . . 584
12.1.6Interdependencies, Class Declarations, and Header Files . . . . . . 585
12.1.7Delete and Destructors . . . . . . . . . . . . . . . . . . . . . . . . 591
12.2Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
12.2.1Creating Nodes with Linked Lists . . . . . . . . . . . . . . . . . . 600
12.2.2Iterating over a Linked List . . . . . . . . . . . . . . . . . . . . . . 601
12.2.3Adding a Last Node to a Linked List . . . . . . . . . . . . . . . . . 602
12.2.4Deleting Nodes in a Linked List . . . . . . . . . . . . . . . . . . . 602
12.2.5Splicing Nodes into a Linked List . . . . . . . . . . . . . . . . . . 604
12.2.6Doubly and Circularly Linked Lists, Header Nodes . . . . . . . . . 608
12.3A Templated Class for Sets . . . . . . . . . . . . . . . . . . . . . . . . . 612
12.3.1Sets of Strings With Linked Lists . . . . . . . . . . . . . . . . . . 613
12.3.2Searching, Clearing, Helper Functions . . . . . . . . . . . . . . . . 614
12.3.3Iterators and Friend Functions . . . . . . . . . . . . . . . . . . . . 616
12.3.4Interactive Testing . . . . . . . . . . . . . . . . . . . . . . . . . . 618
12.3.5Deep Copy, Assignment, and Destruction . . . . . . . . . . . . . . 622
12.3.6A Templated Version of LinkStringSet . . . . . . . . . . . . . 627
12.4Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
12.5Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
June 7, 1999 10:10 owltex Sheet number 24 Page number xxiv magenta black

xxiv

13 Inheritance for Object-Oriented Design 643


13.1Essential Aspects of Inheritance . . . . . . . . . . . . . . . . . . . . . . 643
13.1.1The Inheritance Hierarchy for Streams . . . . . . . . . . . . . . . . 644
13.1.2An Inheritance Hierarchy: Math Quiz Questions . . . . . . . . . . 646
13.1.3Implementing Inheritance . . . . . . . . . . . . . . . . . . . . . . 649
13.1.4Public Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 651
13.1.5Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
13.1.6Protected Data Members . . . . . . . . . . . . . . . . . . . . . . . 656
13.2Using an Abstract Base Class . . . . . . . . . . . . . . . . . . . . . . . . 657
13.2.1Abstract Classes and Pure Virtual Functions . . . . . . . . . . . . . 659
13.2.2When Is a Method virtual? . . . . . . . . . . . . . . . . . . . . 663
13.3Advanced Case Study: Gates, Circuits, and Design Patterns . . . . . . . . 670
13.3.1An Introduction to Gates and Circuits . . . . . . . . . . . . . . . . 670
13.3.2Wires, Gates, and Probes . . . . . . . . . . . . . . . . . . . . . . . 672
13.3.3Composite Gates and Connectors . . . . . . . . . . . . . . . . . . 674
13.3.4Implementation of the Wire and Gate Classes . . . . . . . . . . . 682
13.3.5Gates and Wires: Observers and Observables . . . . . . . . . . . . 684
13.3.6Encapsulating Construction in WireFactory . . . . . . . . . . . 686
13.3.7Refactoring: Creating a BinaryGate Class . . . . . . . . . . . . 688
13.3.8Interactive Circuit Building . . . . . . . . . . . . . . . . . . . . . . 692
13.3.9SimpleMap: Mapping Names to Gates . . . . . . . . . . . . . . . 697
13.4Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698
13.5Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699

A How to: Use Basic C++, Syntax and Operators 707


A.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
A.1.1 The Function main . . . . . . . . . . . . . . . . . . . . . . . . . . 707
A.1.2 Built-in and Other Types . . . . . . . . . . . . . . . . . . . . . . . 707
A.1.3 Variable Definition and Assignment . . . . . . . . . . . . . . . . . 708
A.1.4 C++ Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
A.1.5 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
A.2 Functions and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
A.2.1 Defining and Declaring Functions and Classes . . . . . . . . . . . . 713
A.2.2 Importing Classes and Functions: #include . . . . . . . . . . . 715
A.2.3 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
A.2.4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
A.2.5 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
A.2.6 Command-line Parameters . . . . . . . . . . . . . . . . . . . . . . 718

B How to: Format Output and Use Streams 721


B.1 Formatting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
B.1.1 General and Floating-Point Formatting . . . . . . . . . . . . . . . 721
B.1.2 Manipulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
B.1.3 Stream Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
B.2 Random Access Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
B.3 I/O Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
June 7, 1999 10:10 owltex Sheet number 25 Page number xxv magenta black

xxv

C How to: Use the Class string 733


C.1 The Class string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
C.1.1 Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
C.1.2 Conversion to/from C-style Strings . . . . . . . . . . . . . . . . . . 734
C.2 String Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 734
C.2.1 Adding Characters or Strings . . . . . . . . . . . . . . . . . . . . . 734
C.2.2 Using Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
C.2.3 Finding (Sub)strings and Characters . . . . . . . . . . . . . . . . . 737
D How to: Understand and Use const 739
D.1 Why const? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
D.1.1 Literal Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 740
D.2 const Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . 740
D.2.1 Overloading on const . . . . . . . . . . . . . . . . . . . . . . . . 742
D.3 Mutable Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
D.4 Pointers and const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
D.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
E How to: Overload Operators 747
E.1 Overloading Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
E.2 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
E.2.1 Binary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
E.2.2 Arithmetic Assignment Operators . . . . . . . . . . . . . . . . . . 750
E.3 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
E.4 I/O Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
E.4.1 The Function tostring() . . . . . . . . . . . . . . . . . . . . . 756
E.5 Constructors and Conversions . . . . . . . . . . . . . . . . . . . . . . . . 757
F How to: Understand and Use Standard Libraries 759
F.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
F.1.1 The Library <cmath> . . . . . . . . . . . . . . . . . . . . . . . . 759
F.1.2 The Library <cctype> . . . . . . . . . . . . . . . . . . . . . . . 760
F.2 Constants and Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
F.2.1 Limits in <climits> . . . . . . . . . . . . . . . . . . . . . . . . 761
F.2.2 Double Limits in <cfloat> . . . . . . . . . . . . . . . . . . . . 762
F.2.3 Limits in <limits> . . . . . . . . . . . . . . . . . . . . . . . . . 763
F.2.4 ASCII Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
G How to: Understand and Use Tapestry Classes 767
G.1 A Library of Useful Classes . . . . . . . . . . . . . . . . . . . . . . . . . 767
G.1.1 Summary of Classes and Functions . . . . . . . . . . . . . . . . . 767
G.1.2 Implementations of Tapestry Classes . . . . . . . . . . . . . . . . . 768
G.2 Header Files for Tapestry Classes . . . . . . . . . . . . . . . . . . . . . . 769
G.2.1 Prompting Functions in prompt.h . . . . . . . . . . . . . . . . . 769
G.2.2 The Class Date . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
G.2.3 The Class Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
G.2.4 The Class RandGen . . . . . . . . . . . . . . . . . . . . . . . . . 774
June 7, 1999 10:10 owltex Sheet number 26 Page number xxvi magenta black

xxvi

G.2.5 The Class CTimer . . . . . . . . . . . . . . . . . . . . . . . . . . 775


G.2.6 The Class WordStreamIterator . . . . . . . . . . . . . . . . 775
G.2.7 The Class StringSet . . . . . . . . . . . . . . . . . . . . . . . 777
G.2.8 The String Functions in strutils.h . . . . . . . . . . . . . . . 778
G.2.9 The Math Helper Functions in mathutils.h . . . . . . . . . . . 778
G.2.10The struct Point . . . . . . . . . . . . . . . . . . . . . . . . . . 779
G.2.11The Classes in directory.h . . . . . . . . . . . . . . . . . . . 780
G.2.12The Class CList . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
G.2.13The Class Poly . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
G.2.14The Sorting Functions in sortall.h . . . . . . . . . . . . . . . 787
G.2.15Header Files from Circuit Simulation . . . . . . . . . . . . . . . . 789
G.2.16The Map Class SimpleMap . . . . . . . . . . . . . . . . . . . . . 794
H How to: Use the Graphics Classes in canvas.h 797
H.1 The Graphics Library: TOOGL 1.0 . . . . . . . . . . . . . . . . . . . . . 797
H.2 Using the Canvas Class . . . . . . . . . . . . . . . . . . . . . . . . . . 798
H.2.1 Canvas Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798
H.2.2 Drawing, Styles, and Colors . . . . . . . . . . . . . . . . . . . . . 798
H.2.3 Drawing Shapes and Text . . . . . . . . . . . . . . . . . . . . . . . 800
H.3 Using the AnimatedCanvas Class . . . . . . . . . . . . . . . . . . . . 803
H.3.1 The Shape Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . 803
H.3.2 Properties of Shape Objects . . . . . . . . . . . . . . . . . . . . . 804
H.3.3 Using Shapes: addShape and clone . . . . . . . . . . . . . . . 805
H.3.4 The CompositeShape Class . . . . . . . . . . . . . . . . . . . 807
H.3.5 Processing Mouse and Key Events . . . . . . . . . . . . . . . . . . 809
H.3.6 Animations with Bouncer and Mover . . . . . . . . . . . . . . . 812
H.3.7 Canvas Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
H.3.8 Specifying Color with Class CanvasColor . . . . . . . . . . . . 817
H.3.9 The Class Key in key.h . . . . . . . . . . . . . . . . . . . . . . . . 818
I How to: Cope with C++ Environments 819
I.1 Coping with Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
I.1.1 Keeping Current . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
I.2 Creating a C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . . 820
I.2.1 The Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
I.2.2 The Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822
I.2.3 The Linker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822
June 7, 1999 10:10 owltex Sheet number 27 Page number xxvii magenta black

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

Program 5.10: usedate.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194


Program 5.11: roll.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Program 5.12: tryroll.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Program 5.13: testdice.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Program 6.1: dice.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Program 6.2: simpquiz.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Program 6.3: simpquiz2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Program 6.4: pbyvalue.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Program 6.5: constref.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Program 6.6: sentinel.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Program 6.7: countw.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Program 6.8: countw2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Program 6.9: mindata.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Program 6.10: mindata2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Program 6.11: maxword.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Program 6.12: usetimer.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Program 6.13: maxword2time.cpp . . . . . . . . . . . . . . . . . . . . . . . . 260
Program 6.14: setdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Program 6.15: setdemo2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Program 6.16: maxword3.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Program 6.17: listcount.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Program 7.1: studentstub.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Program 7.2: mainstub.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Program 7.3: quizstub.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Program 7.4: question.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Program 7.5: numtostring.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Program 7.6: mathquest.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Program 7.7: mathquest.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Program 7.8: quiz.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Program 7.9: capquest.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Program 7.10: frogwalk.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Program 7.11: walk.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Program 7.12: frogwalk2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Program 7.13: walk.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Program 7.14: brownian.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Program 7.15: twodwalk.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Program 7.16: usepoint.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Program 8.1: dieroll.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Program 8.2: dieroll2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Program 8.3: letters.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Program 8.4: shuffle.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Program 8.5: growdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Program 8.6: stocks.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Program 8.7: timesearch.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Program 8.8: fixlist.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Program 9.1: charlist.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
June 7, 1999 10:10 owltex Sheet number 29 Page number xxix magenta black

xxix

Program 9.2: spreader.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405


Program 9.3: filelines.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Program 9.4: readnums.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Program 9.5: filelines2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Program 9.6: wc.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Program 9.7: decomment.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Program 9.8: enumdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Program 9.9: clockt.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Program 9.10: useclock.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Program 9.11: cdsum.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Program 10.1: digits2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Program 10.2: digits3.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Program 10.3: recdepth.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Program 10.4: files.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Program 10.5: subdir.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Program 10.6: facttest.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Program 10.7: fibtest.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Program 10.8: usepermuter.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 479
Program 10.9: recfib.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Program 10.10: scope.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Program 10.11: recfib2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Program 10.12: staticdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 487
Program 10.13: listdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Program 10.14: listsize.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Program 10.15: readlist.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Program 10.16: listappend.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 495
Program 10.17: listreverse.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 498
Program 10.18: polydemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Program 10.19: polymult.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Program 10.20: polycount.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Program 10.21: matdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Program 10.22: bitmapdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . 508
Program 10.23: blobs.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Program 10.24: nqueenpartial.cpp . . . . . . . . . . . . . . . . . . . . . . . . 523
Program 11.1: selectsort1.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Program 11.2: selectsort2.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Program 11.3: insertsort.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Program 11.4: timequadsorts.cpp . . . . . . . . . . . . . . . . . . . . . . . . 534
Program 11.5: sortwlen.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Program 11.6: countiter.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Program 11.7: uniqueiter.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Program 11.8: strlensort.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Program 11.9: sortstocks.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Program 11.10: dirvecfun.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Program 11.11: checkselect.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 557
Program 11.12: checkinsert.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 560
June 7, 1999 10:10 owltex Sheet number 30 Page number xxx magenta black

xxx

Program 12.1: pointerdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 574


Program 12.2: sharetoy.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Program 12.3: walkdesign.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Program 12.4: frogwalk3.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Program 12.5: strlink.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Program 12.6: orderedlist.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Program 12.7: listremove.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 608
Program 12.8: linkstringsetiterator.h . . . . . . . . . . . . . . . . . . . . . . 617
Program 12.9: testlinkset.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 618
Program 12.10: linksetdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . 622
Program 12.11: linkset.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
Program 12.12: linkset.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Program 12.13: wordgame.h . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Program 12.14: wordgame.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 640
Program 13.1: streaminherit.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 644
Program 13.2: inheritquiz.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 646
Program 13.3: mathquestface.h . . . . . . . . . . . . . . . . . . . . . . . . . 650
Program 13.4: mathquestface.cpp . . . . . . . . . . . . . . . . . . . . . . . . 653
Program 13.5: questface.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
Program 13.6: whatsthequizmain.cpp . . . . . . . . . . . . . . . . . . . . . . 660
Program 13.7: whatstheface.h . . . . . . . . . . . . . . . . . . . . . . . . . . 662
Program 13.8: inheritdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 664
Program 13.9: gatetester.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Program 13.10: gatewiredemo.cpp . . . . . . . . . . . . . . . . . . . . . . . 673
Program 13.11: xorgate.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Program 13.12: xordemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Program 13.13: gwinteraction.cpp . . . . . . . . . . . . . . . . . . . . . . . . 683
Program 13.14: simplemapdemo.cpp . . . . . . . . . . . . . . . . . . . . . . 697
Program 13.15: singleton.h . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
Program A.1: namespacedemo.cpp . . . . . . . . . . . . . . . . . . . . . . . 715
Program A.2: mainargs.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
Program B.1: formatdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Program B.2: streamflags.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 726
Program B.3: formatparams.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 727
Program B.4: binaryfiles.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 730
Program C.1: stringdemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 736
Program E.1: datecomps.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 753
Program F.1: oldlimits.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
Program F.2: limits.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
Program G.1: prompt.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
Program G.2: date.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
Program G.3: dice.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
Program G.4: randgen.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
Program G.5: ctimer.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Program G.6: worditer.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Program G.7: stringset.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
June 7, 1999 10:10 owltex Sheet number 31 Page number xxxi magenta black

xxxi

Program G.8: strutils.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778


Program G.9: mathutils.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
Program G.10: point.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
Program G.11: directory.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780
Program G.12: clist.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Program G.13: poly.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
Program G.14: sortall.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
Program G.15: wires.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789
Program G.16: gates.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791
Program G.17: simplemap.h . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
Program H.1: circles.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
Program H.2: drawshapes.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 800
Program H.3: grid.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
Program H.4: abcshape.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
Program H.5: bouncedemo.cpp . . . . . . . . . . . . . . . . . . . . . . . . . 806
Program H.6: bouncefish.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . 808
Program H.7: circlefun.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
Program H.8: sketchpad.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
Program H.9: fishforth.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . 814
Program H.10: molebouncer.cpp . . . . . . . . . . . . . . . . . . . . . . . . 815
June 7, 1999 10:10 owltex Sheet number 32 Page number 1 magenta black

(first empty page)


June 7, 1999 10:10 owltex Sheet number 33 Page number 2 magenta black

(second empty page)


June 7, 1999 10:10 owltex Sheet number 34 Page number 823 magenta black

Bibliography

[AA85] Donald J. Albers and G.L. Alexanderson. Mathematical People. Birkhäuser,


1985.
[ACM87] ACM. Turing Award Lectures: The First Twenty Years 1966–1985. ACM
Press, 1987.
[AS96] Harold Abelson and Gerald Jay Sussman. Structure and Interpretation of
Computer Programs. 2nd. ed. MIT Press and McGraw-Hill, 1996.
[Asp90] William Aspray. Computing Before Computers. Iowa State University Press,
1990.
[Aus98] Matthew H. Austern Generic Programming and the STL. Addison-Wesley,
1998.
[Ben86] Jon Bentley. Programming Pearls. Addison-Wesley, 1986.
[Ben88] Jon Bentley. More Programming Pearls. Addison-Wesley, 1988.
[Ble90] Guy E. Blelloch. Vector Models for Data-Parallel Computing. MIT Press,
1990.
[Boo91] Grady Booch. Object-Oriented Design with Applications. Benjamin Cum-
mings, 1991.
[Boo94] Grady Booch. Object-Oriented Design and Analysis with Applications. 2nd.
ed. Benjamin Cummings, 1994.
[BRE71] I. Barrodale, F.D. Roberts, and B.L. Ehle. Elementary Computer Applications
in Science Engineering and Business. John Wiley & Sons Inc., 1971.
[Coo87] Doug Cooper. Condensed Pascal. W.W. Norton, 1987.
[Dij82] Edsger W. Dijkstra. Selected Writings on Computing: A Personal Perspec-
tive. Springer-Verlag, 1982.
[DR90] Nachum Dershowitz and Edward M. Reingold. “Calendrical Calculations.”
Software-Practice and Experience 20, (September 1990) pp. 899–928.
[(ed91] Allen B. Tucker (ed.). Computing Curricula 1991 Report of the ACM/IEEE-
CS Joint Curriculum Task Force. ACM Press, 1991.
[EL94] Susan Epstein and Joanne Luciano, eds. Grace Hopper Celebration of
Women in Computing. Computing Research Association, 1994. Hopper-
[email protected].

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.

[G9̈5] Denise W. Gürer. “Pioneering women in Computer Science.” Communica-


tions of the ACM 38, (January 1995) pp 45–54.

[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.

[Gri74] David Gries. “On Structured Programming—A Reply to Smoliar.” Commu-


nications of the ACM 17, 11 (1974), pp 655–657.

[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.

[Hor92] John Horgan. Claude E. Shannon. IEEE Spectrum, April 1992.

[JW89] William Strunk Jr. and E.B. White. The Elements of Style. 3rd. ed. MacMillan
Publishing Co., 1989.

[Knu97] Donald E. Knuth. The Art of Computer Programming, vol. 1, Fundamental


Algorithms. 3rd ed. Addison-Wesley, 1997.

[Knu98a] Donald E. Knuth. The Art of Computer Programming, vol. 2, Seminumerical


Algorithms. 3rd. ed. Addison-Wesley, 1998.

[Knu98b] Donald E. Knuth. The Art of Computer Programming, vol. 3, Sorting and
Searching. 3rd ed. Addison-Wesley, 1998.

[KR78] Brian W. Kernighan and Dennis Ritchie. The C Programming Language.


Prentice-Hall, 1978.

[KR96] Samuel N. Kamin and Edward M. Reingold. Programming with class: A


C++ Introduction to Computer Science. McGraw-Hill, 1996.

[Mac92] Norman Macrae. John von Neumann. Pantheon Books, 1992.


June 7, 1999 10:10 owltex Sheet number 36 Page number 825 magenta black

825

[McC79] Pamela McCorduck. Machines Who Think. W.H. Freeman and Company,
1979.

[McC93] Steve McConnell. Code Complete. Microsoft Press, 1993.

[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.

[Neu95] Peter G. Neumann. Computer Related Risks. Addison-Wesley, 1995.

[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.

[PL90] Przemyslaw Prusinkiewicz and Aristid Lindenmayer. The Algorithmic Beauty


of Plants. Springer-Verlag, 1990.

[RDC93] Edward M. Reingold, Nachum Dershowitz, and Stewart M. Clamen. ”Calen-


drical Calculations, II: Three Historical Calendars.” Software-Practice and
Experience, 23, (April 1993), pp. 383–404.

[Rie96] Arthur Riel. Object-Oriented Design Heuristics. Addison-Wesley, 1996.

[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.

[Rob95] Eric S. Roberts. The Art and Science of C. Addison-Wesley, 1995.

[Sla87] Robert Slater. Portraits in Silicon. MIT Press, 1987.

[Str87] Bjarne Stroustrup. The C++ Programming Language. Addison-Wesley,


1987.

[Str94] Bjarne Stroustrup. The Design and Evolution of C++. Addison-Wesley, 1994.

[Str97] Bjarne Stroustrup. The C++ Programming Language. 3rd. ed. Addison-
Wesley, 1997.

[Mey92] Scott Meyers. Effective C++. Addison-Wesley, 1992.

[Mey96] Scott Meyers. More Effective C++. Addison-Wesley, 1996.

[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.

[Wil95] Maurice V. Wilkes. Computing Perspectives. Morgan Kaufmann, 1995.


[Wir87] Niklaus Wirth. From Programming Language Design to Compiler Construc-
tion. In ACM Turing Award Lectures: The First Twenty Years. ACM Press,
1987.
June 7, 1999 10:10 owltex Sheet number 22 Page number 3 magenta black

Computer Science and


Programming 1
The computer is no better than its program.
Elting Elmore Morison
Men, Machines and Modern Times

Science and technology, and the various forms of art, all unite humanity in a single and
interconnected system.
Zhores Medvedev The Medvedev Papers

I want to reach that state of condensation of sensations which constitutes a picture.


Henri Matisse
Notes d’un Peintre

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.

1.1 What Is Computer Science?


In some respects, computer science is a new discipline; it has grown and evolved along
with the growth of computing technology and the cheaper, faster, and more accessible
processing power of modern-day computers. As recently as 1970, many colleges and
universities did not even have departments of computer science. But computer science
has benefited from work done in such older disciplines as mathematics, psychology,
electrical engineering, physics, and linguistics. Computer science inherits characteristics
from all these fields in ways that we’ll touch on in this book, but the thread that links
these and the many subdisciplines of computer science is computer programming.
Some people prefer the term used in many European languages, informatics, over
what is called computer science in the United States. Computer science is more the
study of managing and processing information than it is the study of computers. Com-
puter science is more than programming, but programming is at the core of information
processing and computer science.

3
June 7, 1999 10:10 owltex Sheet number 23 Page number 4 magenta black

4 Chapter 1 Computer Science and Programming

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.

1.1.1 The Tapestry of Computer Science

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 (1913–1954)

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

6 Chapter 1 Computer Science and Programming

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)

Figure 1.1 Arranging cards in order.

1. A hand of cards (arrange by rank and suit)


2. 100,000 exams (arrange by six-digit student ID number)

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.1 Arranging 13 Cards


A hand of cards might look like this:
10 5 Q 7 9 A 8 J 3 7 K Q 8
10 5 Q 7 9 A 8 J 3 7 K Q 8
Most people arrange cards in order by suits (spades, hearts, diamonds, and clubs), and
within suit by rank (2, …, 10, J, Q, K, A) with little thought. In fact, many people perform
a slightly different sequence of steps in arranging different hands of cards, modifying
their basic technique depending on the order in which the cards are dealt. However,
if you are asked to describe the process of arranging a hand of cards to someone who
has never seen cards before, the task becomes difficult. The careful description of such
processes is one of the fundamental parts of computer science. The descriptions are
called algorithms and are the focus of much study in computer science and in this book.
The algorithm for sorting cards shown in Figure 1.1 is both correct and concise, two
traits to strive for in writing algorithms. The instructions to sort a group are applicable to
all groups, not just to the spades or to the diamonds. Instructions that apply in more than
one situation are much more versatile than instructions that apply in a single situation.
Algorithms are often compared to recipes used in cooking: they are step-by-step
plans used in some process (arranging cards or baking bread) to arrive at some end (a
June 7, 1999 10:10 owltex Sheet number 26 Page number 7 magenta black

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.

1.2.2 Arranging 100,000 exams


Arranging 100,000 exams by ID number is a much more cumbersome task than arranging
13 cards. Imagine being confronted with 100,000 exams to sort. Where would you
begin? This task is more time-consuming and more prone to error than arranging cards.
Although you probably don’t need a precise description of the card-arranging algorithm
to sort cards correctly, you’ll need to think carefully about developing an algorithm to sort
100,000 exams using 40 people as assistants. Utilizing these “computational assistants”
requires communication and organization beyond what is needed to arrange 13 cards in
one person’s hand. A sample of 32 student ID numbers is shown here:

672029 662497 118183 452603 637238 249262 617834 396939


483595 613046 361999 231519 695368 689831 346006 539184
712077 816735 540778 975985 950610 846581 931662 625487
278827 821759 131232 952606 547825 385646 880295 816645

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

8 Chapter 1 Computer Science and Programming

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

Figure 1.2 Arranging exams in order.

1.3 Computer Science Themes and Concepts


The previous sorting example provides a context for the broad set of themes and concepts
that comprise computer science.

1.3.1 Theory, Language,Architecture


Three areas mentioned in [Ble90] as forming the core of computer science serve nicely as
the essential themes, linking the various scenes of the computer science tapestry together.
These themes are shown in Figure 1.3. Although we can develop algorithms for both
sorting tasks, it would be useful to know if there are better algorithms or if there is a
“best” algorithm in the sense that it can be proven to be the most efficient. Determining
whether an algorithm is “better” than another may not be relevant for arranging cards
because a nonoptimal algorithm will probably still work quickly. However, a “good”
algorithm is very relevant when arranging 100,000 exams. Developing algorithms and
evaluating them is the part of computer science known as theory.

Theory Language

Architecture

Figure 1.3 Essential computer science themes.


June 7, 1999 10:10 owltex Sheet number 28 Page number 9 magenta black

1.3 Computer Science Themes and Concepts 9

Levels Conceptual
of and formal
abstraction models

Efficiency
and
complexity

Figure 1.4 Recurring concepts

If the algorithms are to be implemented on a computer or used by people (who are in


some sense “computational engines”), there must be a language in which the algorithms
are expressed. We have noted that cooking recipes, while similar to algorithms, often
leave room for ambiguity. Although English (or other natural languages) may at some
point become a viable language in which to “instruct” computers, specialized computer
languages are needed now. Many programming languages exist, and often the choice
of language has a large impact on how well a program is written and on how fast it
is developed. Languages are necessary to implement algorithms on specific kinds of
computers.
Although both these arranging tasks are similar, an algorithm for one may be inap-
propriate for the other. Viewing a person as a computational resource (or processor), we
see that the card-arranging task is done using one processor while the exam-arranging
task is done using several processors. Just as some people can sort cards more quickly
than others, some computer processors are faster and work differently than other proces-
sors. The term architecture is used to describe how a computer is put together just as it
is used to describe how a building is put together. One active research area in computer
science involves developing algorithms and architectures for multiprocessor computing
systems.

1.3.2 Abstractions, Models, and Complexity


In this section we continue our contextural approach, whereby we weave the essential
themes into the fabric that is computer science and the scenes that make up its tapestry. In
addition to the themes of theory, language, and architecture, we’ll often refer to several
of the recurring concepts presented in [(ed91]. These form part of the foundation on
which computer science is built; they are shown in Figure 1.4.
Both sorting tasks involve arranging things, yet the complexity of the second task
makes it imperative that an efficient algorithm be used if the goal is to be achieved within a
reasonable time frame. Both efficiency and complexity are parts of the computer science
June 7, 1999 10:10 owltex Sheet number 29 Page number 10 magenta black

10 Chapter 1 Computer Science and Programming

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.

Bogo-sort: Repeatedly throw a hand of cards in the air, picking them up


at random, and stopping the process when examining the hand reveals the
cards are in order.

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ä.

— Ei hän tyydy kaikkeen, — hymähti Mitja. — Tässä on, veliseni,


semmoista, mitä ei yksikään nainen voi antaa anteeksi. Mutta
tiedätkö, mitä olisi paras tehdä?

— Mitä?

— Antaa hänelle takaisin kolmetuhatta.

— Mutta mistä ne otetaan? Kuule, minulla on kaksituhatta, Ivan


antaa lisäksi tuhat, siinä on kolmetuhatta, ota ja maksa pois.

— Mutta milloin ne saadaan, nuo sinun kolmetuhattasi? Sinä olet


vielä kaiken lisäksi alaikäinenkin, mutta on tarpeen välttämättömästi,
välttämättömästi, että sinä jo tänään käyt hänen luonaan, rahojen
kanssa tai ilman rahoja, sillä pitemmälle en voi asiaa venyttää, asia
on nyt sillä asteella. Huomenna on jo myöhäistä. Minä lähetän sinut
isän luo.

— Isän luo?

— Niin, isän luo, ennenkuin menet hänen luokseen. Pyydä isältä


kolmeatuhatta.

— Mutta eihän hän anna, Mitja.

— Kuinka hän antaisi, minä tiedän, että hän ei anna. Tiedätkö


sinä,
Aleksei, mitä on epätoivo?
— Tiedän.

— Kuule: juridisesti hän ei ole minulle mitään velkaa. Olen ottanut


häneltä kaikki, aivan kaikki, tiedän sen. Mutta moraalisesti hän on
minulle velkaa, eikö niin? Hänhän aloitti äidin
kahdeksallakolmattatuhannella ja ansaitsi satatuhatta. Antakoon hän
minulle vain kolmetuhatta kahdeksastakolmattatuhannesta, niin hän
pelastaa sieluni helvetistä ja se korvaa monet hänen syntinsä! Minä
puolestani, vakuutan sen sinulle juhlallisesti, lopetan kaiken noihin
kolmeentuhanteen, eikä hän sen koommin kuule minusta mitään.
Annan hänelle viimeisen kerran tilaisuuden olla isänä. Sano hänelle,
että Jumala itse on lähettänyt hänelle tämän tilaisuuden.

— Mitja, ei hän anna missään tapauksessa.

— Tiedän, että ei anna, tiedän täydelleen. Ja varsinkaan nyt.


Tiedänpä lisäksi vielä seuraavaakin: nyt näinä päivinä, kenties vasta
eilen, hän on saanut tietää vakavasti (alleviivaa sana vakavasti), että
Grušenjka todellakaan ehkä ei laske leikkiä, vaan tahtoo laittautua
naimisiin kanssani. Hän tuntee tuon luonteen, tuntee tuon kissan.
No, tokkohan hän kaiken lisäksi vielä antaa minulle rahaakin
edistääkseen tämmöistä asiaa, kun hän itse on aivan hullaantunut
Grušenjkaan? Mutta ei vielä siinä kyllin, voin mainita sinulle
enemmänkin: tiedän, että hänellä jo noin viisi päivää on ollut
esilleotettuna kolmetuhatta ruplaa, vaihdettuina sadan ruplan
seteleihin ja niputettuina isoon kääröön, viidellä sinetillä suljettuun,
joka on päältä sidottu ristiin punaisella nauhalla. Näetkö, miten
yksityiskohtaiset tiedot minulla on! Kääröön on kirjoitettu: »Enkelilleni
Grušenjkalle, jos hän tahtoo tulla», itse hän on sen kaikessa
hiljaisuudessa ja salaisuudessa raapustanut, eikä kukaan tiedä, että
hänen hallussaan on nämä rahat, paitsi lakeija Smerdjakov, jonka
rehellisyyteen hän luottaa niinkuin itseensä. Jo kolme tai neljä päivää
hän on nyt odottanut Grušenjkaa, toivoo hänen tulevan hakemaan
kääröä, niin on hän hänelle ilmoittanut, ja Grušenjka puolestaan on
lähettänyt sanan, että »ehkäpä tulenkin». Jos hän siis tulee ukon luo,
niin voinko silloin mennä hänen kanssaan naimisiin? Ymmärrätkö
nyt, miksi minä siis istun täällä salaa ja mitä minä nimenomaan
vahdin?

— Grušenjkaa?

— Häntä. Näitten homssujen, tämän talon emäntien asunnosta on


vuokrannut itselleen kopukan Foma. Foma on meidän
seuduiltamme, meidän entisiä sotamiehiämme. Hän on heidän
palveluksessaan, vartioi yöllä ja käy päivällä teiriä ampumassa, se
on hänen elinkeinonsa. Tänne hänen luokseen olen nyt asettunut.
Sekä hän että emännät tuntevat salaisuuden, nimittäin että minä
olen täällä vahdissa.

— Smerdjakov yksinkö tietää?

— Hän yksin. Hän myös minulle ilmoittaa, jos Grušenjka tulee


ukon luo.

— Hänkö sinulle kertoi kääröstä?

— Hän. Se on hyvin suuri salaisuus. Ei edes Ivan tiedä rahoista


eikä mistään. Mutta ukko lähettää Ivanin ajelemaan Tšermašnjaan
pariksi kolmeksi päiväksi: on ilmaantunut lehdon ostaja, tahtoisi
hakkauttaa sen maksamalla kahdeksantuhatta, niinpä ukko pyytelee
Ivania: »Autahan minua, käy itse katsomassa, kysymyksessä on pari
kolme päivää.» Hän tahtoo, että Grušenjka tulisi Ivanin
poissaollessa.
—- Hän siis odottaa tänäänkin Grušenjkaa.

— Ei, tänään Grušenjka ei tule, merkit viittaavat siihen suuntaan.


Ihan varmaan ei tule! — huudahti Mitja äkkiä. — Niin luulee
Smerdjakovkin. Isä juopottelee nyt, istuu pöydässä veli Ivanin
kanssa. Käy, Aleksei, pyytämässä häneltä nuo kolmetuhatta…

— Rakas Mitja, mikä sinun on! — huudahti Aljoša hypäten


paikaltaan ja katsellen tarkasti raivostunutta Dmitri Fjodorovitšia.
Hetken ajan hän luuli tämän menettäneen järkensä.

— Mitä sinä? En minä ole menettänyt järkeäni, — lausui Dmitri


Fjodorovitš katsellen häntä kiinteästi ja omituisen juhlallisesti. —
Älä pelkää, minä lähetän sinut isän luo ja tiedän mitä puhun: minä
uskon ihmeeseen.

— Ihmeeseen?

— Jumalan Kaitselmuksen ihmeeseen. Jumala tietää sydämeni.


Hän näkee kaiken epätoivoni. Hän näkee koko tämän kuvan. Salliiko
hän kauhean asian tapahtua? Aljoša, minä uskon ihmeeseen, mene!

— Minä menen. Sano, odotatko sinä täällä?

— 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?

— Grušenjka? Pidän silmällä, hyökkään esille ja estän…

— Entä jos…

— Jos niin, niin tapan. Näin en voi elää.

— Kenet sinä tapat?

— Ukon. Grušenjkaa en tapa.

— Veli, mitä sinä puhut!

— Enhän tiedä, en tiedä… Kenties en tapa, kenties tapan.


Pelkään, että hänen kasvonsa käyvät äkkiä minulle vastenmielisiksi
sillä hetkellä. Minä vihaan hänen aataminpalaansa, hänen
nenäänsä, hänen silmiään, hänen häpeämätöntä pilkkaansa.
Tunnen persoonallista inhoa. Juuri sitä pelkään. Saattaa käydä niin,
että en voi hillitä itseäni…

— Minä menen, Mitja. Minä uskon Jumalan järjestävän niinkuin


hän parhaaksi näkee, ettei tapahtuisi kauheata.

— Ja minä istun ja odotan ihmettä. Mutta jos ei tapahdu, niin…

Aljoša lähti mietteissään isän luo.

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!

Aljoša alkoi selitellä, että hän ei tahdo likööriä.

— Tuokoon joka tapauksessa, jos ei sinulle, niin meille, — puheli


Fjodor Pavlovitš säteilevänä. — Mutta maltahan, oletko syönyt
päivällisen vai etkö?

— 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.

— Armas! Kelpo poika! Hän juo kahvia. Eikö sitä pitäisi


kuumentaa? Eihän, sehän on kiehuvaa. Mainiota kahvia,
Smerdjakovin tekemää. Tuossa on kahvia ja tuossa on piiraita.
Smerdjakov on taiteilija ja varsinkin kalakeiton laittamisessa, se on
totinen tosi. Tulehan joskus syömään kalakeittoa, ilmoita
edeltäpäin… Mutta kuulehan, kuulehan, minähän käskin äsken sinua
muuttamaan tänne jo tänään patjoinesi ja tyynyinesi? Toitko patjan
tullessasi? Hehehe!…

— En, en tuonut, — hymähti Aljošakin.

— Ahaa, pelästyit, pelästyitpä äsken, pelästyit? Voi sinua,


kyyhkyseni, minäkö voisin pahoittaa mieltäsi! Kuule, Ivan, minä en
voi nähdä hänen katsovan tuolla tavoin silmiin ja nauravan, en voi.
Kaikki sisälmykseni alkavat nauraa hänelle, minä rakastan häntä!
Aljoška, annahan kun siunaan sinua isän siunauksella.

Aljoša nousi seisomaan, mutta Fjodor Pavlovitš oli jo ennättänyt


muuttaa mieltään.

— Ei, ei, nyt teen ylitsesi ainoastaan ristinmerkin, kas näin,


istuudu. No, nyt saat hauskaa ja nimenomaan omasta aiheestasi.
Saat nauraa tarpeeksesi. Meillä on Bileamin aasintamma ruvennut
puhumaan, ja sepä vasta puhuu, sepä vasta puhuu!

Selville kävi, että Bileamin aasintamma oli palvelija Smerdjakov.


Hän oli vielä nuori mies, vain kahdenkymmenenneljän vuoden
ikäinen, mutta hirveän ihmisarka ja vaitelias. Ei niin, että hän olisi
ollut ujo tai hävennyt jotakin, ei, hän oli luonteeltaan päinvastoin
ylpeä ja tavallaan halveksi kaikkia. Mutta eipä voi olla mainitsematta
hänestä vaikkapa lyhyesti, ja se on tehtävä juuri nyt. Marfa
Ignatjevna ja Grigori Vasiljevitš kasvattivat häntä, mutta poika kasvoi
»ilman mitään kiitollisuutta», kuten Grigori hänestä lausui, ujoksi
pojaksi ja katsellen maailmaa nurkasta. Lapsena hän hyvin
mielellään hirtti kissoja ja sitten hautasi ne juhlamenoin. Hän pani
silloin ylleen lakanan, joka oli olevinaan jonkinmoinen papin viitta,
lauloi ja oli suitsuttavinaan pyhää savua. Tämän hän teki aina
kaikessa hiljaisuudessa, mitä suurimmassa salaisuudessa. Grigori
sai hänet kerran kiinni tämmöisestä hommasta ja antoi hänelle
vitsaa, niin että se teki kipeätä. Poika meni nurkkaan ja katseli sieltä
kulmiensa alta viikon verran. — Hän ei rakasta meitä, tuo peto, —
sanoi Grigori Marfa Ignatjevnalle, — eikä hän rakasta ketään. Oletko
sinä mikään ihminen, — kääntyi hän äkkiä suoraan Smerdjakovin
puoleen, — sinä et ole ihminen, sinä olet syntynyt saunan
märkyydestä, semmoinen sinä olet… — Smerdjakov ei, kuten
myöhemmin kävi selville, voinut koskaan antaa hänelle anteeksi
näitä sanoja. Grigori opetti hänet lukemaan ja hänen tultuaan
kahdentoista vuoden ikään alkoi opettaa hänelle raamatun historiaa.
Mutta asia päättyi kohta viemättä tuloksiin. Kerran, jo toisella tai
kolmannella oppitunnilla, poika äkkiä naurahti.

— Mitä sinä? — kysyi Grigori katsoen häneen ankarasti


silmälasiensa alta.

— En mitään. Maailman Herra Jumala loi ensimmäisenä päivänä,


mutta auringon, kuun ja tähdet neljäntenä päivänä. Mistä valo sitten
loisti ensimmäisenä päivänä?

Grigori hämmästyi. Poika katseli ivallisesti opettajaansa. Hänen


katseessaan oli jotakin ylimielistäkin. Grigori ei jaksanut hillitä
itseään. — Täältä se loisti! — huudahti hän ja löi vimmoissaan
oppilasta korvalle. Poika kesti korvapuustin sanaakaan sanomatta,
mutta painautui taas nurkkaan muutamaksi päiväksi. Sattui niin, että
viikon kuluttua hänessä ilmeni kaatuvatauti ensimmäisen kerran,
eikä se sittemmin jättänyt häntä koko elämän aikana. Tästä
kuultuaan Fjodor Pavlovitš näytti ikäänkuin muuttaneen mieltä
poikaan nähden. Ennen hän oli katsellut poikaa jokseenkin
välinpitämättömästi, vaikkakaan ei koskaan torunut häntä, ja
kohdatessaan hän oli aina antanut pojalle kopeekan. Hellällä päällä
ollessaan hän oli toisinaan lähettänyt pojalle pöydästään jotakin
makeata. Mutta nyt, saatuaan tiedon taudista, hän alkoi todenteolla
huolehtia pojasta, kutsutti lääkärin ja hankki hoitoa, mutta tauti
osoittautui mahdottomaksi parantaa. Kohtauksia sattui keskimäärin
kerran kuussa ja eri aikoina. Ne olivat myös erivoimaisia, — toiset
lieviä, toiset erittäin ankaria. Fjodor Pavlovitš kielsi hyvin ankarasti
Grigoria antamasta pojalle ruumiillista rangaistusta ja alkoi päästää
häntä luokseen ylös. Niinikään hän kielsi toistaiseksi antamasta
pojalle minkäänlaista opetusta. Mutta kerran, kun poika jo oli
viidentoista vuoden ikäinen, Fjodor Pavlovitš huomasi hänen
liikuskelevan kirjakaapin luona ja lukevan lasin läpi kirjojen nimiä.
Fjodor Pavlovitšilla oli koko joukko kirjoja, toista sataa nidettä, mutta
häntä itseään ei kukaan ollut nähnyt kirjan ääressä. Hän antoi heti
kirjakaapin avaimen Smerdjakoville: — No, lue sitten, saat ruveta
kirjaston hoitajaksi. Sen sijaan, että vetelehdit pihalla, istu ja lue. Lue
tämä, — ja Fjodor Pavlovitš antoi hänelle kaapista Illat maatalossa
Dikanjkan luona.

Poika luki kirjan, mutta oli tyytymätön, ei naurahtanut kertaakaan,


vaan päinvastoin loppuun päästyään rypisti kulmiaan.

— Mitä? Eikö se naurata? — kysyi Fjodor Pavlovitš.

Smerdjakov oli vaiti.

— Vastaa, hölmö.

— Se on kaikki valetta, — mutisi Smerdjakov hymyillen.

— Mene hiiteen, sinä olet alhainen sielu. Seis, tässä on


Smaragdovin Yleinen historia, siinä on kaikki totta, lue se.

Mutta Smerdjakov ei lukenut Smaragdovia kymmentä sivuakaan,


sillä se tuntui ikävältä. Niinpä kirjakaappi taas sulkeutui. Pian Marfa
ja Grigori ilmoittivat Fjodor Pavlovitšille, että Smerdjakoviin oli
vähitellen ilmaantunut jonkinmoinen kauhea nirsous: hän istuu
liemiastian ääressä, ottaa lusikan ja etsii etsimistään jotakin
liemestä, kumartuu, tirkistelee, ottaa lusikkaan ja nostaa valoa
vastaan.
Torakkako siellä on vai mikä? — kysyy Grigori.

— Kenties kärpänen, — huomauttaa Marfa.

Puhtautta rakastava nuorukainen ei koskaan vastannut, mutta


leivälle ja lihalle ja kaikille ruoille hän teki samoin: nosti palasen
haarukalla valoa kohti, tarkasteli kuin mikroskoopilla, tavallisesti
pitkän ajan, vihdoin viimein katsoi voivansa pistää suuhunsa. »Kas
vain, millainen herra tänne on ilmestynyt», mutisi Grigori häntä
katsellessaan. Kuultuaan Smerdjakovin uudesta ominaisuudesta
Fjodor Pavlovitš päätti heti, että miehestä on tehtävä kokki, ja lähetti
hänet oppiin Moskovaan. Opissa hän oli muutamia vuosia ja
palatessaan hän oli suuresti muuttunut kasvoiltaan. Hän oli äkkiä
luonnottomasti vanhentunut, niin että se ei ollenkaan ollut
sopusoinnussa hänen ikänsä kanssa, oli tullut ryppyiseksi,
keltaiseksi, kuohilaan näköiseksi. Moraalisesti hän oli palatessaan
melkein samanlainen kuin ennen Moskovaan lähtöään: hän oli
edelleen yhtä ihmisarka eikä tuntenut pienintäkään tarvetta etsiä
kenenkään seuraa. Hän oli Moskovassakin, kuten myöhemmin
kerrottiin, aina ollut vaiti. Itse Moskova näytti perin vähässä
määrässä kiinnostaneen häntä, niin että hän tunsi siitä vain joitakin
asioita, mutta mihinkään muuhun hän ei ollut edes kiinnittänyt
huomiota. Kerran hän oli ollut teatterissakin, mutta palannut sieltä
äänettömänä ja tyytymättömänä. Sen sijaan hän oli Moskovasta
luoksemme palatessaan hyvin puettu, hänellä oli puhdas takki ja
puhtaat liinavaatteet, hän puhdisti itse harjalla hyvin huolellisesti
pukunsa säännöllisesti kaksi kertaa päivässä ja hän puhdisti
vasikannahkaisia, komeita saappaitaan hyvin mielellään
erikoislaatuisella englantilaisella vahalla, niin että ne kiilsivät kuin
peili. Hän osoittautui erinomaisen hyväksi kokiksi. Fjodor Pavlovitš
määräsi hänelle palkan, ja tämän palkkansa Smerdjakov käytti miltei
kokonaan vaatteisiin, hiusvoiteeseen, hajuvesiin j.n.e. Mutta
naisväkeä hän näytti halveksivan aivan yhtä paljon kuin miehiäkin,
esiintyi naisten seurassa arvokkaasti, miltei luoksepääsemättömänä.
Fjodor Pavlovitš alkoi katsella häntä eräältä toiseltakin näkökannalta.
Seikka oli semmoinen, että Smerdjakovin kaatuvataudin kohtaukset
olivat tulleet ankarammiksi, ja semmoisina päivinä valmisti ruoan
Marfa Ignatjevna, mikä ei ollenkaan ollut mieluisaa Fjodor
Pavlovitšista.

— Miksi sinulla on kohtauksia useammin? — sanoi hän toisinaan


nyrpeästi uudelle kokilleen katsoen häntä kasvoihin. — Jospa
menisit naimisiin jonkun kanssa, tahdotko, että naitan?…

Nämä puheet saivat Smerdjakovin vain kalpenemaan harmista,


mutta hän ei vastannut mitään. Fjodor Pavlovitš viittasi kädellään ja
meni tiehensä. Pääasia oli, että hän oli kerta kaikkiaan vakuutettu
Smerdjakovin rehellisyydestä, siitä, että tämä ei mitään ota eikä
varasta. Tapahtui kerran, että Fjodor Pavlovitš juovuksissa pudotti
omalle pihalleen lokaan kolme sadan ruplan seteliä, jotka hän juuri
oli saanut, ja kaipasi niitä vasta seuraavana päivänä. Juuri kun hän
oli alkanut etsiä niitä taskuistaan, hän huomasikin, että kaikki kolme
seteliä olivat pöydällä. Mistä? Smerdjakov oli löytänyt ne maasta ja
tuonut jo eilen. — No, veikkoseni, tuommoisia kuin sinä en ole
nähnytkään, — lausahti silloin Fjodor Pavlovitš ja lahjoitti hänelle
kymmenen ruplaa. On lisättävä, ettei hän vain ollut vakuutettu
Smerdjakovin rehellisyydestä, vaan jostakin syystä myös piti
hänestä, vaikka nuorukainen katseli häntäkin kulmiensa alta niinkuin
muitakin eikä koskaan puhunut. Vain harvoin hän sanoi jotakin. Jos
tuohon aikaan jonkun päähän olisi pistänyt häntä katsellessaan
kysyä: mikä herättää tuossa nuoressa miehessä mielenkiintoa ja
mitä hänellä on useimmin mielessä, niin todellakin olisi ollut
mahdotonta saada siitä selvää katsomalla häntä. Ja kuitenkin hän
toisinaan kotona tai myös pihalla ja kadulla pysähtyi, vaipui
mietteisiinsä ja seisoi näin kymmenkunta minuuttiakin. Fysionomisti
olisi häntä katsellessaan sanonut, että tässä ei ole mitään mietteitä
eikä ajatuksia, vaan jonkinmoista itseensä vaipumista. Maalari
Kramskoi on maalannut huomattavan taulun, jonka nimi on Itseensä
vaipunut: siinä on kuvattu talvinen metsä, ja metsässä seisoo tiellä
risaisessa nutussa ja tallukoissa aivan yksinään kokonaan erillään
muusta maailmasta syrjään poikennut talonpoika. Seisoo ja on
ikäänkuin mietteissään, mutta ei ajattele, vaan on »vaipunut
itseensä». Jos häntä nykäisisi, niin hän hätkähtäisi ja katsoisi aivan
kuin heräten, mutta mitään ymmärtämättä. Tosin hän kohta tajuaisi
kaiken, mutta jos häneltä kysyttäisiin, mitä hän siinä seisoessaan
ajatteli, niin hän varmaankaan ei muistaisi mitään, mutta sen sijaan
hän varmaankin kätkisi mieleensä sen vaikutelman, jonka alaisena
hän oli ollessaan itseensä syventyneenä. Nämä vaikutelmat ovat
hänelle kalliita, ja varmaankin hän niitä kokoaa, huomaamattaan ja
tiedottomasti, — mitä varten ja miksi, sitä hän ei tietysti itsekään
tiedä: kenties hän äkkiä, koottuaan monien vuosien vaikutelmat,
jättää kaikki ja menee pyhiinvaellukselle Jerusalemiin etsimään
sielulleen pelastusta, tai kenties yht'äkkiä polttaa poroksi kotikylänsä,
saattaapa samalla kertaa tehdä kumpiakin. Itseensä-vaipujia on
kansassa melkoinen määrä. Yksi noita itseensä-vaipujia on
luultavasti myös Smerdjakov, ja luultavasti hänkin kokosi
vaikutelmiaan ahneesti tietämättä juuri itsekään, miksi teki niin.

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.

— Mitäs sinä? — kysyi Fjodor Pavlovitš, joka oli heti huomannut


naurahduksen ja ymmärtänyt, että se tietysti tarkoitti Grigoria.

— Sitähän minä, — alkoi äkkiä Smerdjakov aivan odottamatta


puhua kovalla äänellä, — että vaikka tuon kiitosta ansaitsevan
sotamiehen sankarityö olikin sangen suuri, niin ei kuitenkaan minun
mielestäni olisi siinä ollut mitään syntiä, jos hän tässä tapauksessa
olisikin kieltänyt esimerkiksi Kristuksen nimen ja luopunut
kasteestaan pelastaakseen sillä tavoin henkensä hyviä töitä varten,
joilla hän vuosien kuluessa sitten olisi sovittanut pelkuruutensa.

— Kuinka ei olisi ollut synti? Puhut joutavia, sentähden joudutkin


suoraan helvettiin ja siellä sinua käristetään kuin lampaanlihaa, —
tokaisi Fjodor Pavlovitš.

Juuri silloin Aljoša astui sisälle. Fjodor Pavlovitš, kuten olemme


nähneet, ilostui suuresti Aljošan tulosta.

— Sinun aiheestasi, sinun aiheestasi! — nauraa hihitti hän iloisesti


käskiessään Aljošaa istumaan ja kuuntelemaan.

— Mitä sanoitte lampaanlihasta, niin se ei ole sillä tavoin eikä


siellä siitä mitään tule eikä pidäkään tulla sellaista oikeuden ja
kohtuuden mukaan, — huomautti Smerdjakov lujasti.

— Kuinka niin oikeuden ja kohtuuden mukaan, — huudahti Fjodor


Pavlovitš vielä iloisemmin ja töytäisi Aljošaa polvellaan.

— Konna hän on, se hän on! — lausui äkkiä Grigori. Hän katsoi
vihaisesti Smerdjakovia suoraan silmiin.

— Mitä konnaan tulee, niin malttakaahan mielenne, Grigori


Vasiljevitš, — lausui Smerdjakov tyynesti ja hillitysti. — Ajatelkaa
mieluummin itse, että jos minä olen joutunut kristittyjen ihmisten
kiduttajain vangiksi ja he vaativat minua kiroamaan Jumalan nimen
sekä luopumaan pyhästä kasteesta, niin minulle antaa siihen täyden
valtuuden oma järkeni, sillä mitään syntiä siitä ei tule.
— Johan sinä olet sen sanonut, älä kuvaile, vaan todista! — huusi
Fjodor Pavlovitš.

— Senkin liemenkeittäjä! — kuiskasi Grigori halveksivasti.

— Mitä tulee liemenkeittäjään, niin malttakaa siihenkin nähden


mielenne, Grigori Vasiljevitš, älkääkä haukkuko, vaan ajatelkaa itse.
Sillä heti kun sanon kiduttajille: »Ei, minä en ole kristitty ja minä
kiroan totisen Jumalan», niin samassa minusta Jumalan korkeimman
tuomion kautta tulee heti ja erityisesti anateema kirottu ja pyhästä
kirkosta erotettu, aivan kuin muukalaispakana, vieläpä niin, että
samalla hetkellä kuin sen lausun, tai oikeastaan kun vain aionkin sen
lausua, joten siinä ei kulu neljännessekuntiakaan, niin olen jo
erotettu, onko niin vai eikö, Grigori Vasiljevitš?

Hän kääntyi ilmeisellä mielihyvällä Grigorin puoleen, vaikka itse


asiassa vastasi vain Fjodor Pavlovitšin kysymyksiin ja varsin hyvin
ymmärsi sen, puhuen tahallaan niinkuin nämä kysymykset olisi
hänelle tehnyt Grigori.

— Ivan! — huudahti äkkiä Fjodor Pavlovitš. — Taivuta pääsi aivan


korvani luo. Hän on kaiken tämän keksinyt sinun takiasi, tahtoo, että
sinä häntä kehuisit. Kehu!

Ivan Fjodorovitš kuunteli aivan vakavana isänsä innostunutta


puhetta.

— Seis, Smerdjakov, ole vaiti vähän aikaa, — huudahti taas


Fjodor
Pavlovitš. — Ivan, kallista taas pääsi aivan lähelleni.
Ivan Fjodorovitš taivutti uudelleen päätään hyvin totisen
näköisenä.

— Minä rakastan sinua samoin kuin Aljoškaakin. Älä luule, että en


rakasta sinua. Saako olla konjakkia?

— Antakaa. »Näytpä itse jo olevan aikalailla pätkässä», — ajatteli


Ivan Fjodorovitš katsellen tarkasti isäänsä. Smerdjakovia taas hän
tarkasteli erittäin uteliaana.

— Sinä olet jo nytkin anateema kirottu, — puhkesi Grigori äkkiä


puhumaan, — ja kuinka sinä, konna, sen jälkeen uskallat arvostella,
jos…

— Älä toru, Grigori, älä toru! — keskeytti Fjodor Pavlovitš.

— Odottakaa te, Grigori Vasiljevitš, vaikkapa vain hyvin vähän


aikaa, ja kuunnelkaa jatkoa, sillä minä en ole vielä puhunut kaikkea
loppuun. Samassa kuin Jumala on minut viipymättä kironnut, sinä
samana korkeana hetkenä minusta on näet tullut aivan kuin
muukalaispakana, ja kasteeni on minusta otettu pois ja muuttunut
mitättömäksi, — onko edes tämä oikein puhuttu?

— Puhu pian loppuun, veliseni, puhu loppuun, — hoputti Fjodor


Pavlovitš ottaen mielihyvällä kulauksen ryyppylasistaan.

— Jos kerran en enää ole kristitty, niin siis en valehdellut


kiduttajille, kun he kysyivät: »Olenko kristitty vai enkö», sillä itse
Jumala on poistanut minusta kristillisyyden jo pelkän aikeeni
perusteella ja ennenkuin olen ennättänyt lausua sanaakaan
kiduttajille. Mutta jos minut jo on joukosta erotettu, niin kuinka ja millä
oikeudella minulta kysytään toisessa maailmassa aivan kuin
kristityltä, miksi olen luopunut Kristuksesta, kun jo pelkän
aikomuksen tähden, ennenkuin mitään luopumista olikaan, minut
erotettiin kasteestani? Jos kerran en ole kristitty, niin en myöskään
voi luopua Kristuksesta, sillä eihän minulla silloin ole, mistä
luopuisin. Kuka vaatii edesvastuuseen pakanan tataarilaisen, Grigori
Vasiljevitš, vaikkapa taivaassakin, siitä, että hän ei syntynyt
kristityksi, ja kuka häntä siitä rankaisee, jos nimittäin ajatellaan, että
yhdestä härästä ei nyljetä kahta nahkaa. Ja jos itse Jumala
Kaikkivaltias vaatii tataarilaisen tilille tämän kuoltua, niin luullakseni
hän määrää jonkin kaikkein pienimmistä rangaistuksista (sillä eihän
häntä voi jättää kokonaan rankaisematta) ottaen huomioon, että
eihän ole hänen syynsä, jos hän on tullut pakanallisista
vanhemmista pakanana maailmaan. Eihän Herra Jumala voi ottaa
väkisin tataarilaista ja sanoa hänestä, että hänkin oli kristitty? Sehän
merkitsisi, että Jumala Kaikkivaltias puhuisi silkkaa valhetta. Mutta
voiko Jumala, taivaan ja maan valtias, lausua valheen, vaikkapa vain
yhdessä sanassa?

Grigori oli kuin jähmettynyt ja katseli puhujaa silmät selällään.


Vaikka hän ei hyvin ymmärtänytkään mitä puhuttiin, niin jotakin tästä
sekasotkusta hän yht'äkkiä kuitenkin käsitti ja jäi seisomaan sen
näköisenä kuin mies, joka äkkiarvaamatta on lyönyt otsansa
seinään. Fjodor Pavlovitš joi lasinsa pohjaan ja alkoi nauraa
vinkuvaa nauruaan.

— Aljoška, Aljoška, mitäs arvelet! Voi sinua, senkin kasuisti! Hän


on ollut jesuiittain opissa jossakin, Ivan. Voi sinua, löyhkäävä
jesuiitta, kuka sinua on opettanut? Mutta sinä puhut roskaa, kasuisti,
roskaa, paljasta roskaa. Älä itke, Grigori, tuossa paikassa me
iskemme hänet tomuksi ja tuhaksi. Sanopa minulle tämä, sinä
aasintamma: olkoonpa, että olet kiduttajien edessä oikeassa, mutta
itsesi edessähän sinä kuitenkin olet luopunut uskostasi ja sanot
itsekin, että olit sillä hetkellä anateema kirottu, mutta jos kerran olet
anateema, niin eipä tämän anateeman vuoksi silitetä päätäsi
helvetissä. Mitä sinä tästä ajattelet, oiva jesuiittani?

— Ei ole epäilemistäkään, etten olisi itseni edessä luopunut, mutta


ei siinä kuitenkaan ollut mitään erikoissyntiä, vaan jos minkä verran
syntiä oli, niin se oli kaikkein tavallisinta laatua!

— Millä tavoin kaikkein tavallisinta laatua!

— Valehtelet, kirottu lurjus, — sähisi Grigori.

— Ajatelkaahan itse, Grigori Vasiljevitš, — jatkoi Smerdjakov


tasaisesti ja arvokkaasti, tuntien olevansa voiton puolella, mutta
ikäänkuin osoittaen jalomielisyyttä nujerrettua vastustajaa kohtaan,
— ajatelkaahan itse, Grigori Vasiljevitš: onhan Sanassa sanottu, että
jos teillä on uskoa vaikkapa kaikkein pienimmän siemenen verran ja
te sanotte tälle vuorelle, että se siirtyisi mereen, niin se siirtyy
vähääkään viipymättä heti käskettyänne. Mitäs, Grigori Vasiljevitš,
jos minä olen epäuskoinen, mutta te olette niin uskovainen, että
lakkaamatta torutte minuakin, niin koettakaahan itse sanoa tälle
vuorelle, että se siirtyisi ei mereen (sillä mereen on täältä pitkä
matka), vaan vaikkapa haisevaan jokeemme, joka virtaa tuossa
puutarhamme takana, niin saattepa nähdä itse sillä samalla hetkellä,
että mikään ei siirrykään, vaan kaikki jää entiseen kuntoon ja
eheäksi, huutakaapa miten paljon tahansa. Mutta tämä merkitsee
sitä, että tekään ette usko, Grigori Vasiljevitš, oikealla tavalla, vaan
haukutte ainoastaan muita siitä kaikin tavoin. Jos taasen otamme
lukuun sen, ettei kukaan meidän aikanamme, ette vain te, vaan ei
kerrassaan kukaan, alkaen kaikkein korkeimmista henkilöistä aina
viimeiseen talonpoikaan asti, voi siirtää vuoria mereen, paitsi ehkä
yksi ainoa mies koko maan päällä tai enintään kaksi, ja nämäkin
kenties ovat jossakin Egyptin erämaassa kaikessa salaisuudessa
sieluansa pelastamassa, niin että heitä ei ollenkaan voi löytää, — jos
on näin, jos kaikki muut siis ovat epäuskoisia, niin tokkohan Jumala
kiroaa kaikki nuo muut, toisin sanoen koko maan asujaimiston,
lukuunottamatta noita paria erakkoa, eikä armossansa, joka on
kaikille tunnettu, anna kenellekään heistä anteeksi? Senpä vuoksi
minä luotankin siihen, että jos kerran olen epäillyt, niin minulle
annetaan anteeksi, kun vuodatan katumuksen kyyneliä.

— Seis! — vikisi Fjodor Pavlovitš, ja hänen ihastuksensa kohosi


huippuunsa. — Sinä siis kuitenkin otaksut, että on olemassa pari
sellaista, jotka voivat siirtää vuoria? Ivan, vedä risti seinään, kirjoita
muistiin: tuossa ilmeni venäläinen ihminen kokonaisuudessaan!

— Huomautitte aivan oikein, että tämä on kansallinen piirre


uskossa, — myönsi Ivan Fjodorovitš hymyillen hyväksyvästi.

— Sinä myönnät! Siis se on niin, kun kerran sinäkin olet samaa


mieltä!
Aljoša, onhan se totta? Sehän on täysin venäläistä uskoa?

— Ei, Smerdjakovin usko ei ole ollenkaan venäläistä, — lausui


Aljoša vakavasti ja lujasti.

— En minä puhu hänen uskostaan, vaan tuosta piirteestä, noista


kahdesta erakosta, vain tuosta yhdestä piirteestä: sehän on
venäläistä, venäläistä?

— Niin, se piirre on aivan venäläinen, — hymähti Aljoša.


Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like