Welcome to UE Central Library

Keep Smiling

Programming and problem solving with C++

Dale, Nell

Programming and problem solving with C++ / Nell Dale, Chip Weems. - 4th ed. - Boston : Jones and Bartlett Publishers, 2005 - xxvii, 1119 p. ill. ; 24 cm.

Includes index.

1. Preface v
2. 1 overview of programming and problem solving 1
1.1 Overview of programming 2
3. What is programming? 2
4. How do we write a program? 3
1.2 What is a programming language? 9
1.3 What is a computer? 15
1.4 Ethics and responsibilities in the computing profession 24
5. Software piracy 24
6. Privacy of data 25
7. Use of computer resources 26
8. Software engineering 27
1.5 Problem-solving techniques 27
9. Ask questions 28
10. Look for things that are familiar 28
11. Solve by analogy 28
12. Means-ends analysis 29
13. Divide and conquer 30
14. Dalephattoc 11/4/01 2:34 pm page xiii
15. The building-block approach 30
16. Merging solutions 31
17. Mental blocks: the fear of starting 32
18. Algorithmic problem solving 33
19. Problem-solving case study: an algorithm for an employee
20. Paycheck 33
21. Summary 37
22. Quick check 38
23. Answers 39
24. Exam preparation exercises 39
25. Programming warm-up exercises 41
26. Case study follow-up 41
2 C++ syntax and semantics, and the program development
27. Process 43
2.1 The elements of c++ programs 44
28. C++ program structure 44
29. Syntax and semantics 46
30. Syntax templates 49
31. Naming program elements: identifiers 52
32. Data and data types 53
33. Naming elements: declarations 56
34. Taking action: executable statements 61
35. Beyond minimalism: adding comments to a program 66
2.2 Program construction 67
36. Blocks (compound statements) 69
37. The c++ preprocessor 71
38. An introduction to namespaces 73
2.3 More about output 74
39. Creating blank lines 74
40. Inserting blanks within a line 75
2.4 Program entry, correction, and execution 76
41. Entering a program 76
42. Compiling and running a program 77
43. Finishing up 78
44. Problem-solving case study: contest letter 79
45. Xiv | contents
46. Dalephattoc 11/4/01 2:34 pm page xiv
47. Testing and debugging 83
48. Summary 84
49. Quick check 85
50. Answers 87
51. Exam preparation exercises 88
52. Programming warm-up exercises 90
53. Programming problems 92
54. Case study follow-up 94
3 Numeric types, expressions, and output 95
3.1 Overview of c++ data types 96
3.2 Numeric data types 97
55. Integral types 97
56. Floating-point types 98
3.3 Declarations for numeric types 99
57. Named constant declarations 99
58. Variable declarations 100
3.4 Simple arithmetic expressions 101
59. Arithmetic operators 101
60. Increment and decrement operators 104
3.5 Compound arithmetic expressions 105
61. Precedence rules 105
62. Type coercion and type casting 106
3.6 Function calls and library functions 111
63. Value-returning functions 111
64. Library functions 113
65. Void functions 114
3.7 Formatting the output 115
66. Integers and strings 115
67. Floating-point numbers 118
3.8 Additional string operations 122
68. The length and size functions 122
69. The find function 124
70. The substr function 125
71. Problem-solving case study: painting traffic cones 128
72. Contents | xv
73. Dalephattoc 11/4/01 2:34 pm page xv
74. Testing and debugging 132
75. Summary 133
76. Quick check 133
77. Answers 135
78. Exam preparation exercises 136
79. Programming warm-up exercises 140
80. Programming problems 143
81. Case study follow-up 145
4 Program input and the software design process 147
4.1 Getting data into programs 148
82. Input streams and the extraction operator (>>) 149
83. The reading marker and the newline character 152
84. Reading character data with the get function 153
85. Skipping characters with the ignore function 156
86. Reading string data 157
4.2 Interactive input/output 158
4.3 Noninteractive input/output 160
4.4 File input and output 161
87. Files 161
88. Using files 162
89. An example program using files 165
90. Run-time input of file names 167
4.5 Input failure 168
4.6 Software design methodologies 170
4.7 What are objects? 171
4.8 Object-oriented design 173
4.9 Functional decomposition 174
91. Modules 176
92. Implementing the design 177
93. A perspective on design 181
94. Problem-solving case study: stretching a canvas 183
95. Testing and debugging 189
96. Testing and debugging hints 191
97. Xvi | contents
98. Dalephattoc 11/4/01 2:34 pm page xvi
99. Summary 191
100. Quick check 192
101. Answers 193
102. Exam preparation exercises 193
103. Programming warm-up exercises 196
104. Programming problems 198
105. Case study follow-up 199
5 Conditions, logical expressions, and selection
106. Control structures 201
5.1 Flow of control 202
107. Selection 203
5.2 Conditions and logical expressions 204
108. The bool data type 204
109. Logical expressions 205
110. Precedence of operators 214
111. Relational operators with floating-point types 216
5.3 The if statement 217
112. The if-then-else form 217
113. Blocks (compound statements) 220
114. The if-then form 222
115. A common mistake 224
5.4 Nested if statements 224
116. The dangling else 228
5.5 Testing the state of an i/o stream 229
117. Problem-solving case study: warning notices 231
118. Testing and debugging 236
119. Testing in the problem-solving phase: the algorithm
120. Walk-through 236
121. Testing in the implementation phase 239
122. The test plan 244
123. Tests performed automatically during compilation and execution
124. 246
125. Testing and debugging hints 247
126. Contents | xvii
127. Dalephattoc 11/4/01 2:34 pm page xvii
128. Summary 249
129. Quick check 249
130. Answers 250
131. Exam preparation exercises 250
132. Programming warm-up exercises 254
133. Programming problems 256
134. Case study follow-up 259
6 Looping 261
6.1 The while statement 262
6.2 Phases of loop execution 264
6.3 Loops using the while statement 265
135. Count-controlled loops 265
136. Event-controlled loops 267
137. Looping subtasks 273
6.4 How to design loops 276
138. Designing the flow of control 277
139. Designing the process within the loop 278
140. The loop exit 279
6.5 Nested logic 280
141. Designing nested loops 284
142. Problem-solving case study: average income by gender 291
143. Testing and debugging 297
144. Loop-testing strategy 297
145. Test plans involving loops 297
146. Testing and debugging hints 299
147. Summary 300
148. Quick check 301
149. Answers 301
150. Exam preparation exercises 302
151. Programming warm-up exercises 305
152. Programming problems 305
153. Case study follow-up 308
154. Xviii | contents
155. Dalephattoc 11/4/01 2:34 pm page xviii
7 Functions 309
7.1 Functional decomposition with void functions 310
156. When to use functions 311
157. Writing modules as void functions 311
7.2 An overview of user-defined functions 316
158. Flow of control in function calls 316
159. Function parameters 316
7.3 Syntax and semantics of void functions 319
160. Function call (invocation) 319
161. Function declarations and definitions 320
162. Local variables 322
163. The return statement 324
164. Header files 325
7.4 Parameters 326
165. Value parameters 327
166. Reference parameters 328
167. An analogy 331
168. Matching arguments with parameters 332
7.5 Designing functions 335
169. Writing assertions as program comments 337
170. Documenting the direction of data flow 339
171. Problem-solving case study: comparison of
172. Furniture-store sales 343
173. Testing and debugging 352
174. The assert library function 353
175. Testing and debugging hints 354
176. Summary 355
177. Quick check 356
178. Answers 357
179. Exam preparation exercises 357
180. Programming warm-up exercises 363
181. Programming problems 365
182. Case study follow-up 369
183. Contents | xix
184. Dalephattoc 11/4/01 2:34 pm page xix
8 Scope, lifetime, and more on functions 371
8.1 Scope of identifiers 372
185. Scope rules 374
186. Variable declarations and definitions 378
187. Namespaces 379
8.2 Lifetime of a variable 382
188. Initializations in declarations 382
8.3 Interface design 384
189. Side effects 384
190. Global constants 387
8.4 Value-returning functions 389
191. Boolean functions 394
192. Interface design and side effects 398
193. When to use value-returning functions 399
194. Problem-solving case study: reformat dates 401
195. Problem-solving case study: starship weight
196. And balance 412
197. Testing and debugging 423
198. Stubs and drivers 423
199. Testing and debugging hints 425
200. Summary 426
201. Quick check 427
202. Answers 428
203. Exam preparation exercises 428
204. Programming warm-up exercises 432
205. Programming problems 433
206. Case study follow-up 435
9 Additional control structures 437
9.1 The switch statement 438
9.2 The do-while statement 443
9.3 The for statement 446
9.4 The break and continue statements 450
9.5 Guidelines for choosing a looping statement 453
207. Problem-solving case study: monthly rainfall averages 454
208. Xx | contents
209. Dalephattoc 11/4/01 2:34 pm page xx
210. Testing and debugging 459
211. Testing and debugging hints 460
212. Summary 460
213. Quick check 461
214. Answers 461
215. Exam preparation exercises 462
216. Programming warm-up exercises 463
217. Programming problems 465
218. Case study follow-up 467
10 Simple data types: built-in and user-defined 469
10.1 Built-in simple types 470
219. Integral types 472
220. Floating-point types 475
10.2 Additional c++ operators 476
221. Assignment operators and assignment expressions 478
222. Increment and decrement operators 479
223. Bitwise operators 480
224. The cast operation 480
225. The sizeof operator 481
226. The ?: operator 481
227. Operator precedence 482
10.3 Working with character data 484
228. Character sets 485
229. C++ char constants 487
230. Programming techniques 488
10.4 More on floating-point numbers 495
231. Representation of floating-point numbers 495
232. Arithmetic with floating-point numbers 498
233. Implementation of floating-point numbers in the computer 499
10.5 User-defined simple types 505
234. The typedef statement 506
235. Enumeration types 506
236. Named and anonymous data types 513
237. User-written header files 514
238. Contents | xxi
239. Dalephattoc 11/4/01 2:34 pm page xxi
10.6 More on type coercion 515
240. Type coercion in arithmetic and relational expressions 516
241. Type coercion in assignments, argument passing, and return of a
242. Function value 517
243. Problem-solving case study: finding the area under
244. A curve 519
245. Problem-solving case study: rock, paper, scissors 527
246. Testing and debugging 536
247. Floating-point data 536
248. Coping with input errors 536
249. Testing and debugging hints 537
250. Summary 539
251. Quick check 539
252. Answers 540
253. Exam preparation exercises 540
254. Programming warm-up exercises 543
255. Programming problems 544
256. Case study follow-up 545
11 Structured types, data abstraction, and classes 547
11.1 Simple versus structured data types 548
11.2 Records (c++ structs) 549
257. Accessing individual components 551
258. Aggregate operations on structs 553
259. More about struct declarations 554
260. Hierarchical records 555
11.3 Unions 557
11.4 Data abstraction 559
11.5 Abstract data types 561
11.6 C++ classes 564
261. Classes, class objects, and class members 568
262. Built-in operations on class objects 569
263. Class scope 571
264. Information hiding 571
11.7 Specification and implementation files 573
265. The specification file 573
266. Xxii | contents
267. Dalephattoc 11/4/01 2:34 pm page xxii
268. The implementation file 575
269. Compiling and linking a multifile program 580
11.8 Guaranteed initialization with class constructors 582
270. Invoking a constructor 584
271. Revised specification and implementation files
272. For timetype 585
273. Guidelines for using class constructors 588
274. Problem-solving case study: manipulating dates 590
275. Problem-solving case study: birthday calls 602
276. Testing and debugging 610
277. Testing and debugging hints 614
278. Summary 615
279. Quick check 615
280. Answers 617
281. Exam preparation exercises 619
282. Programming warm-up exercises 622
283. Programming problems 624
284. Case study follow-up 628
12 Arrays 631
12.1 One-dimensional arrays 632
285. Declaring arrays 634
286. Accessing individual components 635
287. Out-of-bounds array indexes 638
288. Initializing arrays in declarations 638
289. (lack of) aggregate array operations 639
290. Examples of declaring and accessing arrays 640
291. Passing arrays as arguments 645
292. Assertions about arrays 648
293. Using typedef with arrays 648
12.2 Arrays of records and class objects 649
294. Arrays of records 649
295. Arrays of class objects 651
12.3 Special kinds of array processing 652
296. Subarray processing 652
297. Indexes with semantic content 652
298. Contents | xxiii
299. Dalephattoc 11/4/01 2:35 pm page xxiii
12.4 Two-dimensional arrays 653
12.5 Processing two-dimensional arrays 656
300. Sum the rows 657
301. Sum the columns 659
302. Initialize the array 660
303. Print the array 661
12.6 Passing two-dimensional arrays as arguments 662
12.7 Another way of defining two-dimensional arrays 664
12.8 Multidimensional arrays 666
304. Problem-solving case study: comparison of two lists 669
305. Problem-solving case study: city council election 675
306. Testing and debugging 685
307. One-dimensional arrays 685
308. Complex structures 686
309. Multidimensional arrays 687
310. Testing and debugging hints 688
311. Summary 689
312. Quick check 689
313. Answers 691
314. Exam preparation exercises 692
315. Programming warm-up exercises 698
316. Programming problems 701
317. Case study follow-up 705
13 Array-based lists 707
13.1 The list as an abstract data type 708
13.2 Unsorted lists 713
318. Basic operations 713
319. Insertion and deletion 716
320. Sequential search 718
321. Sorting 721
13.3 Sorted lists 724
322. Basic operations 726
323. Insertion 727
324. Sequential search 730
325. Binary search 730
326. Deletion 736
327. Xxiv | contents
328. Dalephattoc 11/4/01 2:35 pm page xxiv
13.4 Understanding character strings 739
329. Initializing c strings 742
330. C string input and output 743
331. C string library routines 746
332. String class or c strings? 747
333. Problem-solving case study: exam attendance 748
334. Testing and debugging 755
335. Testing and debugging hints 756
336. Summary 757
337. Quick check 757
338. Answers 758
339. Exam preparation exercises 758
340. Programming warm-up exercises 761
341. Programming problems 762
342. Case study follow-up 763
14 Object-oriented software development 765
14.1 Object-oriented programming 766
14.2 Objects 768
14.3 Inheritance 769
343. Deriving one class from another 770
344. Specification of the exttime class 774
345. Implementation of the exttime class 776
346. Avoiding multiple inclusion of header files 780
14.4 Composition 781
347. Design of a timecard class 782
348. Implementation of the timecard class 783
14.5 Dynamic binding and virtual functions 785
349. The slicing problem 787
350. Virtual functions 788
14.6 Object-oriented design 790
351. Step 1: identify the objects and operations 790
352. Step 2: determine the relationships among objects 792
353. Step 3: design the driver 792
14.7 Implementing the design 793
354. Problem-solving case study: time card lookup 794
355. Contents | xxv
356. Dalephattoc 11/4/01 2:35 pm page xxv
357. Testing and debugging 814
358. Testing and debugging hints 815
359. Summary 816
360. Quick check 816
361. Answers 818
362. Exam preparation exercises 819
363. Programming warm-up exercises 822
364. Programming problems 823
365. Case study follow-up 824
15 Pointers, dynamic data, and reference types 825
15.1 Pointers 826
366. Pointer variables 826
367. Pointer expressions 831
15.2 Dynamic data 836
15.3 Reference types 842
15.4 Classes and dynamic data 846
368. Class destructors 851
369. Shallow versus deep copying 852
370. Class copy-constructors 854
371. Problem-solving case study: personnel records 857
372. Problem-solving case study: dynamic arrays 872
373. Testing and debugging 882
374. Testing and debugging hints 884
375. Summary 885
376. Quick check 886
377. Answers 887
378. Exam preparation exercises 888
379. Programming warm-up exercises 892
380. Programming problems 893
381. Case study follow-up 894
16 Linked structures 897
16.1 Sequential versus linked structures 898
16.2 Array representation of a linked list 900
382. Xxvi | contents
383. Dalephattoc 11/4/01 2:35 pm page xxvi
16.3 Dynamic data representation of a linked list 902
384. Algorithms on dynamic linked lists 908
385. Pointer expressions 926
386. Classes and dynamic linked lists 927
16.4 Choice of data representation 929
387. Problem-solving case study: simulated playing cards 930
388. Problem-solving case study: solitaire simulation 938
389. Testing and debugging 956
390. Testing and debugging hints 956
391. Summary 956
392. Quick check 957
393. Answers 957
394. Exam preparation exercises 957
395. Programming warm-up exercises 960
396. Programming problems 961
397. Case study follow-up 962
17 Templates and exceptions 963
17.1 Template functions 964
398. Function overloading 964
399. Defining a function template 967
400. Instantiating a function template 968
401. Enhancing the print template 969
402. User-defined specializations 970
403. Organization of program code 971
17.2 Template classes 974
404. Instantiating a class template 976
405. Organization of program code 978
406. A caution 981
17.3 Exceptions 982
407. The throw statement 983
408. The try-catch statement 985
409. Nonlocal exception handlers 988
410. Re-throwing an exception 991
411. Standard exceptions 992
412. Back to the division-by-zero problem 995
413. Contents | xxvii
414. Dalephattoc 11/4/01 2:35 pm page xxvii
415. Problem-solving case study: the sortedlist class revisited 996
416. Testing and debugging 1007
417. Testing and debugging hints 1007
418. Summary 1008
419. Quick check 1009
420. Answers 1010
421. Exam preparation exercises 1011
422. Programming warm-up exercises 1012
423. Programming problems 1014
424. Case study follow-up 1014
18 Recursion 1017
18.1 What is recursion? 1018
18.2 Recursive algorithms with simple variables 1022
18.3 Towers of hanoi 1025
18.4 Recursive algorithms with structured variables 1030
18.5 Recursion using pointer variables 1032
425. Printing a dynamic linked list in reverse order 1032
426. Copying a dynamic linked list 1035
18.6 Recursion or iteration? 1040
427. Problem-solving case study: converting decimal integers to
428. Binary integers 1041
429. Problem-solving case study: minimum value in an
430. Integer array 1044
431. Testing and debugging 1046
432. Testing and debugging hints 1046
433. Summary 1047
434. Quick check 1047
435. Answers 1048
436. Exam preparation exercises 1048
437. Programming warm-up exercises 1050
438. Programming problems 1052
439. Case study follow-up 1053
440. Appendix a reserved words 1055
441. Appendix b operator precedence 1055
442. Xxviii | contents
443. Dalephattoc 11/4/01 2:35 pm page xxviii
444. Appendix c a selection of standard library routines 1057
445. Appendix d using this book with a prestandard version of c++ 1066
446. Appendix e character sets 1071
447. Appendix f program style, formatting, and documentation 1073
448. Glossary 1081
449. Answers to selected exercises 1091


0763707988 (pbk.)


C++ (Computer program language)

005.133 / D1391
Copyright © 2023, University of Education, Lahore. All Rights Reserved.
Email:centrallibrary@ue.edu.pk