An Introduction to Programming through C++ Abhiram G. Ranade all chapter instant download
An Introduction to Programming through C++ Abhiram G. Ranade all chapter instant download
com
https://ebookmass.com/product/an-introduction-to-
programming-through-c-abhiram-g-ranade/
OR CLICK BUTTON
DOWNLOAD NOW
https://ebookmass.com/product/an-introduction-to-parallel-
programming-2-edition-pacheco/
ebookmass.com
https://ebookmass.com/product/an-introduction-to-parallel-programming-
second-edition-peter-s-pacheco/
ebookmass.com
https://ebookmass.com/product/gateways-to-democracy-an-introduction-
to-american-government-john-g-geer/
ebookmass.com
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/
ebookmass.com
https://ebookmass.com/product/c-programming-language-c-programming-
lang-_p2-2nd-edition-ebook-pdf/
ebookmass.com
https://ebookmass.com/product/introduction-to-java-programming-
comprehensive-version-y-daniel-liang/
ebookmass.com
https://ebookmass.com/product/through-the-lens-of-anthropology-an-
introduction-to-human-evolution-and-culture-1st-edition-ebook-pdf/
ebookmass.com
An Introdu tion to Programming through C++
Abhiram Ranade
Do not distribute
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
Discovering Diverse Content Through
Random Scribd Documents
IV
COPENHAGUE. — ASPECT GÉNÉRAL DE LA
VILLE. — LES PALAIS.
Mais si l’on veut étudier l’art danois dans sa plus haute et sa plus
pure expression, c’est au musée Thorvaldsen qu’il faut aller.
Thorvaldsen est un de ces hommes qui suffisent à la gloire d’une
époque et d’une nation. Le Danemark peut s’enorgueillir, à juste
titre, d’avoir produit l’un des plus grands sculpteurs du dix-neuvième
siècle, le rival et le vainqueur peut-être de Canova ; le maître illustre,
dont l’influence ne s’est pas seulement exercée sur l’école danoise,
sortie tout entière de lui, mais domine aujourd’hui encore presque
toute la sculpture du nord de l’Europe et celle de l’Allemagne.
A quelques pas du palais de Christiansborg s’élève un édifice qui
a l’aspect d’un mausolée. L’architecture reproduit en partie celle des
sépulcres grecs et étrusques, et la décoration rappelle les ornements
des tombeaux antiques. C’est à la fois le musée et la sépulture de
Thorvaldsen. Le grand sculpteur repose dans la cour centrale, sous
un petit tertre ombragé de lierre, au milieu de ses innombrables
chefs-d’œuvre.
Le monument fut érigé par la ville de Copenhague, avec le
concours d’une souscription publique, du vivant même de
Thorvaldsen, pour recevoir les objets d’art qu’il avait légués à ses
compatriotes. La façade est surmontée d’un groupe de la Victoire
arrêtant son quadrige et, sur les murs extérieurs, de vastes
compositions en ciments de diverses couleurs incrustés dans la
pierre, représentent le retour de Thorvaldsen dans sa ville natale en
1838, et le transport de ses œuvres du vaisseau jusqu’au Musée.
L’art antique a fourni les motifs de toutes les décorations. Ici, c’est
un génie qu’un char emporte dans l’arène ; là, ce sont des vases et
des trépieds, comme les anciens en donnaient pour prix dans les
jeux publics, couronnés des lauriers et des palmes du triomphe.
Entre le vestibule, qui occupe toute la largeur de l’édifice, et la
grande salle du fond, sont disposés en enfilade une série de
cabinets, dans chacun desquels s’élève une statue choisie, entourée
d’un cortége de bustes et de bas-reliefs. Les plafonds sont égayés de
cartouches dans le goût des peintures de Pompéi ; mais la nudité des
murailles, simplement recouvertes d’une couche de couleur brunâtre,
laisse toute leur valeur aux statues, qui se détachent
vigoureusement dans les conditions les plus favorables et les mieux
calculées pour les faire valoir. Le musée Thorvaldsen rachète sa
physionomie vraiment trop funèbre par son heureux aménagement
et l’intelligente appropriation des moindres parties de l’édifice au but
qu’on s’est proposé. C’est vraiment le temple de l’art.
Un Grec du temps de Périclès se fût promené avec délices dans
ce monument peuplé de chefs-d’œuvre où l’antiquité revit.
Thorvaldsen est un élève de Phidias : il a, sans effort, et comme par
un épanouissement naturel, la noblesse, la simplicité sévère,
l’harmonie et la grandeur des lignes, la science du dessin, la pureté
de style, l’élégance correcte et la clarté lumineuse des maîtres
souverains. Ce fils du Nord, dont le génie de bonne heure éveillé
s’échauffa lentement, et resta longtemps à demi engourdi comme
dans les brumes de son pays natal, a l’heureuse fécondité, la
hardiesse tranquille, la perfection calme et sûre d’elle-même, qui
caractérisent le génie grec. Ses bustes sont presque tous admirables
par l’accent de réalité, le caractère et l’expression qu’il leur donne,
sans jamais violer en rien les vieilles traditions classiques. Par la
science de la composition, la pondération des groupes, la sagesse du
plan, la gravité des lignes, il a conquis dans le bas-relief une
suprématie qui n’est pas discutée. La plupart de ses sujets antiques,
les Trois Grâces, l’Amour triomphant, la Vénus, le Jason, le Mercure,
le Bacchus, l’Adonis, le Ganymède, semblent arrachés aux ruines du
Parthénon. Bien qu’il ait surtout la noblesse et la force, il a aussi la
finesse ingénieuse et la grâce délicate. Toutefois son œuvre charme
plus qu’elle n’émeut ; elle s’adresse aux yeux et à l’esprit sans arriver
jusqu’au cœur ; elle ne cause que cette admiration presque froide où
l’âme ne se sent pas suffisamment intéressée. Il resta toujours, sous
l’inspiration du puissant artiste, un peu de cette glace du Nord que
les flammes du soleil d’Italie ne suffirent point à faire fondre
entièrement, et tout en rendant à ce tranquille et harmonieux génie
l’hommage qu’il mérite, je lui voudrais parfois plus de chaleur, de vie
et d’élan pathétique.
Le musée Thorvaldsen contient les dessins, les esquisses et les
modèles originaux en plâtre de son œuvre entière, qui est immense.
On y a joint tous les objets d’art qui se trouvaient en sa possession
au moment de sa mort, et ce second musée n’offre qu’un intérêt
bien médiocre à côté du premier. Les tableaux danois y abondent,
mais les bons ouvrages y sont rares. On y remarquera surtout un
très-spirituel et très-vivant portrait de l’artiste, par son ami Horace
Vernet. Thorvaldsen est dans son atelier, en costume de travail,
tenant en main l’ébauchoir et accoudé sur le socle qui supporte la
statue d’Œhlenschläger. Une bonhomie loyale et une sorte de
naïveté rêveuse se lisent sur cette figure encadrée de longs cheveux
gris et percée de petits yeux bleus. La tête est puissante, et pourtant
elle a quelque chose d’enfantin ; le regard est limpide comme une
source ; l’expression a cette placidité qui fit plus d’une fois accuser
d’insouciance et même de paresse l’un des artistes les plus
prodigieusement, mais aussi les plus tranquillement actifs qu’on ait
jamais vus.
Lorsque Thorvaldsen avait quitté Rome pour revenir dans sa
patrie, on l’avait accueilli comme un triomphateur. Quand il mourut,
la nation fit cortége à son cercueil, et ses funérailles eurent presque
le caractère d’une apothéose. Moins de six ans après, l’ami et
l’émule du grand sculpteur, qui nous a conservé ses traits ; celui qui
éleva la gloire de la poésie danoise presque aussi haut que
Thorvaldsen avait élevé la gloire de l’art danois, Œhlenschläger
mourait à son tour. Tous les spectacles et toutes les réjouissances
publiques étaient suspendues pendant huit jours. La stalle qu’il avait
coutume d’occuper au grand théâtre restait vide et voilée d’un crêpe
durant six mois. Vingt mille personnes, le prince royal, le conseil
entier des ministres, les généraux, tout le clergé, tous les corps et
métiers avec leurs bannières, suivaient par les rues sablées et
jonchées de verdure, entre les maisons tendues de draperies
funèbres, le cercueil du poëte, que les étudiants avaient réclamé
l’honneur de porter eux-mêmes. Nous sommes ici chez un peuple
qui sait honorer ses grands hommes et qui est digne d’en avoir.
J’ai vu, à deux pas de l’hôtel Phœnix, au milieu d’une avenue
plantée d’arbres, la statue d’Œhlenschläger. Il est représenté en robe
de chambre, assis dans son fauteuil ; il a le front haut, le visage
ouvert et la mâle stature de ces héros du Nord qui revivent si bien
dans ses vers. Personne n’a mieux chanté que lui ce « pays
charmant, couvert de larges hêtres, à côté de la Baltique amère, —
qui s’appelle le vieux Danemark, et qui est la demeure de Freya » ;
cette patrie des géants héroïques, « dont les os reposent sous les
monuments des collines », et qui n’a point perdu sa beauté, « car la
mer bleue lui fait une ceinture, et le feuillage vert une couronne ; et
de nobles femmes, de belles filles, des hommes vaillants et des
jeunes gens au cœur déterminé peuplent les îles des Danois [11] . »
Thorvaldsen est un Grec, qui demande son inspiration à l’Olympe ;
Œhlenschläger, un Scandinave pur-sang, qui est allé chercher la
sienne dans le Walhalla.
[11] Fædrelandssang, ou Chant national
d’Œhlenschläger. Comme la plupart de ses poésies, ce
chant est presque intraduisible, car il a surtout un mérite
de style.
On me pardonnera cette digression, qui n’en est pas une, sur les
chansons populaires, où se manifestent si naïvement le génie d’une
race et son caractère national, et ces citations directement puisées à
la source, qui donneront peut-être à l’un de nos lecteurs l’envie de
reprendre et de compléter le curieux recueil de M. Marmier. Les
chants modernes n’ont, d’ailleurs, ni l’intérêt, ni la signification des
Kœmpeviser, où l’on trouve une peinture si frappante et si sincère,
dans sa rudesse même, des mœurs, des idées, des croyances du
Nord. A ce point de vue, on peut dire des légendes poétiques du
Danemark qu’elles sont plus vraies que l’histoire, et le recueil formé
par M. Svend Grundtvig [19] pourrait susciter un nouveau Saxo
Grammaticus.
[19] Gamle danske Folkeviser (Anciennes chansons
populaires danoises). M. Svend Grundtvig est le fils du
célèbre pasteur dont il sera question plus loin.
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.
ebookmass.com