Programming, Problem Solving & Abstraction With C (PDFDrive)
Programming, Problem Solving & Abstraction With C (PDFDrive)
Preface vii
3 Making Choices 29
3.1 Logical expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.3 Pitfalls to watch for . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.4 Case study: Calculating taxes . . . . . . . . . . . . . . . . . . . . . 36
3.5 The switch statement . . . . . . . . . . . . . . . . . . . . . . . . . 38
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4 Loops 45
4.1 Controlled iteration . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.2 Case study: Calculating compound interest . . . . . . . . . . . . . 49
4.3 Program layout and style . . . . . . . . . . . . . . . . . . . . . . . 50
4.4 Uncontrolled iteration . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.5 Iterating over the input data . . . . . . . . . . . . . . . . . . . . . . 56
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
iii
PAGE IV P ROGRAMMING , P ROBLEM S OLVING , AND A BSTRACTION
7 Arrays 99
7.1 Linear collections of like objects . . . . . . . . . . . . . . . . . . . 99
7.2 Reading into an array . . . . . . . . . . . . . . . . . . . . . . . . . 101
7.3 Sorting an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
7.4 Arrays and functions . . . . . . . . . . . . . . . . . . . . . . . . . 105
7.5 Two-dimensional arrays . . . . . . . . . . . . . . . . . . . . . . . . 109
7.6 Array initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
7.7 Arrays and pointers . . . . . . . . . . . . . . . . . . . . . . . . . . 113
7.8 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
7.9 Case study: Distinct words . . . . . . . . . . . . . . . . . . . . . . 121
7.10 Arrays of strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.11 Program arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
8 Structures 129
8.1 Declaring structures . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8.2 Operations on structures . . . . . . . . . . . . . . . . . . . . . . . 131
8.3 Structures, pointers, and functions . . . . . . . . . . . . . . . . . . 135
8.4 Structures and arrays . . . . . . . . . . . . . . . . . . . . . . . . . 137
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
PAGE V
12 Algorithms 203
12.1 Measuring performance . . . . . . . . . . . . . . . . . . . . . . . . 203
12.2 Dictionaries and searching . . . . . . . . . . . . . . . . . . . . . . 205
12.3 Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
12.4 Quick sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
12.5 Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
12.6 Heap sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
12.7 Other problems and algorithms . . . . . . . . . . . . . . . . . . . . 225
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Index 241
PAGE VI P ROGRAMMING , P ROBLEM S OLVING , AND A BSTRACTION
Preface to the Revised Edition
When I commenced this project in 2002, I was motivated by twenty years of teaching
programming to first-year university students, watching their reactions to and behav-
ior with a range of texts and programming languages. My observations at that time
led to the following specification:
• Value for money: Students are (quite rightly) sceptical of $100 books, and will
often commence the semester without owning it. Then, if they do buy one, they
sell it again at the end of the semester, in order to recoup their money. I sought
to write a book that students would not question as a purchase, nor consider
for later sale. With the cooperation of the publishers, and use of the “Pearson
Original” format, this aim has also been met.
• Readability: More than anything else, I wanted to write a book that students
would willingly read, and with which they would engage as active learners.
The prose is intended to be informative rather than turgid, and the key points
in each section have been highlighted, to allow students to quickly remind
themselves of important concepts.
encouraged to actively question the facts they are being told, to better cement
their own understanding.
• Excitement: Last, but certainly not least, I wanted a book that would enthuse
students, and let them see some of the excitement in computing. Few programs
can match the elegance of quick sort, for example.
Those thoughts led to the first edition, finalized in late 2002, and published in early
2003. Now it is nearly 2013, and another decade has gone by. I have used this book
every year since then in my classes, and slowly built up a list of “I wish I hadn’t done
it that way” issues. Those “I wishes” are all addressed in this revised edition. Most of
the changes are modest, and I think I have remained true to the original goals. (One
of the more interesting changes is that I have removed all mention of floppy disks!)
The idea is to show the students not just the end result of a programming exercise
as an abstract object, but to also show them, with a running commentary, how that
result is achieved. That the presentation includes the occasional dead end, judicious
backtracking and redesign, and sometimes quite puzzling behavior, is all grist for
the mill. The students engage and, for example, have at times chorused out loud at
my (sometimes deliberate, sometimes inadvertent) introduction of errors. The web
also helps with this style of teaching – the programs that are generated in class can
be made accessible in their final form, so students are free to participate, rather than
frantically copy.
Running a lecture in this way requires a non-trivial amount of confidence, both
to be able to get it right, and to deal with the consequences of sometimes getting it
wrong. More than once I have admitted that I need to go and read a manual before
coming back to them in the next class with an explanation of some obscure behav-
ior that we have uncovered. But the benefits of taking these risks are considerable:
“what will happen if. . . ” is a recurring theme in my lectures, and whether it is a
rhetorical question from me, or an actual interjection from a student, we always go to
the computer and find out.
Supervised laboratory classes should accompany the lecture presentations. Stu-
dents learn the most when trying it for themselves, but need to be able to ask questions
while they do. Having students work on programming projects is also helpful. The
exercises at the end of each chapter include broader non-programming questions, for
use in discussion-based tutorial classes.
Acknowledgements
I learned programming in the first half of the 1970s as a secondary school student in
Wellington, New Zealand, and will always be grateful to my two maths teachers, Bob
Garden and Ron Ritz, for their vision and enthusiasm. Our programming involved
a dialect of Fortran, and was carried out with a bent paper clip; special preprinted
cards that you popped chads out of; and a bike ride to the local bank branch to drop
the completed programs into a courier bag for transmission to their “Electronic Data
Processing Center”. Each compile/execute cycle took about three days, so we quickly
learned to be accurate.
My interest in computing was deepened during my University study, and I thank
PAGE X P ROGRAMMING , P ROBLEM S OLVING , AND A BSTRACTION
all of the Computer Science staff that worked at the University of Canterbury in New
Zealand during the period 1977–1979. Worth special mention is Tadao Takaoka: in
his own inimitable way, it was he who interested me in algorithms, and who served
as a role model for the academic life that I have pursued for thirty years.
Since then, it has primarily been academic colleagues at the University of Can-
terbury and at the University of Melbourne that have influenced me, by sharing their
knowledge and skills. I first taught introductory programming in 1982, and have done
so every year since then. The people that I have worked with on those subjects, or on
other academic projects, have all left a mark on this book. In roughly chronological
order, they include: Rod Harries, Robert Biddle, Tim C. Bell, Ian Witten, Ed Morris,
Rodney Topor, Justin Zobel, Liz Sonenberg, Lee Naish, Harald Søndergaard, Roy
Johnston, Peter Stuckey, Tim A.H. Bell, Bernie Pope, Peter Hawkins, Martin Sulz-
mann, Owen de Kretser, Michael Kirley, Lars Kulik, and Alan Blair. Many students
have pointed out errors or assisted in various ways, and will continue to do so into
the future; I thank them all.
Finally, there is family, and I gratefully acknowledge the long-ago input of my
parents, Duncan and Hilda Moffat; and the more recent encouragement supplied by
my wife Thau Mee, and our own children, Anne and Kate.
Alistair Moffat,
Melbourne, Australia
http://www.csse.unimelb.edu.au/˜alistair
November 28, 2012