Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

An Introduction to Programming

through C++ Abhiram G. Ranade


Visit to download the full and correct content document:
https://ebookmass.com/product/an-introduction-to-programming-through-c-abhiram-g-
ranade/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

An Introduction to Parallel Programming 2. Edition


Pacheco

https://ebookmass.com/product/an-introduction-to-parallel-
programming-2-edition-pacheco/

Introduction to Java Through Game Development: Learn


Java Programming Skills by Working with Video Games 1st
Edition Victor G. Brusca

https://ebookmass.com/product/introduction-to-java-through-game-
development-learn-java-programming-skills-by-working-with-video-
games-1st-edition-victor-g-brusca/

An Introduction to Parallel Programming. Second Edition


Peter S. Pacheco

https://ebookmass.com/product/an-introduction-to-parallel-
programming-second-edition-peter-s-pacheco/

Gateways to Democracy: An Introduction to American


Government John G. Geer

https://ebookmass.com/product/gateways-to-democracy-an-
introduction-to-american-government-john-g-geer/
Modern C++ for Absolute Beginners: A Friendly
Introduction to the C++ Programming Language and C++11
to C++23 Standards, 2nd Edition Slobodan Dmitrovi■

https://ebookmass.com/product/modern-c-for-absolute-beginners-a-
friendly-introduction-to-the-c-programming-language-
and-c11-to-c23-standards-2nd-edition-slobodan-dmitrovic/

Modern C for Absolute Beginners: A Friendly


Introduction to the C Programming Language 2nd Edition
Slobodan Dmitrovi■

https://ebookmass.com/product/modern-c-for-absolute-beginners-a-
friendly-introduction-to-the-c-programming-language-2nd-edition-
slobodan-dmitrovic/

C Programming Language: C PROGRAMMING LANG _p2 2nd


Edition, (Ebook PDF)

https://ebookmass.com/product/c-programming-language-c-
programming-lang-_p2-2nd-edition-ebook-pdf/

Introduction to Java Programming, Comprehensive Version


Y. Daniel Liang

https://ebookmass.com/product/introduction-to-java-programming-
comprehensive-version-y-daniel-liang/

Through the Lens of Anthropology: An Introduction to


Human Evolution and Culture 1st Edition, (Ebook PDF)

https://ebookmass.com/product/through-the-lens-of-anthropology-
an-introduction-to-human-evolution-and-culture-1st-edition-ebook-
pdf/
An Introdu tion to Programming through C++
Abhiram Ranade

Do not distribute

Abhiram Ranade, 2013


Contents

Prefa e 14
0.1 Graphi s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
0.2 First day/ rst month blues . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
0.3 Obje t oriented programming . . . . . . . . . . . . . . . . . . . . . . . . . . 17
0.4 Fitting the book into a urri ulum . . . . . . . . . . . . . . . . . . . . . . . 17
1 Introdu tion 18
1.1 A simple program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.1.1 Exe uting the program . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.2 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.2.1 Exe ution order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.3 Repeating a blo k of ommands . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.3.1 Drawing any regular polygon . . . . . . . . . . . . . . . . . . . . . . 22
1.3.2 Repeat within a repeat . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.4 Some useful turtle ommands . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.5 Numeri al fun tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.6 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.7 Computation without graphi s . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.8 Con luding Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.8.1 Graphi s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.8.2 A note regarding the exer ises . . . . . . . . . . . . . . . . . . . . . . 29
1.9 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2 A bird's eye view 31
2.1 Problem solving using omputers . . . . . . . . . . . . . . . . . . . . . . . . 31
2.1.1 Algorithms and programs . . . . . . . . . . . . . . . . . . . . . . . . 33
2.2 Basi prin iples of digital ir uits . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3 Number representation formats . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.3.1 Unsigned integer representation . . . . . . . . . . . . . . . . . . . . . 35
2.3.2 Signed integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.3.3 Floating point representations . . . . . . . . . . . . . . . . . . . . . . 36
2.4 Organization of a omputer . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.5 Main memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.5.1 Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.5.2 Ports and operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

1
Abhiram Ranade, 2013. Do not distribute 2
2.6 The arithmeti unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.7 Input-Output Devi es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.7.1 Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.7.2 Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.7.3 Disks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.7.4 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.8 The Control Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.8.1 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.9 High level programming languages . . . . . . . . . . . . . . . . . . . . . . . . 44
2.10 Con luding Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.11 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3 Numbers 46
3.1 Variables and data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.1.1 Identi ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.1.2 Literals and variable initialization . . . . . . . . . . . . . . . . . . . . 49
3.1.3 The onst keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.1.4 Reading data into a variable . . . . . . . . . . . . . . . . . . . . . . . 51
3.1.5 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.1.6 Exa t representational parameters . . . . . . . . . . . . . . . . . . . . 52
3.2 Arithmeti and assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.2.1 Integer division and the modulo operator % . . . . . . . . . . . . . . . 54
3.2.2 Subtleties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.2.3 Over ow and arithmeti errors . . . . . . . . . . . . . . . . . . . . . . 56
3.2.4 In nity and not a number . . . . . . . . . . . . . . . . . . . . . . . . 57
3.2.5 Expli it type onversion . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.2.6 Assignment expression . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.4 Assignment with repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.4.1 Programming Idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
3.4.2 Combining sequen e generation and a umulation . . . . . . . . . . . 62
3.5 Some operators inspired by the idioms . . . . . . . . . . . . . . . . . . . . . 62
3.5.1 In rement and de rement operators . . . . . . . . . . . . . . . . . . . 63
3.5.2 Compound assignment operators . . . . . . . . . . . . . . . . . . . . 63
3.6 Blo ks and variable de nitions . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.6.1 Blo k . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.6.2 General prin iple 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.6.3 General prin iple 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.7 Con luding remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.8 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4 A program design example 71
4.1 Spe i ation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.1.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.2 Program design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Abhiram Ranade, 2013. Do not distribute 3
4.2.1 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.2.2 Corre tness Proof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.2.3 Invariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.3 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.4 Comments in the ode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.5 Con luding remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.5.1 A note on programming exer ises . . . . . . . . . . . . . . . . . . . . 78
4.6 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5 Simple pp graphi s 80
5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.1.1 y axis goes downward! . . . . . . . . . . . . . . . . . . . . . . . . . . 81
5.2 Multiple Turtles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
5.3 Other shapes besides turtles . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.3.1 Cir les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.3.2 Re tangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.3.3 Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.3.4 Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.4 Commands allowed on shapes . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.4.1 Rotation in radians . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
5.4.2 Tra king a shape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
5.4.3 Imprinting on the anvas . . . . . . . . . . . . . . . . . . . . . . . . . 84
5.4.4 Resetting a shape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.5 Cli king on the anvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.6 Proje tile Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
5.7 Best t straight line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.8 Con luding Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
5.9 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
6 Conditional Exe ution 91
6.1 The If statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
6.2 Blo ks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
6.3 Other forms of the if statement . . . . . . . . . . . . . . . . . . . . . . . . . 95
6.4 A di erent turtle ontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
6.4.1 \Buttons" on the anvas . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.5 The swit h statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
6.6 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
6.7 Logi al Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
6.7.1 Reasoning about logi al data . . . . . . . . . . . . . . . . . . . . . . 103
6.7.2 Determining whether a number is prime . . . . . . . . . . . . . . . . 104
6.8 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.9 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Abhiram Ranade, 2013. Do not distribute 4
7 Loops 110
7.1 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
7.1.1 Counting the number of digits . . . . . . . . . . . . . . . . . . . . . . 112
7.1.2 Mark averaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
7.2 The break statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7.3 The ontinue statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
7.4 The do while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
7.5 The for statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
7.5.1 Variables de ned in initialization . . . . . . . . . . . . . . . . . . 120
7.5.2 Break and ontinue . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
7.5.3 Style issue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
7.5.4 Determining if a number is prime . . . . . . . . . . . . . . . . . . . . 121
7.6 Un ommon ways of using for . . . . . . . . . . . . . . . . . . . . . . . . . . 121
7.6.1 Comma separated assignments . . . . . . . . . . . . . . . . . . . . . . 122
7.6.2 Input in initialization and update . . . . . . . . . . . . . . . . . 122
7.7 The Greatest Common Divisor . . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.8 Corre tness of looping programs . . . . . . . . . . . . . . . . . . . . . . . . . 124
7.8.1 Loop Invariant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
7.8.2 Potential . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.8.3 Corre tness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
7.8.4 Additional observations regarding the GCD program . . . . . . . . . 126
7.8.5 Corre tness of other programs . . . . . . . . . . . . . . . . . . . . . . 126
7.9 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
8 Computing ommon mathemati al fun tions 129
8.1 Taylor series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8.1.1 Sine of an angle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
8.1.2 Natural log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
8.1.3 Some general remarks . . . . . . . . . . . . . . . . . . . . . . . . . . 132
8.2 Numeri al integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
8.3 Bise tion method for nding roots . . . . . . . . . . . . . . . . . . . . . . . . 134
8.4 Newton Raphson Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
8.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
8.5.1 Mathemati al ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
8.5.2 Programming ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
9 Fun tions 142
9.1 De ning a fun tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
9.1.1 Exe ution: all by value . . . . . . . . . . . . . . . . . . . . . . . . . 144
9.1.2 Names of parameters and lo al variables . . . . . . . . . . . . . . . . 145
9.2 Nested fun tion alls: LCM . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
9.3 The ontra t view of fun tion exe ution . . . . . . . . . . . . . . . . . . . . . 147
9.3.1 Fun tion spe i ation . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
9.4 Fun tions that do not return values . . . . . . . . . . . . . . . . . . . . . . . 148
9.5 A text drawing program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Abhiram Ranade, 2013. Do not distribute 5
9.6 Some diÆ ulties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
9.7 Call by referen e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
9.7.1 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
9.7.2 Referen e variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
9.8 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
9.8.1 \Address of" operator & . . . . . . . . . . . . . . . . . . . . . . . . . 155
9.8.2 Pointer variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
9.8.3 Dereferen ing operator * . . . . . . . . . . . . . . . . . . . . . . . . . 156
9.8.4 Use in fun tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
9.8.5 Referen e vs. Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . 158
9.9 Returning referen es and pointers . . . . . . . . . . . . . . . . . . . . . . . . 159
9.10 Default values of parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
9.11 Fun tion overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
9.12 Fun tion templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
10 Re ursive Fun tions 164
10.1 Eu lid's algorithm for GCD . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
10.1.1 Exe ution of re ursive g d . . . . . . . . . . . . . . . . . . . . . . . . 166
10.1.2 Interpretation of re ursive programs . . . . . . . . . . . . . . . . . . . 167
10.1.3 Corre tness of re ursive programs . . . . . . . . . . . . . . . . . . . . 167
10.2 Re ursive pi tures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
10.2.1 Trees without using a turtle . . . . . . . . . . . . . . . . . . . . . . . 171
10.2.2 Hilbert spa e lling urve . . . . . . . . . . . . . . . . . . . . . . . . 172
10.3 Virahanka numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
10.3.1 Using a loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
10.3.2 Histori al Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
10.4 The game of Nim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
10.4.1 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
10.5 Con luding remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
11 Program organization and fun tions 185
11.1 The main program is a fun tion! . . . . . . . . . . . . . . . . . . . . . . . . . 186
11.2 Organizing fun tions into les . . . . . . . . . . . . . . . . . . . . . . . . . . 186
11.2.1 Fun tion De laration or Prototype . . . . . . . . . . . . . . . . . . . 186
11.2.2 Splitting a program into multiple les . . . . . . . . . . . . . . . . . . 187
11.2.3 Separate ompilation and obje t modules . . . . . . . . . . . . . . . . 187
11.2.4 Header les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
11.2.5 Header guards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
11.2.6 Pa kaging software . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
11.2.7 Forward de laration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
11.2.8 Fun tion templates and header les . . . . . . . . . . . . . . . . . . . 191
11.3 Namespa es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
11.3.1 De nition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
11.3.2 The using de laration and dire tive . . . . . . . . . . . . . . . . . . . 193
11.3.3 The global namespa e . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Abhiram Ranade, 2013. Do not distribute 6
11.3.4 Unnamed namespa es . . . . . . . . . . . . . . . . . . . . . . . . . . 194
11.3.5 Namespa es and header les . . . . . . . . . . . . . . . . . . . . . . . 194
11.4 Global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
11.5 Two important namespa es . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
11.6 C++ without simple pp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
11.7 Fun tion Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
11.7.1 Some simpli ations . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
11.8 Con luding remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
11.8.1 Fun tion size and readability . . . . . . . . . . . . . . . . . . . . . . . 200
11.9 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
12 Pra ti e of programming: some tips and tools 202
12.1 Clarity of spe i ation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
12.2 Input-output examples and testing . . . . . . . . . . . . . . . . . . . . . . . 204
12.3 Input/output redire tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
12.4 Algorithm design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
12.4.1 Mentally exe ute the program . . . . . . . . . . . . . . . . . . . . . . 208
12.4.2 Test ases for ode overage . . . . . . . . . . . . . . . . . . . . . . . 208
12.5 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
12.5.1 Disabling assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
12.6 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
12.6.1 Debuggers and IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
12.6.2 End of le and data input errors . . . . . . . . . . . . . . . . . . . . . 211
12.6.3 Aside: Input-Output expressions . . . . . . . . . . . . . . . . . . . . 211
12.7 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
12.7.1 The randuv fun tion in simple pp . . . . . . . . . . . . . . . . . . . 213
12.8 Con luding remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
12.9 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
13 Arrays 215
13.1 Array: Colle tion of variables . . . . . . . . . . . . . . . . . . . . . . . . . . 215
13.1.1 Array element operations . . . . . . . . . . . . . . . . . . . . . . . . . 216
13.1.2 A eptable range for the index . . . . . . . . . . . . . . . . . . . . . . 217
13.1.3 Initializing arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
13.2 Examples of use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
13.2.1 Notation for subarrays . . . . . . . . . . . . . . . . . . . . . . . . . . 218
13.2.2 A marks display program . . . . . . . . . . . . . . . . . . . . . . . . . 218
13.2.3 Who got the highest? . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
13.2.4 General roll numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
13.2.5 Histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
13.2.6 A taxi dispat h program . . . . . . . . . . . . . . . . . . . . . . . . . 223
13.2.7 A geometri problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
13.3 The inside story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
13.3.1 Out of range array indi es . . . . . . . . . . . . . . . . . . . . . . . . 228
13.3.2 The array name by itself . . . . . . . . . . . . . . . . . . . . . . . . . 228
Abhiram Ranade, 2013. Do not distribute 7
13.3.3 [℄ as an operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
13.4 Fun tion Calls involving arrays . . . . . . . . . . . . . . . . . . . . . . . . . 231
13.4.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
13.4.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
13.5 Sele tion sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
13.5.1 Estimate of time taken . . . . . . . . . . . . . . . . . . . . . . . . . . 234
13.6 Representing Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
13.7 Array Length and onst values . . . . . . . . . . . . . . . . . . . . . . . . . 236
13.7.1 Why onst de larations? . . . . . . . . . . . . . . . . . . . . . . . . . 237
13.7.2 What we use in this book . . . . . . . . . . . . . . . . . . . . . . . . 238
13.8 Con luding remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
13.9 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
14 More on arrays 243
14.1 Chara ter strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
14.1.1 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
14.1.2 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
14.1.3 Chara ter array pro essing . . . . . . . . . . . . . . . . . . . . . . . . 246
14.1.4 Address arithmeti . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
14.2 Two dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
14.2.1 Linear simultaneous equations . . . . . . . . . . . . . . . . . . . . . . 250
14.2.2 Two dimensional har arrays . . . . . . . . . . . . . . . . . . . . . . 251
14.2.3 Passing 2 dimensional arrays to fun tions . . . . . . . . . . . . . . . . 252
14.2.4 Drawing polygons in simple pp . . . . . . . . . . . . . . . . . . . . . 253
14.3 Arrays of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
14.3.1 Command line arguments to main . . . . . . . . . . . . . . . . . . . . 254
14.4 Binary sear h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
14.4.1 Estimate of time taken . . . . . . . . . . . . . . . . . . . . . . . . . . 257
14.5 Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
14.5.1 A merging algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
14.5.2 Mergesort algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
14.6 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
15 Stru tures 264
15.1 Basi s of stru tures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
15.1.1 Visibility of stru ture types and stru ture variables . . . . . . . . . . 267
15.1.2 Arrays of stru tures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
15.1.3 Pointers to Stru tures and -> . . . . . . . . . . . . . . . . . . . . . . 268
15.1.4 Pointers as stru ture members . . . . . . . . . . . . . . . . . . . . . . 268
15.1.5 Linked stru tures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
15.2 Stru tures and fun tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
15.2.1 onst referen e parameters . . . . . . . . . . . . . . . . . . . . . . . 271
15.2.2 Passing pointers to stru tures . . . . . . . . . . . . . . . . . . . . . . 271
15.3 Representing ve tors from physi s . . . . . . . . . . . . . . . . . . . . . . . . 271
15.4 Taxi dispat h revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Abhiram Ranade, 2013. Do not distribute 8
15.5 Member Fun tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
15.5.1 Referen e parameters and onst . . . . . . . . . . . . . . . . . . . . . 277
15.6 Mis ellaneous features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
15.6.1 Stati data members . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
15.6.2 Stati member fun tions . . . . . . . . . . . . . . . . . . . . . . . . . 278
15.6.3 The this pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
15.6.4 Default values to parameters . . . . . . . . . . . . . . . . . . . . . . . 279
15.7 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
16 Classes: stru tures with a personality 281
16.1 Constru tors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
16.1.1 Calling the onstru tor expli itly . . . . . . . . . . . . . . . . . . . . 284
16.1.2 Default values to parameters . . . . . . . . . . . . . . . . . . . . . . . 284
16.1.3 \Default" Constru tor . . . . . . . . . . . . . . . . . . . . . . . . . . 285
16.1.4 Constru tors of nested stru tures . . . . . . . . . . . . . . . . . . . . 285
16.1.5 Initialization lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
16.1.6 Constant members . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
16.2 The opy onstru tor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
16.3 Destru tors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
16.4 Overloading operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
16.5 Another overloading me hanism . . . . . . . . . . . . . . . . . . . . . . . . . 290
16.6 Overloading assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
16.7 A ess Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
16.7.1 A essor and mutator fun tions . . . . . . . . . . . . . . . . . . . . . 292
16.7.2 Prohibiting ertain operations . . . . . . . . . . . . . . . . . . . . . . 293
16.7.3 Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
16.8 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
16.9 Header and implementation les . . . . . . . . . . . . . . . . . . . . . . . . . 294
16.9.1 Separate ompilation . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
16.9.2 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
16.10Template lasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
16.11Some lasses you have already used, almost . . . . . . . . . . . . . . . . . . . 297
16.11.1Graphi s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
16.11.2Standard input and output . . . . . . . . . . . . . . . . . . . . . . . . 298
16.11.3File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
16.12Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
16.13Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
17 A proje t: osmologi al simulation 301
17.1 Mathemati s of Cosmologi al simulation . . . . . . . . . . . . . . . . . . . . 301
17.2 Overview of the program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
17.2.1 Main Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
17.3 The lass Star . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
17.4 Compiling and exe ution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
17.5 Con luding Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Abhiram Ranade, 2013. Do not distribute 9
17.6 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
18 Graphi s Events 311
18.1 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
18.1.1 Event obje ts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
18.1.2 Waiting for events . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
18.2 Che king for events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
18.2.1 Mouse button press events . . . . . . . . . . . . . . . . . . . . . . . . 312
18.2.2 Mouse drag events . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
18.2.3 Key press events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
18.3 A drawing program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
18.4 A rudimentary Snake game . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
18.4.1 Spe i ation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
18.4.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
18.4.3 User intera tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
18.5 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
19 Representing variable length entities 317
19.1 The Heap Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
19.1.1 A detailed example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
19.1.2 Lifetime and a essibility . . . . . . . . . . . . . . . . . . . . . . . . . 320
19.2 Representing text: a preliminary implementation . . . . . . . . . . . . . . . 320
19.2.1 The basi storage ideas . . . . . . . . . . . . . . . . . . . . . . . . . . 321
19.2.2 Constru tor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
19.2.3 The print member fun tion . . . . . . . . . . . . . . . . . . . . . . . 322
19.2.4 Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
19.2.5 De ning operator + . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
19.3 Advan ed topi s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
19.3.1 Destru tors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
19.3.2 Copy onstru tor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
19.3.3 The [℄ operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
19.3.4 An improved assignment operator . . . . . . . . . . . . . . . . . . . . 326
19.3.5 Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
19.4 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
19.4.1 Class invariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
19.5 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
20 The standard library 332
20.1 The string lass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
20.1.1 Passing strings to fun tions . . . . . . . . . . . . . . . . . . . . . . . 334
20.1.2 A detailed example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
20.2 The template lass ve tor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
20.2.1 Inserting and deleting elements . . . . . . . . . . . . . . . . . . . . . 335
20.2.2 Index bounds he king . . . . . . . . . . . . . . . . . . . . . . . . . . 335
20.2.3 Fun tions on ve tors . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
20.2.4 Ve tors of user de ned data types . . . . . . . . . . . . . . . . . . . . 336
Abhiram Ranade, 2013. Do not distribute 10
20.2.5 Multidimensional ve tors . . . . . . . . . . . . . . . . . . . . . . . . . 336
20.2.6 A matrix lass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
20.3 Sorting a ve tor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
20.4 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
20.4.1 Marks display variation 1 . . . . . . . . . . . . . . . . . . . . . . . . 339
20.4.2 Marks display variation 2 . . . . . . . . . . . . . . . . . . . . . . . . 339
20.4.3 Marks display variation 3 . . . . . . . . . . . . . . . . . . . . . . . . 340
20.5 The map template lass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
20.5.1 Marks display variation 4 . . . . . . . . . . . . . . . . . . . . . . . . 343
20.5.2 Time to a ess a map . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
20.6 Containers and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
20.6.1 Finding and deleting map elements . . . . . . . . . . . . . . . . . . . 347
20.6.2 Inserting and deleting ve tor elements . . . . . . . . . . . . . . . . . 348
20.7 Other ontainers in the standard library . . . . . . . . . . . . . . . . . . . . 348
20.8 The typedef statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
20.8.1 More general form . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
20.9 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
20.10Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
21 Representing networks of entities 353
21.1 Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
21.1.1 Adja en y lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
21.1.2 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
21.1.3 Array indi es rather than pointers . . . . . . . . . . . . . . . . . . . . 355
21.1.4 Edges represented expli itly . . . . . . . . . . . . . . . . . . . . . . . 356
21.2 Adja en y matrix representation . . . . . . . . . . . . . . . . . . . . . . . . . 356
21.3 Sur ng on the internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
21.4 Cir uits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
21.5 Edge list representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
21.6 Auxiliary data stru tures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
21.7 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
21.8 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
22 Stru tural re ursion 364
22.1 Layout of mathemati al formulae . . . . . . . . . . . . . . . . . . . . . . . . 365
22.1.1 Input format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
22.1.2 Layout \by hand" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
22.1.3 Representing mathemati al formulae . . . . . . . . . . . . . . . . . . 368
22.1.4 Reading in a formula . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
22.1.5 Drawing the formula: overview . . . . . . . . . . . . . . . . . . . . . 371
22.1.6 The omplete main program . . . . . . . . . . . . . . . . . . . . . . . 375
22.1.7 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
22.2 Maintaining an ordered set . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
22.2.1 A sear h tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
22.2.2 The general idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Abhiram Ranade, 2013. Do not distribute 11
22.2.3 The implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
22.2.4 A note about organizing the program . . . . . . . . . . . . . . . . . . 380
22.2.5 On the eÆ ien y of sear h trees . . . . . . . . . . . . . . . . . . . . . 380
22.2.6 Balan ing the sear h tree . . . . . . . . . . . . . . . . . . . . . . . . . 382
22.2.7 Sear h trees and maps . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
22.3 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
23 Inheritan e 386
23.1 Turtles with an odometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
23.1.1 Implementation using Composition . . . . . . . . . . . . . . . . . . . 387
23.1.2 Implementation using Inheritan e . . . . . . . . . . . . . . . . . . . . 388
23.2 General prin iples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
23.2.1 A ess rules and prote ted members . . . . . . . . . . . . . . . . . . 390
23.2.2 Constru tors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
23.2.3 Destru tors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
23.2.4 Basi operations on sub lass obje ts . . . . . . . . . . . . . . . . . . 393
23.2.5 The type of a sub lass obje t . . . . . . . . . . . . . . . . . . . . . . 393
23.2.6 Assignments mixing super lass and sub lass obje ts . . . . . . . . . . 393
23.3 Polymorphism and virtual fun tions . . . . . . . . . . . . . . . . . . . . . . . 395
23.3.1 Virtual Destru tor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
23.4 Program to print past tense . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
23.5 Abstra t Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
23.6 Multiple inheritan e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
23.6.1 Diamond Inheritan e . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
23.7 Types of inheritan e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
23.8 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
23.9 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
24 Inheritan e based design 405
24.1 Formula drawing revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
24.1.1 Basi design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
24.1.2 Comparison of the two approa hes . . . . . . . . . . . . . . . . . . . 409
24.1.3 Adding exponential expressions . . . . . . . . . . . . . . . . . . . . . 410
24.2 The simple pp graphi s system . . . . . . . . . . . . . . . . . . . . . . . . . 411
24.3 Composite graphi s obje ts . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
24.3.1 Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
24.3.2 The Composite lass onstru tor . . . . . . . . . . . . . . . . . . . . 415
24.3.3 A Car lass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
24.3.4 Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
24.3.5 Main program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
25 Dis rete event simulation 419
25.1 Dis rete event simulation overview . . . . . . . . . . . . . . . . . . . . . . . 420
25.1.1 Dis rete time systems . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
25.1.2 Evolution of a dis rete time system . . . . . . . . . . . . . . . . . . . 421
25.1.3 Implementing a dis rete time system . . . . . . . . . . . . . . . . . . 422
Abhiram Ranade, 2013. Do not distribute 12
25.1.4 Simple examples of use . . . . . . . . . . . . . . . . . . . . . . . . . . 423
25.2 The restaurant simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
25.3 Resour es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
25.3.1 A Resour e lass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
25.3.2 Simple example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
25.3.3 The o ee shop simulation . . . . . . . . . . . . . . . . . . . . . . . . 429
25.4 Single sour e shortest path . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
25.4.1 Dijkstra's algorithm as a simulation . . . . . . . . . . . . . . . . . . . 433
25.5 Con luding Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
26 Simulation of an airport 438
26.0.1 Chapter outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
26.1 The simulation model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
26.1.1 Overall fun tioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
26.1.2 Safe operation and half-runway-ex lusion . . . . . . . . . . . . . . . . 440
26.1.3 S heduling strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
26.1.4 Gate allo ation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
26.1.5 Simulator input and output . . . . . . . . . . . . . . . . . . . . . . . 441
26.2 Implementation overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
26.2.1 Safe operation and half-runway-ex lusion . . . . . . . . . . . . . . . . 442
26.2.2 Gate representation and allo ation . . . . . . . . . . . . . . . . . . . 442
26.3 Main program and data stru ture . . . . . . . . . . . . . . . . . . . . . . . . 443
26.4 The taxiway lass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
26.5 The ATC lass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
26.6 The plane lass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
26.7 Deadlo ks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
26.8 Con luding remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
27 Non-linear simultaneous equations 454
27.1 Newton-Raphson method in many dimensions . . . . . . . . . . . . . . . . . 454
27.1.1 The general ase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
27.1.2 Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
27.1.3 Initial guess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
27.2 How a ne kla e reposes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
27.2.1 Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
27.2.2 Initial guess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
27.2.3 Experien e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
27.3 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
27.4 Exer ises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
A Installing Simple pp 460
B Managing Heap Memory 461
B.1 Referen e Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
B.2 The template lass shared ptr . . . . . . . . . . . . . . . . . . . . . . . . . 463
B.2.1 Syntheti example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Abhiram Ranade, 2013. Do not distribute 13
B.2.2 General strategy . . . . . . .... . . . . . . . . . . . . . . . . . . . 465
B.2.3 Shared pointer in derivative nding . . . . . . . . . . . . . . . . . . . 465
B.2.4 Weak pointers . . . . . . . . .... . . . . . . . . . . . . . . . . . . . 468
B.2.5 Solution idea . . . . . . . . .... . . . . . . . . . . . . . . . . . . . 468
B.3 Con luding remarks . . . . . . . . . .... . . . . . . . . . . . . . . . . . . . 469
C Libraries 470
C.0.1 Linking built-in fun tions . . . . . . . . . . . . . . . . . . . . . . . . 470
D Reserved words in C++ 472
E Operators and operator overloading 473
E.1 Bitwise Logi al operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
E.1.1 Or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
E.1.2 And . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
E.1.3 Ex lusive or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
E.1.4 Complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
E.1.5 Left shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
E.1.6 Right shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
E.2 Comma operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
E.3 Operator overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
F The stringstream lass 477
G The C++ Prepro essor 479
H Lambda expressions 480
H.1 General form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
H.1.1 The type of a lambda expression . . . . . . . . . . . . . . . . . . . . 481
H.1.2 Variable apture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
H.1.3 Dangling referen es . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
H.1.4 Capturing this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Prefa e

This book presents an introdu tion to programming using the language C++. No prior
knowledge of programming is expe ted, however the book is oriented towards a reader who
has nished about 12 years of s hooling, preferably in the S ien e stream.
The book presents a fairly omprehensive introdu tion to the C++ language, in luding
some ex iting additions to it in the latest standard. However, the goal of the book is to
tea h programming, whi h is di erent from merely learning the di erent statements in any
programming language. Programming, like any skill, must be integrated with the other
knowledge and skills that the learner has. Also, programming is a liberating skill whi h an
hange your world view. Programming enables you to experiment/play with the math or
physi s that you have learned. You have been told that planets must move around the sun;
but if you know programming you an write a program to he k if this laim might be true.
In general, programming should en ourage you to make a model of the world around you
and experiment with it on a omputer. Obviously, this requires a ertain experien e and
knowledge about the world. It is our thesis that 12 years of s hooling in the S ien e stream
an indeed provide su h knowledge. In any ase, the book has been written not only to tea h
programming to our target readership, but also to hallenge and provoke them to using it in
ex iting ways.
This book does not emphasize any spe i programming methodology. Instead, we have
tried to present ideas in order of intelle tual simpli ity as well as simpli ity of programming
syntax. The general presentation style is: \Here is a problem whi h we would like to solve
but we annot using the programming primitives we have learned so far; so let us learn this
new primitive". Obje t oriented programming is learly important, but an attempt is made
to let it evolve as a programming need. We dis uss this more in Se tion 0.3.
This book is expe ted to be used along with a pa kage, simple pp, developed for use
in the book. Appendix A ontains instru tions for installing simple pp and the s ript that
an be used to ompile user programs that use simple pp. This pa kage ontains (a) a set
of graphi s lasses whi h an be used to draw and manipulate simple geometri shapes on
the s reen, and (b) some prepro essor ma ros whi h help in smoothing out the introdu tion
of C++ in the initial period. We feel that the use of simple pp an onsiderably help in
learning C++. This is dis ussed in greater detail below.
We on lude this prefa e by suggesting how this book an be tted into a urri ulum.

0.1 Graphi s
I hear and I forget. I see and I remember. I do and I understand.

14
Abhiram Ranade, 2013. Do not distribute 15
{Confu ius
Traditionally, introdu tory programming uses the domain of numbers or text to illustrate
programming problems and programming strategies. However, a large part of the human
experien e deals with pi tures and motion. This is espe ially true for our target reader.
Humans have evolved to have a good sense of geometry and geography, and are experts
at seeing patterns in pi tures and also planning motion. If this expertise an be brought
into a tion while learning programming, it an make for a more mature intera tion with the
omputer. It is for this reason that simple pp was primarily developed.
Our pa kage simple pp ontains a olle tion of graphi s lasses whi h allow simple geo-
metri al shapes to be drawn and manipulated on the s reen. Ea h shape on the s reen an
be ommanded to move or rotate or s ale as desired. Taking inspiration from the hildren's
programming language Logo, ea h shape also has a pen, whi h may be used to tra e a urve
as the shape moves. The graphi s lasses enable several omputational a tivities su h as
drawing interesting urves and patterns and performing animations together with omputa-
tions su h as ollision dete tion. These a tivities are hallenging and intuitive at the same
time.
The graphi s lasses are used right from the rst hapter. The introdu tory hapter
begins with a program to draw polygons. The program statements ommand a turtle 1

holding a pen to tra es the lines of the polygon. An immediate bene t is that this simple
exer ise makes the imperative aspe t of programming and notions su h as ontrol ow very
obvious. A more important realization, even from this very elementary session is the need to
re ognize patterns in the pi ture being drawn. A pattern in the pi ture often translates to
an appropriate programming pattern, say iteration or re ursion. Identifying and expressing
patterns is a fundamental a tivity in programming. These onsiderations an be brought to
the fore very easily.
As you read along, you will see that graphi s is useful for explaining many on epts, from
variable s oping and parameter passing to inheritan e based design. Graphi al fa ilities make
several traditional examples (e.g. tting lines to points, or simulations) very vivid. Finally,
graphi s is a lot of fun, a fa tor not to be overlooked. After all, edu ators worldwide are
on erned about dwindling student attention and how to attra t students to a ademi s.

0.2 First day/ rst month blues


C++, like many professional programming languages, is not easy to introdu e to novi es.
Many introdutory programming books begin with a simple program that prints the mes-
sage \hello world". On the fa e of it, this is a very natural beginning. However, even a
simple program su h as this appears ompli ated in C++ be ause it must be en ased in a
fun tion, main, having a return type int. The notion of a return type is learly inappropriate
to explain on the very rst day. Other on epts su h as namespa es are even more daunt-
ing. The only possibility is to tell the students, \dont worry about these, you must write
these mantras whose meaning you will understand later". This doesnt seem pedagogi ally
satisfa tory.
1 Represented by a triangle on the s reen, as in the language Logo.
Abhiram Ranade, 2013. Do not distribute 16
After the student has somehow negotiated through int main and namespa es, there is
typi ally a long preparatory period in whi h substantial basi material su h as data types
and ontrol stru tures has to be learnt, until any interesting program an be written. Psy-
hologi ally and logisti ally, this \slow" period is a problem. Psy hologi ally, a preparatory
period without too mu h intelle tual hallenge an be viewed by the student as boring, whi h
is a bad initial impression for the subje t. Se ond, in most ourse o erings, students tend
to have weekly le ture hours and weekly programming pra ti e hours. In the initial weeks,
students are fresh and rearing to go. It is disappointing to them if there is nothing ex iting
to be done, not to mention the waste of time.
To ounter these problems the following features have been in luded in simple pp.
Instead of the main program being spe i ed inside a fun tion main returning int, a
prepro essor ma ro main program is de ned (it expands to int main()). The main program
an be written as
main_program{
body
}
Further, on e the student loads in the simple pp pa kage using #in lude <simple pp>,
nothing additional needs to be loaded, nor using dire tives given. The simple pp pa kage
itself loads other header les su h as iostream and issues the using dire tives. These
\training wheels" are taken o when fun tions et . are explained (Chapter 9).
A se ond \language extension" is the in lusion of a \repeat" statement. This statement
has the form
repeat( ount){body}

and it auses the body to be exe uted as many times as the value of the expression ount.
This is also implemented using prepro essor ma ros and it expands into a for statement.
We believe that the repeat statement is very easy to learn, given a good ontext. Indeed,
it is introdu ed in Chapter 1, where instead of using a separate statement to draw ea h edge
of a polygon, a single line drawing statement inside a repeat statement suÆ es. In fa t the
turtle-based graphi al drawing examples are ompelling enough that students do not have
any diÆ ulty in understanding nested repeat statements either.
In the se ond and third hapters, there is a dis ussion of omputer hardware, data rep-
resentation and data types. These topi s are important, but are not amenable to good
programming exer ises. For this period, the repeat statement together with the notions in-
trodu ed in the rst hapter an be used very fruitfully to generate relevant and interesting
programming exer ises.
In addition, simple pp ontains some mis ellaneous fa ilities, a fun tion to generate
random numbers in a range, and lasses for managing event driven simulations. The ode
for all this is of ourse provided, and the student an look at the ode when he or she is
ready to do so.
Abhiram Ranade, 2013. Do not distribute 17
0.3 Obje t oriented programming
The dominant paradigm in modern programming pra ti e is learly the obje t oriented
paradigm. As a result, there have been approa hes to edu ation whi h attempt to introdu e
lasses and obje ts from \day 1". But even the proponents of these approa hes have noted
that lasses and obje ts are diÆ ult to tea h from the rst day for a number of reasons. For
example, for very simple programs, organizing programs into lasses might be very arti ial
and verbose. Expe ting a student to a tually develop lasses very early requires understand-
ing a fun tion abstra tion (for developing member fun tions/methods) even before ontrol
stru tures are understood. This an appear unmotivated and overwhelming.
Our dis ussion of obje t oriented programming an be onsidered to begin in Chapter 5:
reating a graphi al shape on the s reen requires reating an obje t of a graphi s lass.
However, in the initial hapters, it is only ne essary to use lasses, not build new lasses.
Thus shapes an be reated, and member fun tions invoked on them to move them around
et .
Classes are presented as an evolution of the stru t notion of the programming language
C. The major dis ussion of lasses in luding the modern motivations happens in Chapter 16,
however member fun tions are introdu ed in Se tion 15.5. Inheritan e is presented in Chap-
ter 23. Chapter 24 presents inheritan e based design. It ontains a detailed example in
whi h a program developed earlier, without inheritan e, is redeveloped, but this time using
inheritan e. This vividly shows how inheritan e an help in writing reusable, extensible
ode. A brief des ription of the design of simple pp graphi s system is also given, along
with an extension to handle omposite obje ts.

0.4 Fitting the book into a urri ulum


The book an be used for either a one semester ourse or a two semester sequen e.
For a one semester ourse, the re ommended syllabus is Chapters 1 through Chapter 16,
Chapter 19, Chapter 23. Many of these hapters ontain multiple examples of the same
on ept, all of these need not be \ overed" in lass. In addition, Chapter 20 should be
dis ussed, at least at a high level.
A two semester sequen e an over Chapters 1 through Chapter 16 in the rst semester,
going over them arefully and onsidering at length aspe ts su h as proving orre tness of
programs. The se ond semester ould over the remaining hapters. In a two semester
ourse, it would be appropriate to introdu e some of the modern ideas su h as referen e
ounting pointers (Appendix B).
Chapter 1

Introdu tion

A omputer is one of the most remarkable ma hines invented by man. Most other ma hines
have a very narrow purpose. A wat h shows time, a amera takes pi tures, a tru k arries
goods from one point to another, an ele tron mi ros ope shows magni ed views of very small
obje ts. Some of these ma hines are mu h larger than a omputer, and many mu h more
expensive, but a omputer is mu h, mu h more omplex and interesting in the kind of uses
it an be put to. Indeed, many of these ma hines, from a wat h to an ele tron mi ros ope
typi ally might ontain a omputer inside them, performing some of the most vital fun tions
of ea h ma hine. The goal of this book is to explain how a omputer an possibly be used
for so many purposes, and many more.
Viewed one way, a omputer is simply an ele tri al ir uit; a giant, omplex ele tri al
ir uit, but a ir uit nevertheless. In prin iple, it is possible to make omputers without the
use of ele tri ity { indeed there have been designs of omputers based on using me hani al
gears, or uidi s devi es. But all that is mostly of histori al importan e. For pra ti al
1

purposes, today, it is ne to regard a omputer as an ele tri al ir uit. Parts of this ir uit
are apable of re eiving data from the external world, remembering it so that it an be
reprodu ed later, pro essing it, and sending the results ba k to the external world. By data
we ould mean di erent things. For example, it ould mean some numbers you type from the
keyboard of a omputer. Or it ould mean ele tri al signals a omputer an re eive from a
sensor whi h senses temperature, pressure, light intensity and so on. The word pro ess might
mean something as simple as al ulating the average of the sequen e of numbers you type
from the keyboard. It ould also mean something mu h more omplex: e.g. determining
whether the signals re eived from a light sensor indi ate that there is some movement in
the vi inity of the sensor. Finally, by \send data to the external world" we might mean
something as simple as printing the al ulated average on the s reen of your omputer so
that you an read it. Or we ould mean a tivating a beeper onne ted to your omputer if
the movement dete ted is deemed suspi ious. Exa tly whi h parts of the ir uit are a tive
at what time is de ided by a program fed to the omputer.
It is the program whi h distinguishes a omputer from most other ma hines; by installing
di erent programs the same omputer an be made to behave in dramati ally di erent ways.
How to develop these programs is the subje t of this book. In this hapter, we will begin
1 Also it is appropriate to think of our own brain as a omputer made out of biologi al material, i.e.
neurons or neural ells.

18
Abhiram Ranade, 2013. Do not distribute 19
by seeing an example of a program. It turns out that we an understand, or even develop
(typi ally alled write) programs without knowing a lot about the spe i ir uits that the
omputer ontains. Learning to write programs is somewhat similar to how one might learn
to drive a ar; learly one an learn to drive without knowing how exa tly an automobile
engine works. Indeed, not only will you be able understand the program that we show you,
but you will immediately be able to write some simple programs.
There are many languages using whi h programs an be written. The language we will
use in this book is the C++ programming language, invented in the early 1980s by Bjarne
Stroustrup. For the initial part of the book, we will not use the bare C++ language, but
instead augment it with a pa kage alled simple pp whi h we have developed. How to
install this pa kage is explained in Appendix A. We developed this pa kage so that C++
appears more friendly and more fun to people who are starting to learn C++. To use the
driving metaphor again, it ould be said that C++ is like a omplex ra ing ar. When you
are learning to drive, it is better to start with a simpler vehi le, in whi h there aren't too
many onfusing ontrols. Also, standard C++ does not by default ontain the ability to
draw pi tures. The pa kage simple pp does ontain this feature. We thus expe t that by
using the simple pp pa kage it will be easier and more fun to learn the language. But in
a few hapters (by Se tion 11.6), you will outgrow simple pp and be able to use standard
C++ (like \the pros"), unless of ourse you are using the graphi s features.

1.1 A simple program


Our rst example program is given below.
#in lude <simple pp>
main_program{
turtleSim();

forward(100);
left(90);
forward(100);
left(90);
forward(100);
left(90);
forward(100);

wait(5);
}

If you exe ute this program on your omputer, it will rst open a window. Then a small
triangle whi h we all a turtle will appear in the window. Then the turtle will move and
2

draw lines as it moves. The turtle will draw a square and then stop. After that, the window
will vanish, and the program will end. Shortly we will des ribe how to exe ute the program.
2 Our turtle is meant to mimi the turtle in the Logo programming language.
Another random document with
no related content on Scribd:
Half portions. Ferber, E. (Jl ’20)
Hall with doors. Hasbrouck, L. S. (N ’20)
Hamilton, Alexander, 1757–1804
Ford, H: J. Alexander Hamilton. (Ag ’20)
Hamlet. Stoll, E. E. (Je ’20)
Hand in the dark. Rees, A. J: (S ’20)
Hand-made fables. Ade, G: (Ap ’20)
Handbook for practical farmers. Findlay, H., ed.
(Ja ’21)
Handbook for rural school officers. Showalter, N.
D: (Jl ’20)
Handbook of American government. Bartlett, W:
H. (S ’20)
Handbook of ore dressing, equipment and
practice. Allen, A. W. (F ’21)
Handbook on health and how to keep it. Camp, W.
C. (My ’20)
Handbook to the league of nations. Butler, G. G.
(My ’20)
Hands off Mexico. Turner, J: K. (Jl ’20)
Hannah Bye. Morris, H. S. (Ja ’21)
Happily married. Harris, C. M. (Ap ’20)
Happy bride. Jesse, F. T. (D ’20)
Happy house. Abbott, J. L. (Jl ’20)
Happy house. Hutten zum Stolzenberg, B. (Ap
’20)
Happy hunting grounds. Roosevelt. K. (D ’20)
Happy woman. Weyl, M. (Ap ’20)
Harbors
Rush, T. E. Port of New York. (Jl ’20)
Harriet and the piper. Norris, K. (S ’20)
Hartley, Catherine Gasquoine. See Gallichan,
C. G.
Harvest. Ward, M. A. (My ’20)
Haunted hour. Widdemer, M. (Ap ’20)
Havana
Hergesheimer, J. San cristóbal de la Habana.
(Ja ’21)
Have we a Far Eastern policy? Sherrill, C: H. (Ag
’20)
Hay, Elijah, pseud. See Seiffert, M. A.
Head of the house. Eng title of Lister’s great
adventure. Bindloss, H. (F ’21)
Health
Roget, F. R. Altitude and health. (D ’20)
Heart
Diseases
Bishop, L: F. Heart troubles. (S ’20)
Heart of Cherry McBain. Durkin, D. (N ’20)
Heart of New England. Brown, A. F. (N ’20)
Heart of the world. Jones, J. H:, jr. (Ag ’20)
Heart of Unaga. Cullum, R. (N ’20)
Hearts of three. London, J. (F ’21)
Heavens and earth. Benét, S. V. (F ’21)
Hellenic conceptions of peace. Caldwell, W. E. (Mr
’20)
Helmets and body armor in modern warfare.
Dean, B. (O ’20)
Helping Hersey. Hutten zum Stolzenberg, B. (Ag
’20)
Helping men own farms. Mead, E. (Ag ’20)
Henry Elizabeth. McCarthy, J. H. (S ’20)
Henry V, king of England, 1387–1422
Mowat, R. B. Henry V. (D ’20)
Hero of the longhouse. Laing, M. E. (F ’21)
Hero stories of France. Tappan, E. M. (Je ’20)
Heroes
Dimmock, F. H., ed. Scouts’ book of heroes. (F
’21)
Lanier, H: W. Book of bravery. (Ja ’21)
Heroes of early Israel. Wood, I. F. (F ’21)
Hesitant heart. Welles, W. (My ’20)
Hey-rub-a-dub-dub. Dreiser, T. (Je ’20)
Hidden Creek. Burt, K. N. (O ’20)
Hidden eyes. Levison, E. (F ’21)
Hidden people. Miller, L. E: (Ja ’21)
Hidden trails. White, W: P. (O ’20)
Hidden treasure of Rasmola. Rihbany, A. M. (F
’21)
Hide and seek. Morley, C. D. (D ’20)
High company. Lee, H. S. (O ’20)
High life. Rhodes, H. G. (F ’21)
High school boy and his problems. Clark, T: A. (N
’20)
High school farces. Hill, F: T. (Ja ’21)
High schools
Parker, S: C. Methods of teaching in high
schools. (Jl ’20)
Highacres. Abbott, J. L. (D ’20)
Higher psychical development. Carrington, H. (N
’20)
Highway to leadership. Slattery, M. (F ’21)
Hiker Joy. Connolly, J. B. (Je ’20)
Hills of Han. Merwin, S: (D ’20)
His friend and his wife. Hamilton, C. (Je ’20)
His Majesty’s well-beloved. Orczy, E. (Ap ’20)
Hispanic anthology. Walsh, T:, ed. (D ’20)
Historical child. Chrisman, O. (F ’21)
Historical portraits, 1700–1850. Fletcher, C: R. L.
(D ’20)
Historical sources of Defoe’s Journal of the plague
year. Nicholson, W. (S ’20)
History
Reade, W. Martyrdom of man. (Ag ’20)
Outlines, syllabi, etc.
Putnam, G: P., comp. Tabular views of
universal history. (Ap ’20)
Philosophy
Adams, H: Degradation of the democratic
dogma. (Ap ’20)
Partridge, G: E. Psychology of nations. (My
’20)
Study and teaching
Hasluck, E. L: Teaching of history. (F ’21)
Tuell, H. E. Study of nations. (Mr ’20)
History, Ancient
Murray, G. Our great war and the great war of
the ancient Greeks. (Ag ’20)
History, Modern
West, W. M. Story of modern progress. (Jl ’20)
History, Universal
Bryce, J. B. World history. (F ’21)
Wells, H. G: Outline of history. (D ’20)
History and power of mind. Ingalese, R: (Ag ’20)
History of a literary radical. Bourne, R. S. (F ’21)
History of American literature. Boynton, P. H. (Mr
’20)
History of Cuba. Johnson, W. F. (S ’20)
History of education. Cubberley, E. P. (Ja ’21)
History of Europe, ancient and medieval.
Breasted, J. H:, and Robinson, J. H. (O ’20)
History of everyday things in England. Quennell,
M. and C: H: B. (Jl ’20)
History of France. Davis, W: S. (Ag ’20)
History of France. Duruy, V: (D ’20)
History of imperialism. Tucker, I. St J: (Ja ’21)
History of journalism in the United States. Payne.
G: H: (Ag ’20)
History of modern colloquial English. Wyld, H: C.
K. (D ’20)
History of Persian literature under Tartar
dominion. Browne, E: G. (F ’21)
History of sea power. Stevens, W: O., and
Westcott, A. F. (Ja ’21)
History of the A. E. F. Thomas, S. (D ’20)
History of the American field service in France. (N
’20)
History of the Atlantic coast line railroad. Dozier,
H. D. (Je ’20)
History of the great war. Doyle, A. C. (Ap ’20)
History of the Japanese people. Brinkley, F., and
Kikuchi, D. (O ’20)
History of the thrift movement in America. Straus,
S. W: (Ap ’20)
History of the United States from Hayes to
McKinley. Rhodes, J. F. (Ag ’20)
History of the world war. Simonds, F. H. (O ’20)
History of trade unionism. Webb, S. and B. (Je
’20)
Hither and thither in Germany. Howells, W: D.
(Mr ’20)
Holland, Henry Fox, 1st baron, 1705–1774
Ilchester, G. S. H. F. Henry Fox. (N ’20)
Holy fire. Wylie, I. A. R. (Ag ’20)
Home—then what? (Ap ’20)
Home economics
Cooley, A. M., and Spohr, W. Household arts for
home and school. (My ’20)
Cooley, A. M., and others. Teaching home
economics. (My ’20)
Willard, F., and Gillett, L. H. Dietetics for high
schools. (F ’21)
Home-education of children. Forbush, W: B. (Ap
’20)
Homespun and gold. Brown, A. (Ja ’21)
Honest thief. Dostoevskii, F. M. (Ap ’20)
Honor Bright. Richards, L. E. (O ’20)
Hoover, Herbert Clark, 1874–
Kellogg, V. L. Herbert Hoover. (Je ’20)
Lane, R. Making of Herbert Hoover. (N ’20)
Hope of the world. Wilson, W. (Jl ’20)
Horses
Poetry
Frothingham, R., comp. Songs of horses. (N
’20)
Horsley, Sir Victor Alexander Haden,
1857–1916
Paget, S. Sir Victor Horsley. (D ’20)
Hosiery manufacture. Davis, W: (O ’20)
Hot bulb oil engines and suitable vessels. Pollock,
W. (Ja ’21)
Hours of labor
Leverhulme, W: H. L. Six-hour shift and
industrial efficiency. (Jl ’20)
House of Baltazar. Locke, W: J: (Mr ’20)
House of dreams-come-true. Pedler, M. (S ’20)
House of dust. Aiken, C. P. (N ’20)
House organs
O’Shea, P: F. Employees’ magazines. (O ’20)
Ramsay, R. E. Effective house organs. (Ap ’20)
House with a bad name. Sheehan, P. P. (D ’20)
Household arts for home and school. Cooley, A.
M., and Spohr, W. (My ’20)
Housing and the housing problem. Aronovici, C.
(S ’20)
Housing problem
Aronovici, C. Housing and the housing problem.
(S ’20)
Knowles, M. Industrial housing. (Ja ’21)
Whitaker, C: H. Joke about housing. (Ag ’20)
How I filmed the war. Malins, G. H. (Ap ’20)
How it feels to be fifty. Butler, E. P. (Ag ’20)
How many cards? Ostrander, I. E. (Ja ’21)
How presidents are made. Dunn, A. W. (Je ’20)
How the war came. Loreburn, R. T. R. (My ’20)
How to become an office stenographer. Mason, W.
L. (Ap ’20)
How to develop your will power. Major, C. T. (N
’20)
How to reduce. Donnelly, A. (Ja ’21)
How to study music. Farnsworth, C: H. (D ’20)
How to teach in Sunday-school. Schmauk, T. E.
(Ag ’20)
How to use cement for concrete construction for
town and farm. Campbell, H: C. (Je ’20)
How to write special feature articles. Bleyer, W. G.
(Mr ’20)
How we advertised America. Creel, G: (Ag ’20)
Human and industrial efficiency. Chellew, H: (N
’20)
Human costs of the war. Folks, H. (Jl ’20)
Human efficiency and levels of intelligence.
Goddard, H: H. (D ’20)
Human factor in education. Munroe, J. P. (D ’20)
Human factor in industry. Frankel, L. K., and
Fleisher, A. (S ’20)
Human nature in business. Kelly, F. C. (Jl ’20)
Human personality and its survival of bodily
death. Myers, F: W: H: (Ag ’20)
Human psychology. Warren, H. C. (Jl ’20)
Human traits and their social significance.
Edman, I. (N ’20)
Humanism in New England theology. Gordon, G:
A. (My ’20)
Humanizing industry. Feld, R. C. (O ’20)
Humor
Ade, G: Hand-made fables. (Ap ’20)
Cobb, I. S. Abandoned farmers. (D ’20)
Cobb, I. S., and Rinehart, M. R. Oh, well, you
know how women are! and Isn’t that just like
a man! (My ’20)
Robey, G: My rest cure. (Ap ’20)
Humours of a parish, and other quaintnesses.
Money, W. B. (O ’20)
Hunger. Hamsun, K. (D ’20)
Hungry hearts. Yezierska, A. (D ’20)
Hunting
Caswell, J: Sporting rifles and rifle shooting. (S
’20)
Clapham, R: Foxhunting on the Lakeland fells.
(Ja ’21)
Newbolt, H: J: Book of good hunting. (F ’21)
Roosevelt, K. Happy hunting grounds. (D ’20)
Stebbing, E: P. Diary of a sportsman naturalist
in India. (Ja ’21)
Husband. Anstruther, E. H. A. (Jl ’20)
Hygiene
Camp, W. C. Handbook on health and how to
keep it. (My ’20)
Miles, E. H. Self-health as a habit. (D ’20)
Proceedings of the international conference of
women physicians. (Ja ’21)
Hygiene, Public
Henry, A. Forests, woods and trees in relation to
hygiene. (Ja ’21)
Hyphen. Schem, L. C. (Ja ’21)

I wonder why. Goldsmith, M. (O ’20)


Ibos (Nigeria)
Basden, G: T: Among the Ibos of Nigeria. (Ja
’21)
Idea of atonement in Christian theology. Rashdall,
H. (Je ’20)
Idea of progress. Bury, J: B. (N ’20)
Ideals of America. City club of Chicago. (Je ’20)
Idling in Italy. Collins, J. (D ’20)
Idolatry of science. Coleridge, S. (F ’21)
Idyl of the split bamboo. Holden, G: P. (Ja ’21)
If you don’t write fiction. Cushing, C: P. (Ag ’20)
Ignition devices
Pagé, V: W. Automobile starting, lighting, and
ignition. (D ’20)
Illinois
Social life and customs
Reed, E. H. Tales of a vanishing river. (F ’21)
Illustrating of books
Whiting, J: D. Practical illustration. (Ja ’21)
Imagination and its place in education.
Kirkpatrick, E. A. (D ’20)
Immigration
Davis, P., and Schwartz, B., eds. Immigration
and Americanization. (Mr ’20)
Drachsler, J. Democracy and assimilation. (F
’21)
Orth, S: P. Our foreigners. (D ’20)
Immigration and Americanization. Davis, P., and
Schwartz, B., eds. (Mr ’20)
Immortality
Heagle, D: Do the dead still live? (Jl ’20)
Tweedale, C: L. Man’s survival after death. (D
’20)
Imperfect mother. Beresford, J: D. (Jl ’20)
Imperial trans-Antarctic expedition, 1914–
1917
Shackleton, E. H: South. (Ap ’20)
Imperialism
Tucker, I. St J: History of imperialism. (Ja ’21)
Impressions that remained. Smyth, E. (My ’20)
In April once. Percy, W: A. (D ’20)
In Berkshire fields. Eaton, W. P. (N ’20)
In chancery. Galsworthy, J: (N ’20)
In Kut and captivity with the Sixth Indian
division. Sandes, E: W. C. (F ’21)
In Lincoln’s chair. Tarbell, I. M. (My ’20)
In lower Florida wilds. Simpson, C: T. (O ’20)
In Morocco. Wharton, E. N. (D ’20)
In old Pennsylvania towns. Wharton, A. H. (F ’21)
In the days of the Pilgrim fathers. Crawford, M. C.
(Jl ’20)
In the garret. Van Vechten, C. (Mr ’20)
In the house of another. Mantle, B. (D ’20)
In the mountains. (N ’20)
In the prison camps of Germany. Hoffman, C. (Ja
’21)
In the shadow of Lantern street. Woodworth, H.
G. (Jl ’20)
In the tracks of the trades. Freeman, L: R. (N ’20)
In the world war. Czernin von und zu Chudenitz,
O. T. O: M. (My ’20)
Inbreeding and outbreeding. East, E: M., and
Jones, D. F. (Ag ’20)
Income
Pickard, B. Reasonable revolution. (My ’20)
Index number for state school systems. Ayres, L.
P. (F ’21)
Index to St Nicholas. Guthrie, A. L., comp. (Ap
’20)
India
Bannerjea, D. N. India’s nation builders. (N ’20)
Ladd, G: T. Intimate glimpses of life in India.
(Mr ’20)
History
Moreland, W: H. India at the death of Akbar.
(F ’21)
Politics and government
Macdonald, J. R. Government of India. (D
’20)
Mookerji, R. Local government in ancient
India. (D ’20)
Young, P. N. F., and Ferrers, A. India in
conflict. (F ’21)
India at the death of Akbar. Moreland, W: H. (F
’21)
India in conflict. Young, P. N. F., and Ferrers, A.
(F ’21)
Indiana
Description and travel
Parsons, J: Tour through Indiana in 1840. (O
’20)
Indians of North America
Laing, M. E. Hero of the longhouse. (F ’21)
Social life and customs
Autobiography of a Winnebago Indian. (S
’20)
India’s nation builders. Bannerjea, D. N. (N ’20)
Indiscretions of the naval censor. Brownrigg, D. E.
R. (Je ’20)
Industrial administration. Berriman, A. E., and
others. (Ja ’21)
Industrial arts
Griffith, I. S: Teaching manual and industrial
arts. (Ag ’20)
Industrial gases. Greenwood, H. C. (Ag ’20)
Industrial housing. Knowles, M. (Ja ’21)
Industrial management
Muscio, B. Lectures on industrial psychology.
(N ’20)
Tead, O., and Metcalf, H: C. Personnel
administration. (D ’20)
Industrial relations
Baker, R. S. New industrial unrest. (Je ’20)
Bloomfield, D., comp. Selected articles on
modern industrial movements. (Ag ’20)
Goodrich, C. L. Frontier of control. (Ja ’21)
Holmes, J: H. Is violence the way out of our
industrial disputes? (Je ’20)
Leverhulme, W: H. L. Six-hour shift and
industrial efficiency. (Jl ’20)
Litchfield, P. W. Industrial republic. (Jl ’20)
Thomas, E: Industry, emotion and unrest. (O
’20)
Industrial republic. Litchfield, P. W. (Jl ’20)
Industrial research
Mees, C: E: K. Organization of industrial
scientific research. (S ’20)
Industrialism
Eckel, E. C. Coal, iron and war. (S ’20)
Industry and state
Berriman, A. E., and others. Industrial
administration. (Ja ’21)
Industry, emotion and unrest Thomas, E: (O ’20)
Inevitable. Couperus, L: M. A. (D ’20)
Inflation and high prices. Academy of political
science. (F ’21)
Influence of animism on Islam. Zwemer, S: M. (Ag
’20)
Influence of oversea expansion on England to
1700. Gillespie, J. E: (F ’21)
Influence of Puritanism on the political and
religious thought of the English. Flynn, J: S. (F
’21)
Inland navigation
Ogilvie, P. M. International waterways. (Je ’20)
Inorganic chemical synonyms. Darling, E. R. (Mr
’20)
Inscrutable lovers. MacFarlan, A. (Ap ’20)
Insects
Herrick, G. W. Insects of economic importance.
(O ’20)
Inside story of Austro-German intrigue.
Gori[)c]ar, J., and Stowe, L. B. (Ap ’20)
Inside story of the peace conference. Dillon, E. J.
(Mr ’20)
Insomnia
Walsh, W: S. Yours for sleep. (S ’20)
Instigations of Ezra Pound. Pound, E. L. (S ’20)
Insurance, Marine
Huebner, S. S. Marine insurance. (D ’20)
Intellectuals and the wage workers. Cory, H. E.
(My ’20)
Interim. Richardson, D. M. (Ag ’20)
Internal-combustion engines. Lind, W. L. (F ’21)
International commerce and reconstruction.
Friedman, E. M. (Jl ’20)
International labor legislation. Ayusawa, I. F: (Ja
’21)
International law and relations

You might also like