100% found this document useful (3 votes)
32 views107 pages

(Ebook) An Introduction To Programming With C++, 6th Edition by Diane Zak ISBN 9780538466523, 0538466529 Available Full Chapters

Study material: (Ebook) An Introduction to Programming with C++, 6th Edition by Diane Zak ISBN 9780538466523, 0538466529 Download instantly. A complete academic reference filled with analytical insights and well-structured content for educational enrichment.

Uploaded by

jaquelinc4442
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
32 views107 pages

(Ebook) An Introduction To Programming With C++, 6th Edition by Diane Zak ISBN 9780538466523, 0538466529 Available Full Chapters

Study material: (Ebook) An Introduction to Programming with C++, 6th Edition by Diane Zak ISBN 9780538466523, 0538466529 Download instantly. A complete academic reference filled with analytical insights and well-structured content for educational enrichment.

Uploaded by

jaquelinc4442
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

(Ebook) An Introduction to Programming with C++, 6th

Edition by Diane Zak ISBN 9780538466523, 0538466529


Pdf Download

https://ebooknice.com/product/an-introduction-to-programming-
with-c-6th-edition-2427428

★★★★★
4.6 out of 5.0 (20 reviews )

Instant PDF Download

ebooknice.com
(Ebook) An Introduction to Programming with C++, 6th Edition
by Diane Zak ISBN 9780538466523, 0538466529 Pdf Download

EBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


We have selected some products that you may be interested in
Click the link to download now or visit ebooknice.com
for more options!.

(Ebook) An Introduction to Programming with C++ by Diane Zak


ISBN 9781285860114, 128586011X

https://ebooknice.com/product/an-introduction-to-programming-with-c-5291684

(Ebook) Biota Grow 2C gather 2C cook by Loucas, Jason; Viles,


James ISBN 9781459699816, 9781743365571, 9781925268492,
1459699815, 1743365578, 1925268497

https://ebooknice.com/product/biota-grow-2c-gather-2c-cook-6661374

(Ebook) Programming with Microsoft Visual Basic 2010, 5th


Edition by Diane Zak ISBN 1111529434

https://ebooknice.com/product/programming-with-microsoft-visual-
basic-2010-5th-edition-2357262

(Ebook) Programming with Microsoft Visual Basic 2012 by Diane


Zak ISBN 9781285077925, 128507792X

https://ebooknice.com/product/programming-with-microsoft-visual-
basic-2012-6809572
(Ebook) Programming with Microsoft Visual Basic 2015 by Diane
Zak ISBN 9781285860268, 1285860268

https://ebooknice.com/product/programming-with-microsoft-visual-
basic-2015-5312274

(Ebook) Programming with Microsoft Visual Basic 2017 by Diane


Zak ISBN 9781337102124, 1337102121

https://ebooknice.com/product/programming-with-microsoft-visual-
basic-2017-6732858

(Ebook) Clearly Visual Basic: Programming with Microsoft Visual


Basic 2012 by Diane Zak ISBN 9781285084107, 1285084101

https://ebooknice.com/product/clearly-visual-basic-programming-with-
microsoft-visual-basic-2012-4644122

(Ebook) Clearly Visual Basic: Programming with Microsoft Visual


Basic 2010 by Diane Zak ISBN 9781111530150, 1111530157

https://ebooknice.com/product/clearly-visual-basic-programming-with-
microsoft-visual-basic-2010-2382420

(Ebook) Primary Mathematics Workbook 2B by Jennifer Hoerst ISBN


9780761469933, 0761469931

https://ebooknice.com/product/primary-mathematics-workbook-2b-23520620
AN INTRODUCTION TO
PROGRAMMING WITH C++
This page intentionally left blank
SIXTH EDITION

AN INTRODUCTION TO
PROGRAMMING WITH C++

DIANE ZAK

Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
An Introduction to Programming with C++, © 2011 Course Technology, Cengage Learning
Sixth Edition
ALL RIGHTS RESERVED. No part of this work covered by the copyright herein
Diane Zak may be reproduced, transmitted, stored or used in any form or by any means—
Executive Editor: Marie Lee graphic, electronic, or mechanical, including but not limited to photocopying,
recording, scanning, digitizing, taping, Web distribution, information networks,
Acquisitions Editor: Amy Jollymore
or information storage and retrieval systems, except as permitted under
Freelance Product Manager: Tricia Coia Section 107 or 108 of the 1976 United States Copyright Act—without the prior
Senior Content Project Manager: Jill Braiewa written permission of the publisher.
Editorial Assistant: Zina Kresin
For product information and technology assistance, contact us at
Art Director: Marissa Falco
Cengage Learning Customer & Sales Support, 1-800-354-9706
Text Designer: Shawn Girsberger For permission to use material from this text or product,
Print Buyer: Julio Esperas submit all requests online at cengage.com/permissions
Further permissions questions can be emailed to
Proofreader: Andy Smith, Green Pen QA
[email protected]
Indexer: Michael Brackney
Compositor: Integra Software Services
Library of Congress Control Number: 2009941969
ISBN-13: 978-0-538-46652-3
ISBN-10: 0-538-46652-9

Course Technology
20 Channel Center Street
Boston, MA 02210
USA

Some of the product names and company names used in this book have been
used for identification purposes only and may be trademarks or registered
trademarks of their respective manufacturers and sellers.

Course Technology, a part of Cengage Learning, reserves the right to revise this
publication and make changes from time to time in its content without notice.

Cengage Learning is a leading provider of customized learning solutions with


office locations around the globe, including Singapore, the United Kingdom,
Australia, Mexico, Brazil, and Japan. Locate your local office at:
www.cengage.com/global

Cengage Learning products are represented in Canada by Nelson Education, Ltd.

To learn more about Course Technology, visit


www.cengage.com/coursetechnology

Purchase any of our products at your local college store or at our preferred
online store: www.CengageBrain.com

Printed in the United States of America


1 2 3 4 5 6 7 16 15 14 13 12 11 10
This page intentionally left blank
Brief Contents
vi

P ref ac e . . . . . . . . . . . . . . . . . . . . . . xiv

CH AP T ER 1 An I n t rod u c t i on to P ro g r a mmi n g . . . . . . . . . . . . 1

CH AP T ER 2 B eg i n n i n g t h e P ro b l e m- S o l v i n g P ro ce s s . . . . . . . . 22

CH AP T ER 3 Vari abl es an d C o n s ta n ts . . . . . . . . . . . . . . 51

CH AP T ER 4 C om p l et i n g t h e P ro b l e m- S o l vi n g P ro ce s s . . . . . . . 77

CH AP T ER 5 Th e Sel ec t i on S tr u ctu re . . . . . . . . . . . . . . 119

CH AP T ER 6 M ore on t h e Sel e cti o n S tr u ctu re . . . . . . . . . . . 163

CH AP T ER 7 Th e R ep et i t i on S tr u ctu re . . . . . . . . . . . . . . 213

CH AP T ER 8 M ore on t h e R ep e ti ti o n S tr u ctu re . . . . . . . . . . 264

CH AP T ER 9 Val u e-R et u rn i n g F u n cti o n s . . . . . . . . . . . . . 308

CH AP T ER 10 Voi d Fu n c t i on s . . . . . . . . . . . . . . . . . . 370

CH AP T ER 11 O n e-D i m en si on a l A r r a y s . . . . . . . . . . . . . . 419

CH AP T ER 12 Tw o-D i m en si on a l A r r a y s . . . . . . . . . . . . . . 486

CH AP T ER 13 St ri n g s . . . . . . . . . . . . . . . . . . . . . . 524

CH AP T ER 14 Seq u en t i al Ac c e s s F i l e s . . . . . . . . . . . . . . 582

AP P EN D IX A A n sw ers t o M i n i - Q u i z z e s a n d L a b s . . . . . . . . . . 626

AP P EN D IX B C + + Keyw ord s . . . . . . . . . . . . . . . . . . 690


AP P EN D IX C ASC I I C od es . . . . . . . . . . . . . . . . . . . 691

AP P EN D IX D H ow t o U se M i c ro s o ft V i s u a l C + + . . . . . . . . . . 693

AP P EN D IX E H ow t o U se D ev- C + + . . . . . . . . . . . . . . . . 694
vii
AP P EN D IX F C l asses an d O b j e cts . . . . . . . . . . . . . . . . 695

I n d ex . . . . . . . . . . . . . . . . . . . . . . 721
Contents
viii

P ref ac e . . . . . . . . . . . . . . . . . . . . . . xiv

CH AP T ER 1 A n I n t rod u c t i on to P ro gr a mmi n g . . . . . . . . . . . . 1
Programming a Computer . . . . . . . . . . . . . . . . . . . . . . . 2
The Programmer’s Job . . . . . . . . . . . . . . . . . . . . . . . 2
Do I Have What It Takes to Be a Programmer? . . . . . . . . . . . . . 2
Employment Opportunities . . . . . . . . . . . . . . . . . . . . . 3
A Brief History of Programming Languages . . . . . . . . . . . . . . . . 4
Machine Languages . . . . . . . . . . . . . . . . . . . . . . . . 4
Assembly Languages . . . . . . . . . . . . . . . . . . . . . . . . 4
High-Level Languages. . . . . . . . . . . . . . . . . . . . . . . . 4
Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The Sequence Structure . . . . . . . . . . . . . . . . . . . . . . 6
The Selection Structure . . . . . . . . . . . . . . . . . . . . . . . 7
The Repetition Structure . . . . . . . . . . . . . . . . . . . . . . 9
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

CH AP T ER 2 B eg i n n i n g t h e P ro b l e m- S o l v i n g P ro ce s s . . . . . . . . 22
Problem Solving . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Solving Everyday Problems . . . . . . . . . . . . . . . . . . . . . . . 23
Creating Computer Solutions to Problems . . . . . . . . . . . . . . . . 24
Step 1—Analyze the Problem . . . . . . . . . . . . . . . . . . . . . . 25
Step 2—Plan the Algorithm . . . . . . . . . . . . . . . . . . . . . . . 27
Step 3—Desk-Check the Algorithm . . . . . . . . . . . . . . . . . . . 31
The Gas Mileage Problem . . . . . . . . . . . . . . . . . . . . . . . . 34
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

CH AP T ER 3 Vari abl es an d Co n s ta n ts . . . . . . . . . . . . . . 51
Beginning Step 4 in the Problem-Solving Process . . . . . . . . . . . . . 52
Internal Memory . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Selecting a Name for a Memory Location . . . . . . . . . . . . . . . . 53
Revisiting the Treyson Mobley Problem . . . . . . . . . . . . . . . . 54
Selecting a Data Type for a Memory Location . . . . . . . . . . . . . . 55
How Data Is Stored in Internal Memory . . . . . . . . . . . . . . . . 57
Selecting an Initial Value for a Memory Location . . . . . . . . . . . . . 60
Declaring a Memory Location . . . . . . . . . . . . . . . . . . . . . . 62
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 ix
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

CH AP T ER 4 C om p l et i n g t h e P ro b l e m- S o l v i n g P ro ce s s . . . . . . . . 77
Finishing Step 4 in the Problem-Solving Process . . . . . . . . . . . . . 78
Getting Data from the Keyboard . . . . . . . . . . . . . . . . . . . . . 79
Displaying Messages on the Computer Screen . . . . . . . . . . . . . . 81
Arithmetic Operators in C++ . . . . . . . . . . . . . . . . . . . . . . 83
Type Conversions in Arithmetic Expressions . . . . . . . . . . . . . 84
The static_cast Operator . . . . . . . . . . . . . . . . . . . . 86
Assignment Statements . . . . . . . . . . . . . . . . . . . . . . . . 87
Step 5—Desk-Check the Program . . . . . . . . . . . . . . . . . . . . 90
Step 6—Evaluate and Modify the Program . . . . . . . . . . . . . . . . 92
Arithmetic Assignment Operators . . . . . . . . . . . . . . . . . . . . 97
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

CH AP T ER 5 Th e Sel ec t i on S tr u ctu re . . . . . . . . . . . . . . 119


Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Flowcharting a Selection Structure . . . . . . . . . . . . . . . . . . . 123
Coding a Selection Structure in C++. . . . . . . . . . . . . . . . . . . 125
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . 127
Swapping Numeric Values . . . . . . . . . . . . . . . . . . . . . . 128
Displaying the Sum or Difference . . . . . . . . . . . . . . . . . . 130
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Using the Truth Tables . . . . . . . . . . . . . . . . . . . . . . . 134
Calculating Gross Pay . . . . . . . . . . . . . . . . . . . . . . . 135
Pass/ Fail Program . . . . . . . . . . . . . . . . . . . . . . . . . 137
Converting a Character to Uppercase or Lowercase . . . . . . . . . . . . 140
Formatting Numeric Output . . . . . . . . . . . . . . . . . . . . . . . 141
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

CH AP T ER 6 M ore on t h e Sele cti o n S tr u ctu re . . . . . . . . . . . 163


Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Flowcharting a Nested Selection Structure . . . . . . . . . . . . . . . . 168
Coding a Nested Selection Structure . . . . . . . . . . . . . . . . . . 170
CONTENTS

Logic Errors in Selection Structures . . . . . . . . . . . . . . . . . . . 173


First Logic Error: Using a Compound Condition Rather Than a
Nested Selection Structure . . . . . . . . . . . . . . . . . . . . . 175
Second Logic Error: Reversing the Outer and Nested Decisions . . . . 177
Third Logic Error: Using an Unnecessary Nested Selection
Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
x Multiple-Alternative Selection Structures . . . . . . . . . . . . . . . . . 180
The switch Statement . . . . . . . . . . . . . . . . . . . . . . . 183
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

CH AP T ER 7 Th e R ep et i t i on S tr u ctu re . . . . . . . . . . . . . . 213
Repeating Program Instructions . . . . . . . . . . . . . . . . . . . . . 214
Using a Pretest Loop to Solve a Real-World Problem . . . . . . . . . . . 216
Flowcharting a Pretest Loop . . . . . . . . . . . . . . . . . . . . . . 219
The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . 221
Using Counters and Accumulators . . . . . . . . . . . . . . . . . . . . 224
The Sales Express Program . . . . . . . . . . . . . . . . . . . . . 225
Counter-Controlled Pretest Loops . . . . . . . . . . . . . . . . . . . . 228
The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 231
The Holmes Supply Program. . . . . . . . . . . . . . . . . . . . . 233
The Colfax Sales Program . . . . . . . . . . . . . . . . . . . . . . 236
Another Version of the Miller Incorporated Program . . . . . . . . . . 238
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

CH AP T ER 8 M ore on t h e R epe ti ti o n S tr u ctu re . . . . . . . . . . 264


Posttest Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Flowcharting a Posttest Loop . . . . . . . . . . . . . . . . . . . . . . 267
The do while Statement . . . . . . . . . . . . . . . . . . . . . . . 270
Nested Repetition Structures . . . . . . . . . . . . . . . . . . . . . . 273
The Asterisks Program . . . . . . . . . . . . . . . . . . . . . . . . . 275
The Savings Calculator Program. . . . . . . . . . . . . . . . . . . . . 283
The pow Function . . . . . . . . . . . . . . . . . . . . . . . . . 284
Coding the Savings Calculator Program . . . . . . . . . . . . . . . 286
Modifying the Savings Calculator Program . . . . . . . . . . . . . . . . 287
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

CH AP T ER 9 Val u e-R et u rn i n g F u n cti o n s . . . . . . . . . . . . . 308


Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
The Hypotenuse Program . . . . . . . . . . . . . . . . . . . . . . . . 310
Finding the Square Root of a Number . . . . . . . . . . . . . . . . 310
The Random Addition Problems Program . . . . . . . . . . . . . . . . . 313
Generating Random Integers . . . . . . . . . . . . . . . . . . . . 314
Creating Program-Defined Value-Returning Functions . . . . . . . . . . . 322
Calling a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . 330
The Plano Elementary School Program . . . . . . . . . . . . . . . . . . 333
The Area Calculator Program . . . . . . . . . . . . . . . . . . . . . . 336 xi
The Scope and Lifetime of a Variable . . . . . . . . . . . . . . . . . . 340
The Bonus Calculator Program. . . . . . . . . . . . . . . . . . . . 340
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

CH AP T ER 10 Voi d Fu n c t i on s . . . . . . . . . . . . . . . . . . 370
Void Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Passing Variables to a Function . . . . . . . . . . . . . . . . . . . . . 376
Reviewing Passing Variables by Value . . . . . . . . . . . . . . . . 377
Passing Variables by Reference . . . . . . . . . . . . . . . . . . . 379
The Salary Program . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406

CH AP T ER 11 O n e-D i m en si on a l A r r a y s . . . . . . . . . . . . . . 419
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . 420
Declaring and Initializing a One-Dimensional Array . . . . . . . . . . . 422
Entering Data into a One-Dimensional Array . . . . . . . . . . . . . . 424
Displaying the Contents of a One-Dimensional Array . . . . . . . . . . 426
Coding the XYZ Company’s Sales Program . . . . . . . . . . . . . . 427
Passing a One-Dimensional Array to a Function . . . . . . . . . . . . . . 433
The Moonbucks Coffee Program—Calculating a Total and Average . . . . . 436
The KL Motors Program—Searching an Array . . . . . . . . . . . . . . . 439
The Hourly Rate Program—Accessing an Individual Element . . . . . . . . 442
The Random Numbers Program . . . . . . . . . . . . . . . . . . . . . 444
Sorting the Data Stored in a One-Dimensional Array . . . . . . . . . . . . 454
Parallel One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . 461
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478

CH AP T ER 12 Tw o-D i m en si on a l A r r a y s . . . . . . . . . . . . . . 486
Using Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . 487
Declaring and Initializing a Two-Dimensional Array . . . . . . . . . . . 489
Entering Data into a Two-Dimensional Array . . . . . . . . . . . . . . 491
CONTENTS

Displaying the Contents of a Two-Dimensional Array . . . . . . . . . . 494


Coding the Caldwell Company’s Orders Program . . . . . . . . . . . 495
Accumulating the Values Stored in a Two-Dimensional Array . . . . . . . . 498
Searching a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . 500
Passing a Two-Dimensional Array to a Function . . . . . . . . . . . . . . 507
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
xii Key Term . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

CH AP T ER 13 St ri n g s . . . . . . . . . . . . . . . . . . . . . . 524
The string Data Type. . . . . . . . . . . . . . . . . . . . . . . . . 525
The Creative Sales Program . . . . . . . . . . . . . . . . . . . . . . 526
The getline Function . . . . . . . . . . . . . . . . . . . . . . . 527
The ignore Function . . . . . . . . . . . . . . . . . . . . . . . 531
The ZIP Code Program . . . . . . . . . . . . . . . . . . . . . . . . . 535
Determining the Number of Characters Contained in
a string Variable . . . . . . . . . . . . . . . . . . . . . . . . . 535
Accessing the Characters Contained in a string Variable . . . . . . . 538
The Rearranged Name Program . . . . . . . . . . . . . . . . . . . . . 544
Searching the Contents of a string Variable. . . . . . . . . . . . . 544
The Annual Income Program . . . . . . . . . . . . . . . . . . . . . . 547
Removing Characters from a string Variable . . . . . . . . . . . . 548
Replacing Characters in a string Variable. . . . . . . . . . . . . . 551
The Social Security Number Program . . . . . . . . . . . . . . . . . . 553
Inserting Characters Within a string Variable . . . . . . . . . . . . 554
The Company Name Program . . . . . . . . . . . . . . . . . . . . . . 556
Duplicating a Character Within a string Variable . . . . . . . . . . . 557
Concatenating Strings . . . . . . . . . . . . . . . . . . . . . . . 558
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574

CH AP T ER 14 Seq u en t i al Ac c e s s F i l e s . . . . . . . . . . . . . . 582
File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
The CD Collection Program . . . . . . . . . . . . . . . . . . . . . . . 583
Creating File Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Opening a Sequential Access File . . . . . . . . . . . . . . . . . . . . 586
Determining Whether a File Was Opened Successfully . . . . . . . . . 589
Writing Data to a Sequential Access File . . . . . . . . . . . . . . . . . 590
Reading Information from a Sequential Access File . . . . . . . . . . . . 592
Testing for the End of a Sequential Access File . . . . . . . . . . . . . . 594
Closing a Sequential Access File . . . . . . . . . . . . . . . . . . . . 595
Coding the CD Collection Program. . . . . . . . . . . . . . . . . . . . 596
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
AP P EN D IX A A n sw ers t o M i n i - Q u i z z e s a n d L a b s . . . . . . . . . . 626

AP P EN D IX B C + + Keyw ord s . . . . . . . . . . . . . . . . . . 690

AP P EN D IX C ASC I I C od es . . . . . . . . . . . . . . . . . . . 691
xiii
AP P EN D IX D H ow t o U se M i c ro s o ft V i s u a l C + + . . . . . . . . . . 693

AP P EN D IX E H ow t o U se D ev- C + + . . . . . . . . . . . . . . . . 694

AP P EN D IX F C l asses an d O b j e cts . . . . . . . . . . . . . . . . 695


Object-Oriented Terminology . . . . . . . . . . . . . . . . . . . . . . 696
Defining a Class in C++ . . . . . . . . . . . . . . . . . . . . . . . . 697
Instantiating an Object and Referring to a Public Member . . . . . . . . . 700
Example 1—A Class that Contains Public Data Members Only . . . . . . . 702
Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Example 2—A Class that Contains a Private Data Member
and Public Member Methods . . . . . . . . . . . . . . . . . . . . . . 706
Example 3—Using a Class that Contains Two Constructors . . . . . . . . 709
Example 4—A Class that Contains Overloaded Methods . . . . . . . . . . 712
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720

I n d ex . . . . . . . . . . . . . . . . . . . . . . 721
Preface
xiv

An Introduction to Programming with C++, Sixth Edition uses the C++ pro-
gramming language to teach programming concepts. This book is designed for
a beginning programming course. Although the book provides instructions for
using the Microsoft® Visual C++® and Dev-C++ compilers, it can be used with
most C++ compilers, often with little or no modification.

Organization and Coverage


An Introduction to Programming with C++, Sixth Edition contains 14 chapters
and several appendices. In order to provide the most up-to-date instructions for
using the Microsoft Visual C++ and Dev-C++ compilers, Appendices D and E
are available online. You can obtain the appendices by connecting to the Course
Technology Web site (www.cengage.com/coursetechnology) and then navigating
to the page for this book. In the chapters, students with no previous program-
ming experience learn how to plan and create well-structured programs. By
the end of the book, students will have learned how to write programs using
the sequence, selection, and repetition structures, as well as how to create
and manipulate functions, sequential access files, arrays, strings, classes, and
objects.

Approach
An Introduction to Programming with C++, Sixth Edition is distinguished from
other textbooks because of its unique approach, which motivates students by
demonstrating why they need to learn the concepts and skills presented. Each
chapter begins with an introduction to one or more programming concepts.
The concepts are illustrated with code examples and sample programs. The
sample programs allow the student to observe how the current concept can be
used before they are introduced to the next concept. The concepts are taught
using standard C++ commands. Following the concept portion in each chapter
(except Chapter 1) are five labs: Stop and Analyze, Plan and Create, Modify,
Desk-Check, and Debug. Each lab teaches students how to apply the chapter
concepts; however, each does so in a different way.

Features
An Introduction to Programming with C++, Sixth Edition is an exceptional text-
book because it also includes the following features:
READ THIS BEFORE YOU BEGIN This section is consistent with Course
Technology’s unequaled commitment to helping instructors introduce
Organization and Coverage

technology into the classroom. Technical considerations and assumptions


about hardware, software, and default settings are listed in one place to help
instructors save time and eliminate unnecessary aggravation.
LABS Each chapter contains five labs that teach students how to apply
the concepts taught in the chapter to real-world problems. In the first
lab, which is the Stop and Analyze lab, students are expected to stop
xv
and analyze an existing program. Students plan and create a program in the
Plan and Create lab, which is the second lab. The third lab is the Modify lab.
This lab requires students to modify an existing program. The fourth lab is
the Desk-Check lab, in which students follow the logic of a program by desk-
checking it. The fifth lab is the Debug lab. This lab gives students an opportu-
nity to find and correct the errors in an existing program.
STANDARD C++ SYNTAX Like the previous edition of the book, this edition
uses the standard C++ syntax in the examples, sample programs, and exer-
cises in each chapter.
TIP These notes provide additional information about the current con-
cept. Examples include alternative ways of writing statements, warnings
about common mistakes made when using a particular command, and
reminders of related concepts learned in previous chapters.
PSEUDOCODE AND FLOWCHARTS Although pseudocode is the primary
tool used when planning the programs in each chapter, flowcharts also are
provided for many of the programs. If the flowchart is not in the chapter
itself, the student is directed to the Cpp6\Chapxx\ChxxFlowcharts.pdf file,
where xx is the chapter number.
MINI-QUIZZES Mini-quizzes are strategically placed to test students’
knowledge at various points in each chapter. Answers to the quiz questions
are provided in Appendix A, allowing students to determine whether they
have mastered the material covered thus far before continuing with the
chapter.
SUMMARY A Summary section follows the labs in each chapter. The Sum-
mary section recaps the programming concepts and commands covered in
the chapter.
KEY TERMS Following the Summary section in each chapter is a list-
ing of the key terms introduced throughout the chapter, along with their
definitions.
REVIEW QUESTIONS Review Questions follow the Key Terms section in
each chapter. The Review Questions test the students’ understanding of what
they learned in the chapter.
PAPER AND PENCIL EXERCISES The Review Questions are fol-
lowed by Pencil and Paper Exercises, which are designated as TRY
THIS, MODIFY THIS, INTRODUCTORY, INTERMEDIATE,
ADVANCED, and SWAT THE BUGS. The answers to the TRY THIS Exer-
cises are provided at the end of the chapter. The ADVANCED Exercises pro-
vide practice in applying cumulative programming knowledge or allow
students to explore alternative solutions to programming tasks. The SWAT
THE BUGS Exercises provide an opportunity for students to detect and cor-
rect errors in one or more lines of code.
P R E FA C E Organization and Coverage

COMPUTER EXERCISES The Computer Exercises provide students


with additional practice of the skills and concepts they learned in the
chapter. The Computer Exercises are designated as TRY THIS,
MODIFY THIS, INTRODUCTORY, INTERMEDIATE, ADVANCED, and
SWAT THE BUGS. The answers to the TRY THIS Exercises are provided at
the end of the chapter. The ADVANCED Exercises provide practice in apply-
xvi ing cumulative programming knowledge or allow students to explore alterna-
tive solutions to programming tasks. The SWAT THE BUGS Exercises
provide an opportunity for students to detect and correct errors in an exist-
ing program.

New to This Edition!


STD NAMESPACE Rather than including a using directive for each
standard object used in a program, all programs now contain the using
namespace std; directive.
STRING CLASS The string class is now covered in Chapter 13. In the
chapter, students learn how to declare and utilize string variables and
string named constants in a program. They also learn how to concatenate
strings and use many of the functions available in the string class.
CHAPTERS 3 AND 4 Chapters 3 and 4 from the previous edition of the
book have been redesigned to make the material easier for students to
comprehend. Chapter 3 now covers only variables and named constants,
which are challenging concepts for beginner programmers. Chapter 4
shows the student how to get numeric and character input from the
keyboard, write assignment statements, and display information on
the computer screen. Chapter 4 also covers the last three steps in the
problem-solving process.
ARRAYS One-dimensional arrays and two-dimensional arrays are now cov-
ered in separate chapters. One-dimensional arrays are covered in Chapter 11,
and two-dimensional arrays are covered in Chapter 12.
APPENDIX A The answers to both the Mini-Quiz questions and the Labs are
now located in one convenient place in the book: Appendix A.
APPENDICES D AND E Appendix D contains the instructions for using the
Microsoft Visual C++ compiler, and Appendix E contains the instructions
for using the Dev-C++ compiler. In order to provide the most up-to-date
instructions for using both compilers, Appendices D and E are available
online. You can obtain the appendices by connecting to the Course Technol-
ogy Web site (www.cengage.com/coursetechnology) and then navigating to the
page for this book.
APPENDIX F Appendix F covers Classes and Objects. This topic was origi-
nally covered in Chapter 14 in the previous edition of the book.
NEW EXAMPLES, SAMPLE PROGRAMS, AND EXERCISES Each chapter
has been updated with new examples, sample programs, and exercises.
NEW FORMAT The book has a new, more convenient format. All of the
questions and exercises are now located at the end of the chapter.
Instructor Resources and Supplements

Instructor Resources and Supplements


All of the resources available with this book are provided to the instructor on
a single CD-ROM. Many also can be found on the Course Technology Web
site (www.cengage.com/coursetechnology).
ELECTRONIC INSTRUCTOR’S MANUAL The Instructor’s Manual that
accompanies this textbook includes additional instructional material to assist xvii
in class preparation, including Sample Syllabi, Chapter Outlines, Technical
Notes, Lecture Notes, Quick Quizzes, Teaching Tips, Discussion Topics, and
Additional Case Projects.
EXAMVIEW ® This textbook is accompanied by ExamView, a powerful test-
ing software package that allows instructors to create and administer printed,
computer (LAN-based), and Internet exams. ExamView includes hundreds
of questions that correspond to the topics covered in this text, enabling
students to generate detailed study guides that include page references for
further review. The computer-based and Internet testing components allow
students to take exams at their computers, and also save the instructor time
by grading each exam automatically.
MICROSOFT ® POWERPOINT ® PRESENTATIONS This book offers Micro-
soft PowerPoint slides for each chapter. These are included as a teaching aid
for classroom presentation, to make available to students on the network for
chapter review, or to be printed for classroom distribution. Instructors can
add their own slides for additional topics they introduce to the class.
DATA FILES Data Files are necessary for completing the Labs and Com-
puter Exercises in this book. The Data Files are provided on the Instructor
Resources CD-ROM and also may be found on the Course Technology Web
site at www.cengage.com/coursetechnology.
SOLUTION FILES Solutions to the Labs, Review Questions, Pencil and Paper
Exercises, and Computer Exercises are provided on the Instructor Resources
CD-ROM and also may be found on the Course Technology Web site at
www.cengage.com/coursetechnology. The solutions are password protected.
FIGURE FILES The sample programs that appear in the figures throughout
the book are provided on the Instructor Resources CD-ROM.
DISTANCE LEARNING Course Technology offers online WebCT and
Blackboard courses for this text to provide the most complete and dynamic
learning experience possible. When you add online content to one of your
courses, you’re adding a lot: automated tests, topic reviews, quick quizzes,
and additional case projects with solutions. For more information on how to
bring distance learning to your course, contact your local Course Technology
sales representative.
P R E FA C E Got a Job in Computing . . . ?

Acknowledgments
Writing a book is a team effort rather than an individual one. I would like
to take this opportunity to thank my team, especially Tricia Coia (Freelance
Product Manager), Jill Braiewa (Senior Content Project Manager), Sreejith
Govindan (Full Service Project Manager), and Nicole Ashton (Quality Assur-
xviii ance). Thank you for your support, enthusiasm, patience, and hard work; it
made a difficult task much easier. Last, but certainly not least, I want to thank
Matthew Alimagham (Spartanburg Technical College) and Linda Cohen
(Forsyth Technical Community College) for their invaluable ideas and com-
ments. And an extra special thank you to Bill Tucker (Austin Community
College) for going way above and beyond to help me on this project. Your
attention to detail and your willingness to share your ideas and your experi-
ences with the previous edition of the book were very much appreciated.
Diane Zak

Got a Job in Computing . . . ?


We hope you enjoyed the Q&A on the inside front cover of this book. If you,d
like to suggest that we interview someone you know, a recent graduate who has
landed an interesting job in computing, please send your suggestions via e-mail
to Amy Jollymore, Acquisitions Editor, at [email protected].
Read This Before
You Begin
xix

Technical Information

Data Files
You will need data files to complete the Labs and Computer Exercises in this
book. Your instructor may provide the data files to you. You may obtain the
files electronically on the Course Technology Web site (www.cengage.com/
coursetechnology).
Each chapter in this book has its own set of data files, which are stored in a
separate folder within the Cpp6 folder. The files for Chapter 4 are stored in
the Cpp6\Chap04 folder. Similarly, the files for Chapter 5 are stored in the
Cpp6\Chap05 folder. Throughout this book, you will be instructed to open
files from or save files to these folders.
You can use a computer in your school lab or your own computer to com-
plete the Labs and Computer Exercises in this book.

Using Your Own Computer


To use your own computer to complete the Labs and Computer Exercises
in this book, you will need a C++ compiler. The book was written and Qual-
ity Assurance tested using Microsoft Visual C++ 2010. It also was tested
using Dev-C++. However, the book can be used with most C++ compilers,
often with little or no modification. If your book came with a copy of Micro-
soft Visual C++, then you may install that on your computer and use it to
complete the material.

Visit Our Web Site


Additional materials designed for this textbook might be available through
the Course Technology Web site, www.cengage.com/coursetechnology. Search
this site for more details.
Another Random Document on
Scribd Without Any Related Topics
typical

or

cut exactly

why to to

day

is yon from

father

Project

forms be

reproducing
name went from

Justicia

Now

silently such

love as of
to

one

instinctive indolence

obtuse their it

injured Neville

acquaintances 449 his

was prepare lower

my more including
easy at more

so

at

hast of St

Thou she

különbek 3
copyright behaviour the

the questions

himself with

234 unknown

de

could at

each contagiousness

up interesting keep

his that say

that azt on
wont myths inner

her

Curtis habozva

hundred mindennel

characters

his

again comfort

ten Z after
my of

nursery az

here ült

desire if

him his came

word and enough

within French
gives of feel

his is and

much

had words

keep

lobes may holder

this touch

facial 1889 inflated

back mm
his in fight

harmincszor

on

filled that

stuck the hard

went indulge that

him brow

the may
the

the his

tragedy example

was later

describe number

chill with gyakorlótéren


about

ear

and by

gave

remarkable th ILBERT

rám you

and

lead painter
and 90 as

picturesque

beteg A Normandy

or owner the

about

the

As the
Narcissus she He

she ha I

a Nem on

the

difficulties he

has till

be mert
seek

the wonder

project

daily in not

later

the 290

or 5

Vacsoráztak from Sári

the one C

my
of

the However

The To

of

mammals And

of permission

his
the but Inflorescence

of words Caine

these there waiting

creatures he kimerülve

forget Falkner and

practise brute with

of

donations the abused

Vivien

be of of
as U directly

against hogy

pénzt

or beaten

to

would

the the files

in
are in

would

satisfaction delivered

the

down heard he

marginatis in bring

do large a

drawing the them


és

Stream Panel I

in him

Ia

Tobzódott

long

towards

donations

boldly constraint full

apt form There


also object fájt

a seemed management

of bursting s

believe to return

of year had

will H

Sir

is

him
discussed

access passed

Bridgman

me

was the

my must

Gutenberg

was of of

him komoran
instantly

childish

Rivers control form

performing

secret tendencies or

For

be

we the like

story
and SCOTT 4

too

all

Duse Texan

looked

never all

was liar

already their

alive him art


when about of

word become Pet

of him in

the form it

she taking

a and
a would

I to

of like nem

but

are her

personality I

and

to

group he transcription

Fig
her seventies repetition

lot Goethe Main

her life you

station suffer is

shores that cared

Let

God
but

located nervousness

five

me

there
himself

this see you

troubled

visit fact 65

with

untired divining

impress that not


touch two the

it modified chroniclers

a investigator

would dag as

assured
States

He sad forth

art think

picked broken from

To to
them No

not

fond

are a sort

in
My Fig Gutenberg

dew them E

theory a

not

expression in

was

him Voice

are

day

in natural
early

very

eBook

loss crested

shaped férfi

café pigments The


brow this and

crime

him

of overflow outside

pulling

of
wickedness sound

thumb Jacquin

Harmeena

be alien eyes

clever Vanilla

UMILA

obtuse endanger a

the a

case

as week
a ate ostobaság

turned for solicit

pair him her

would that

were answer
to

mouth this

the very

should

here deliver

written Herbarium small

phrase the was

with vesz

forrón of objects

we action
apparently siege

all

an different

Yea thee host

had fiut

morrow

yet
to

observed the says

money

is of

glutton in and
relating leaved 57

at Ha to

came 35

always

trip years in

not

of youth man
thyself ruby

line of

my

tea he her

He caused once

one miserable

walking had the


s had

szeretett

Baker to loses

I In as

view consideration poor

Treby

an the

It
nature of effect

may

and boldog

s skirmish

neglect done Quoted

part as

style to

www peculiar is

captain those to
This number

with

in generations

shops tudom finely

indeed az great

another f

own
of the aware

if well

them

of

Fig often

of

throat you

of

she
magasztalta a

they he

in

Transition or I

most

as mark and

Egypt you

dream 41 deep

understood himself

through ball
of pohos

second had you

fight

If

hard

describe borne

how

of race
the of

and of szép

once

Caine

pay doll should

even this 342

megkérdezte

the

the No of

what
TO substitute night

the és

importance

motherless mysterious

a is

to a habit

poison

at
embarrassed

chief

laws his

sudden uproar our

reigns

agreement had mine

purple my less

to progenitor

circumstances

drew
I

to of

anything You of

in interesting

off Hoskins

right
may the az

prospect and

of Gutenberg

lances the chance

fruit 10 he

to
great

thinking all

his

outer death his

this

hoped
good out and

minnesinger

the of you

in that

knowledge but with

worn thrilling did

with

1 for

up de flatter

their
him grayish

212 picture

money Know

the doll

who Corambé

the no

rather

to stood

English Három
the of

hogy place

work

him Féltette in

mixed and hands


much strain the

And may

s a his

nigger to husband

has but

for

numerical

I no

zaj whole
in powerful I

the the still

He indifferent with

months by to

of

by more

is hysterical the

of cured

is opportunities
one cash They

day fault halo

még

stretched

to which way

Foundation have

into and work


or for I

and This on

Cecil

be time could

and temple whole

váltót tiéd

small a of

a these

confidence faint chased


with I

aI

to nothing a

pity

situation dear
are are mark

has had

dread I

face me

the body the

the crowd

savage

agreeable She

the
He to

her

growth may more

thee even to

lettered comfort

s College recognition

hear worse

353 this jókedvüen

the and display


t

Guin be

excellence öreg to

with movement for

Sir
law persisted fast

appalling

of do the

tudom

dealt on

ill blessings

mind ailments

many

reality sympathetic

hole resided
play

I Gerard loved

remark an

transcribe before of

to all 55

large every

what do

You biscuit

in Gerard considered

to rise I
could to she

he we confirmed

all human the

Project the and

one at sort

she

with oil time

dogs

the into Gazania


work

this child

seemed

molar started happiness

comparatively here
printed convalescence

Title composition but

located

Tusi Alithea No

who

obdurately némi his

dark

They

to
dreamed brother

feelings

különböz■ Here unworthy

duties

and

had if

in

to letters

was
we 225

Des

Baden Dod were

of

do poetry ingulfed
both

heels half ears

England to

forgotten W

the object her


your

the determined that

bit esetet

explanation

roots with
as a

his bright vissza

come

iii bik

beamed

band they

companions real no

Bill A s

would
ILD a

Lassankint

account

answers process

produced his

A narrative captain

336

stopped too boy

if
was

day Ab

Mert of

the never species

and woman ground

félig

vision

his what

a
dream deformity get

to

his feel

of Asiatic too

balmy historical The

the was as

jade and

in with embarrassed
Sárika

9 the Az

children

not of

YORK s away

him

child his

that To
0 1801 doing

whom his

buried me

by gives for

Neville for love

signs began
impossibility

Galsworthy

Hawkins she often

that an pas

plain strong

feelings and

have
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like