Table Of ContentC++ for Mathematicians
An Introduction for Students and Professionals
Edward Scheinerman
Cover photograph: Ira Scheinerman
Cover design concept: Jonah Scheinerman
Published in 2006 by
Chapman & Hall/CRC
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2006 by Taylor & Francis Group, LLC
Chapman & Hall/CRC is an imprint of Taylor & Francis Group
No claim to original U.S. Government works
Printed in the United States of America on acid-free paper
10 9 8 7 6 5 4 3 2 1
International Standard Book Number-10: 1-58488-584-X (Softcover)
International Standard Book Number-13: 978-0978-1-58488-584-9 (Softcover)
This book contains information obtained from authentic and highly regarded sources. Reprinted material is
quoted with permission, and sources are indicated. A wide variety of references are listed. Reasonable efforts
have been made to publish reliable data and information, but the author and the publisher cannot assume
responsibility for the validity of all materials or for the consequences of their use.
No part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic,
mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and
recording, or in any information storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com
(http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC) 222 Rosewood Drive,
Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration
for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate
system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only
for identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
Taylor & Francis Group and the CRC Press Web site at
is the Academic Division of Informa plc. http://www.crcpress.com
In loving memory of Pauline and of Arnold
Contents
ListofPrograms xiii
ListofFigures xvii
Preface xix
I Procedures 1
1 TheBasics 3
1.1 WhatisC++? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 HelloC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2 Numbers 11
2.1 Theintegertypes . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 Therealnumbertypes . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Theboolandchartypes . . . . . . . . . . . . . . . . . . . . . 14
2.4 Checkingthesizeandcapacityofthedifferenttypes . . . . . . . 15
2.5 Standardoperations . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.6 ComparisonsandBooleanoperations . . . . . . . . . . . . . . . 22
2.7 Complexnumbers . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.8 Namingvariables . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3 GreatestCommonDivisor 31
3.1 Theproblem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2 Afirstapproach . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.3 Euclid’smethod . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.4 Loopingwithfor,while,anddo . . . . . . . . . . . . . . . . 41
3.5 AnexhaustiveapproachtotheGCDproblem . . . . . . . . . . . 43
3.6 Extendedgcd,callbyreference,andoverloading . . . . . . . . . 45
3.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4 RandomNumbers 53
4.1 Pseudorandomnumbergeneration . . . . . . . . . . . . . . . . . 53
4.2 Uniformrandomvalues . . . . . . . . . . . . . . . . . . . . . . . 54
4.3 Moreonpseudorandomnumbergeneration . . . . . . . . . . . . 57
4.4 AMonteCarloprogramfortheGCDproblem . . . . . . . . . . . 60
v
vi C++forMathematicians
4.5 Normalrandomvalues . . . . . . . . . . . . . . . . . . . . . . . 61
4.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5 Arrays 67
5.1 Euler’stotient . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.2 Arrayfundamentals . . . . . . . . . . . . . . . . . . . . . . . . . 69
5.3 Aproceduretofactorintegers . . . . . . . . . . . . . . . . . . . 71
5.4 AproceduretocalculateEuler’stotient . . . . . . . . . . . . . . . 76
5.5 TheSieveofEratosthenes: newanddelete[] . . . . . . . . . 78
5.6 Afastertotient . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.7 Computing p forlargen . . . . . . . . . . . . . . . . . . . . . . 85
n
5.8 Theanswer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
II Objects 91
6 PointsinthePlane 93
6.1 Dataandmethods . . . . . . . . . . . . . . . . . . . . . . . . . . 93
6.2 DeclaringthePointclass . . . . . . . . . . . . . . . . . . . . . 94
6.3 Datahiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
6.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.5 Assignmentandconversion . . . . . . . . . . . . . . . . . . . . . 100
6.6 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
6.7 ProceduresusingargumentsoftypePoint . . . . . . . . . . . . 103
6.8 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
6.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
7 PythagoreanTriples 115
7.1 GeneratingPythagoreantriples . . . . . . . . . . . . . . . . . . . 115
7.2 DesigningaprimitivePythagoreantripleclass . . . . . . . . . . . 116
7.3 ImplementationofthePTripleclass . . . . . . . . . . . . . . . 117
7.4 Findingandsortingthetriples . . . . . . . . . . . . . . . . . . . 121
7.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
8 Containers 127
8.1 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.2 Setiterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
8.3 Multisets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
8.4 Adjustablearraysviathevectorclass . . . . . . . . . . . . . . 134
8.5 Orderedpairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
8.6 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
8.7 Lists,stacks,andassortedqueues . . . . . . . . . . . . . . . . . . 144
8.7.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
8.7.2 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
8.7.3 Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
TableofContents vii
8.7.4 Deques. . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
8.7.5 Priorityqueues . . . . . . . . . . . . . . . . . . . . . . . 150
8.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
9 ModularArithmetic 157
9.1 DesigningtheModtype . . . . . . . . . . . . . . . . . . . . . . . 157
9.2 Thecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
9.3 Thedefaultmodulus: Staticclassvariablesandmethods . . . . . 163
9.4 Constructorsandget/setmethods . . . . . . . . . . . . . . . . . . 167
9.5 Comparisonoperators . . . . . . . . . . . . . . . . . . . . . . . . 167
9.6 Arithmeticoperators . . . . . . . . . . . . . . . . . . . . . . . . 169
9.7 WritingModobjectstooutputstreams . . . . . . . . . . . . . . . 172
9.8 AmaintodemonstratetheModclass . . . . . . . . . . . . . . . 172
9.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
10 TheProjectivePlane 177
10.1 Introductiontotheprojectiveplane,RP2 . . . . . . . . . . . . . . 177
10.2 DesigningtheclassesPPointandPLine . . . . . . . . . . . . 178
10.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
10.4 Protectedclassmembers . . . . . . . . . . . . . . . . . . . . . . 184
10.5 ClassandfileorganizationforPPointandPLine . . . . . . . . 186
10.6 TheparentclassPObject . . . . . . . . . . . . . . . . . . . . . 187
10.7 TheclassesPPointandPLine . . . . . . . . . . . . . . . . . . 195
10.8 Discoveringandrepairingabug . . . . . . . . . . . . . . . . . . 200
10.9 Pappusrevisited . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
10.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11 Permutations 215
11.1 Ulam’sproblem . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
11.2 DesigningthePermutationclass . . . . . . . . . . . . . . . . 217
11.2.1 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
11.2.2 Constructorsanddestructors . . . . . . . . . . . . . . . . 218
11.2.3 Copyandassign . . . . . . . . . . . . . . . . . . . . . . . 220
11.2.4 Basicinspectionandmodificationmethods. . . . . . . . . 223
11.2.5 Permutationoperations . . . . . . . . . . . . . . . . . . . 224
11.2.6 Comparisonoperators . . . . . . . . . . . . . . . . . . . . 225
11.2.7 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
11.2.8 ThecodefilePermutation.c . . . . . . . . . . . . . . 225
11.3 Findingmonotonesubsequences . . . . . . . . . . . . . . . . . . 229
11.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
viii C++forMathematicians
12 Polynomials 235
12.1 Proceduretemplates . . . . . . . . . . . . . . . . . . . . . . . . . 235
12.2 Classtemplates . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
12.2.1 Usingclasstemplates . . . . . . . . . . . . . . . . . . . . 238
12.2.2 Creatingclasstemplates. . . . . . . . . . . . . . . . . . . 239
12.3 ThePolynomialclasstemplate . . . . . . . . . . . . . . . . . 242
12.3.1 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
12.3.2 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . 243
12.3.3 Getandsetmethods . . . . . . . . . . . . . . . . . . . . . 244
12.3.4 Functionmethods . . . . . . . . . . . . . . . . . . . . . . 245
12.3.5 Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
12.3.6 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . 246
12.3.7 Outputtothescreen . . . . . . . . . . . . . . . . . . . . . 247
12.3.8 GCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.3.9 Thecode . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.4 TheGCDproblemrevisited . . . . . . . . . . . . . . . . . . . . . 254
12.5 Workinginbinary . . . . . . . . . . . . . . . . . . . . . . . . . . 258
12.5.1 Signedversusunsignedintegers . . . . . . . . . . . . . . 258
12.5.2 Bitoperations . . . . . . . . . . . . . . . . . . . . . . . . 259
12.5.3 Thebitsetclasstemplate . . . . . . . . . . . . . . . . 260
12.5.4 Classtemplateswithnon-typearguments. . . . . . . . . . 263
12.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
III Topics 267
13 UsingOtherPackages 269
13.1 Arbitraryprecisionarithmetic: TheGMPpackage . . . . . . . . . 269
13.2 Linearalgebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
13.2.1 Two-dimensionalarraysinC++. . . . . . . . . . . . . . . 273
13.2.2 TheTNTandJAMApackages . . . . . . . . . . . . . . . 274
13.2.3 Thenewmatpackage . . . . . . . . . . . . . . . . . . . . 282
13.3 Otherpackages . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
13.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
14 Strings,Input/Output,andVisualization 289
14.1 Characterarrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
14.2 Thestringclass . . . . . . . . . . . . . . . . . . . . . . . . . 291
14.2.1 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . 291
14.2.2 Fundamentaloperations . . . . . . . . . . . . . . . . . . . 292
14.2.3 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . 295
14.2.4 Convertingbetweenstringandchar*types . . . . . . 297
14.3 Commandlinearguments . . . . . . . . . . . . . . . . . . . . . . 297
14.4 Readingandwritingdatainfiles . . . . . . . . . . . . . . . . . . 300
14.4.1 Openingfilesforinput/output . . . . . . . . . . . . . . . . 300
14.4.2 Readingandwriting . . . . . . . . . . . . . . . . . . . . . 303
TableofContents ix
14.4.3 Detectingtheendofaninputfile . . . . . . . . . . . . . . 304
14.4.4 Othermethodsforinput . . . . . . . . . . . . . . . . . . . 305
14.5 Stringstreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
14.6 Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
14.6.1 Settingprecision . . . . . . . . . . . . . . . . . . . . . . 309
14.6.2 Showingalldigits . . . . . . . . . . . . . . . . . . . . . . 309
14.6.3 Settingthewidth . . . . . . . . . . . . . . . . . . . . . . 310
14.6.4 Othermanipulators . . . . . . . . . . . . . . . . . . . . . 311
14.7 Aclasstoparsefiles . . . . . . . . . . . . . . . . . . . . . . . . 311
14.8 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
14.8.1 Introducingandinstallingtheplotutilspackage. . . . 316
14.8.2 Drawingwithplotutils—afirstexample . . . . . . . 317
14.8.3 Pascal’strianglemodulotwo . . . . . . . . . . . . . . . . 322
14.8.4 Tracingthemotionofapointmovingrandomlyinatriangle 324
14.8.5 DrawingPaleygraphs . . . . . . . . . . . . . . . . . . . . 326
14.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
15 OddsandEnds 333
15.1 Theswitchstatement . . . . . . . . . . . . . . . . . . . . . . . 333
15.2 Labelsandthegotostatement . . . . . . . . . . . . . . . . . . . 336
15.3 Exceptionhandling . . . . . . . . . . . . . . . . . . . . . . . . . 338
15.3.1 Thebasicsoftry,throw,andcatch . . . . . . . . . . 338
15.3.2 Otherfeaturesoftheexception-handlingsystem . . . . . . 342
15.4 Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
15.5 Otherwaystocreatetypes . . . . . . . . . . . . . . . . . . . . . 347
15.5.1 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 347
15.5.2 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . 348
15.5.3 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
15.5.4 Usingtypedef . . . . . . . . . . . . . . . . . . . . . . 349
15.6 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
15.6.1 Pointerbasics . . . . . . . . . . . . . . . . . . . . . . . . 350
15.6.2 Dereferencing . . . . . . . . . . . . . . . . . . . . . . . . 351
15.6.3 Arraysandpointerarithmetic . . . . . . . . . . . . . . . . 353
15.6.4 newanddeleterevisited . . . . . . . . . . . . . . . . . 355
15.6.5 Whyusepointers? . . . . . . . . . . . . . . . . . . . . . . 356
15.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
IV Appendices 361
A YourC++ComputingEnvironment 363
A.1 Programmingwithacommandwindowandatexteditor . . . . . 363
A.1.1 Whatyouneedandhowtogetit(forfree) . . . . . . . . . 364
A.1.2 Editingprogramfiles . . . . . . . . . . . . . . . . . . . . 365
A.1.3 Compilingandrunningyourprogram . . . . . . . . . . . 366
A.1.4 Compileroptions . . . . . . . . . . . . . . . . . . . . . . 368
x C++forMathematicians
A.1.5 Introductiontomake . . . . . . . . . . . . . . . . . . . . 370
A.2 Programmingwithanintegrateddevelopmentenvironment . . . . 372
A.2.1 VisualC++forWindows . . . . . . . . . . . . . . . . . . 373
A.2.2 XcodeforMacintoshOSX . . . . . . . . . . . . . . . . . 376
A.3 Generaladviceondebugging . . . . . . . . . . . . . . . . . . . . 378
B DocumentationwithDoxygen 381
B.1 Doxygencomments . . . . . . . . . . . . . . . . . . . . . . . . . 381
B.1.1 Documentingfiles . . . . . . . . . . . . . . . . . . . . . . 382
B.1.2 Documentingprocedures . . . . . . . . . . . . . . . . . . 382
B.1.3 Documentingclasses,data,andmethods . . . . . . . . . . 383
B.2 UsingDoxygen . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
B.2.1 ConfiguringDoxygen . . . . . . . . . . . . . . . . . . . . 386
B.2.2 RunningDoxygen . . . . . . . . . . . . . . . . . . . . . . 389
B.2.3 Morefeatures . . . . . . . . . . . . . . . . . . . . . . . . 389
C C++Reference 391
C.1 Variablesandtypes . . . . . . . . . . . . . . . . . . . . . . . . . 391
C.1.1 Fundamentaltypes . . . . . . . . . . . . . . . . . . . . . 391
C.1.2 Standardclasses/templates . . . . . . . . . . . . . . . . . 391
C.1.3 Declaringvariables . . . . . . . . . . . . . . . . . . . . . 392
C.1.4 Staticvariablesandscope . . . . . . . . . . . . . . . . . . 392
C.1.5 Constantsandthekeywordconst . . . . . . . . . . . . . 393
C.1.6 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
C.2 Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
C.2.1 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . 394
C.2.2 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . 394
C.2.3 Comparisonoperators . . . . . . . . . . . . . . . . . . . . 394
C.2.4 Logicaloperators . . . . . . . . . . . . . . . . . . . . . . 394
C.2.5 Bitoperators . . . . . . . . . . . . . . . . . . . . . . . . . 395
C.2.6 Potpourri . . . . . . . . . . . . . . . . . . . . . . . . . . 395
C.3 Controlstatements . . . . . . . . . . . . . . . . . . . . . . . . . 396
C.3.1 if-else . . . . . . . . . . . . . . . . . . . . . . . . . . 396
C.3.2 Looping: for,while,anddo . . . . . . . . . . . . . . . 396
C.3.3 switch . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
C.3.4 goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
C.3.5 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 398
C.4 Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
C.4.1 Fileorganization . . . . . . . . . . . . . . . . . . . . . . 399
C.4.2 Callbyvalueversuscallbyreference . . . . . . . . . . . 399
C.4.3 Array(andpointer)arguments . . . . . . . . . . . . . . . 400
C.4.4 Defaultvaluesforarguments . . . . . . . . . . . . . . . . 400
C.4.5 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . 400
C.4.6 inlineprocedures. . . . . . . . . . . . . . . . . . . . . 401
C.5 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401