AN INTRODUCTION TO PROGRAMMING WITH C++ EIGHTH EDITION A N I N T R O D U C T I O N T O P R O G R A M M I N G W I T H C + + DIANE ZAK Australia • Brazil • Mexico • Singapore • United Kingdom • United States An Introduction to Programming with C++, © 2016 Cengage Learning Eighth Edition WCN: 02-200-203 Diane Zak ALL RIGHTS RESERVED. No part of this work covered by the copyright Product Director: Kathleen McMahon herein may be reproduced, transmitted, stored or used in any form or by Product Team Manager: Kristin McNary any means graphic, electronic, or mechanical, including but not limited to Senior Product Manager: Jim Gish photocopying, recording, scanning, digitizing, taping, Web distribution, Senior Content Developer: Alyssa Pratt information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Product Assistant: Abigail Pufpaff Act, without the prior written permission of the publisher. Marketing Manager: Eric LaScola Senior Production Director: For product information and technology assistance, contact us at Wendy Troeger Cengage Learning Customer & Sales Support, 1-800-354-9706 Production Director: Patty Stephan For permission to use material from this text or product, submit all Senior Content Project Manager: requests online at www.cengage.com/permissions Jennifer K. Feltri-George Further permissions questions can be emailed to [email protected] Managing Art Director: Jack Pendleton Cover image(s): Library of Congress Control Number: 2015940474 © Rudchenko Liliia/Shutterstock.com ISBN: (cid:412)(cid:410)(cid:411)-(cid:404)-(cid:405)(cid:411)(cid:408)-(cid:411)(cid:409)(cid:403)(cid:404)(cid:404)-(cid:407) Unless otherwise noted all screenshots are Cengage Learning courtesy of Microsoft Corporation 20 Channel Center Street Open Clip art source: OpenClipArt Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with employees residing in nearly 40 different countries and sales in more than 125 countries around the world. Find your local representative at www.cengage.com Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your course and learning solutions, visit www.cengage.com Purchase any of our products at your local college store or at our preferred online store www.cengagebrain.com Notice to the Reader Publisher does not warrant or guarantee any of the products described herein or perform any independent analysis in connection with any of the product informa- tion contained herein. Publisher does not assume, and expressly disclaims, any obligation to obtain and include information other than that provided to it by the manufacturer. The reader is expressly warned to consider and adopt all safety precautions that might be indicated by the activities described herein and to avoid all potential hazards. By following the instructions contained herein, the reader willingly assumes all risks in connection with such instructions. The publisher makes no representations or warranties of any kind, including but not limited to, the warranties of fitness for particular purpose or merchantability, nor are any such representations implied with respect to the material set forth herein, and the publisher takes no responsibility with respect to such material. The publisher shall not be liable for any special, consequential, or exemplary damages resulting, in whole or part, from the readers’ use of, or reliance upon, this material. Printed in the United States of America Print Number: 01 Print Year: 2016 v Brief Contents Preface xiv Read This Before You Begin xviii ChaptEr 1 an introduction to Programming 1 ChaptEr 2 Beginning the Problem-Solving Process 23 ChaptEr 3 Variables and Constants 51 ChaptEr 4 Completing the Problem-Solving Process 75 ChaptEr 5 The Selection Structure 113 ChaptEr 6 More on the Selection Structure 157 ChaptEr 7 The Repetition Structure 201 ChaptEr 8 More on the Repetition Structure 247 ChaptEr 9 Value-Returning Functions 279 ChaptEr 10 Void Functions 329 ChaptEr 11 One-Dimensional arrays 369 ChaptEr 12 Two-Dimensional arrays 425 ChaptEr 13 Strings 461 ChaptEr 14 Sequential access Files 511 ChaptEr 15 Classes and Objects 551 appEndix a C++ keywords 593 appEndix B aSCii Codes 595 appEndix C Common Syntax errors 597 appEndix d How To Boxes 599 index 603 The answers pdf and data files can be found online at CengageBrain com vi Contents Preface xiv Read This Before You Begin xviii ChaptEr 1 an introduction to Programming 1 Programming a Computer 2 The Programmer’s Job 2 employment Opportunities 2 a Brief History of Programming Languages 3 Machine Languages 3 assembly Languages 3 High-Level Languages 4 Control Structures 5 The Sequence Structure 5 The Selection Structure 6 The Repetition Structure 8 LaB 1-1 Stop and analyze 10 LaB 1-2 Plan and Create 10 LaB 1-3 Modify 11 LaB 1-4 What’s Missing? 11 Chapter Summary 12 key Terms 12 Review Questions 13 exercises 14 ChaptEr 2 Beginning the Problem-Solving Process 23 Problem Solving 24 Solving everyday Problems 24 Creating Computer Solutions to Problems 25 Step 1—analyze the Problem 26 Hints for analyzing Problems 26 Step 2—Plan the algorithm 28 Step 3—Desk-Check the algorithm 31 The Gas Mileage Problem 34 LaB 2-1 Stop and analyze 36 LaB 2-2 Plan and Create 36 LaB 2-3 Modify 39 LaB 2-4 What’s Missing? 39 LaB 2-5 Desk-Check 40 LaB 2-6 Debug 40 vii Chapter Summary 42 key Terms 43 Review Questions 43 exercises 45 ChaptEr 3 Variables and Constants 51 Beginning Step 4 in the Problem-Solving Process 52 internal Memory 52 Selecting a name for a Memory Location 53 Revisiting the addison O’Reilly Problem from Chapter 2 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 LaB 3-1 Stop and analyze 64 LaB 3-2 Plan and Create 65 LaB 3-3 Modify 67 LaB 3-4 What’s Missing? 67 LaB 3-5 Desk-Check 67 LaB 3-6 Debug 68 Chapter Summary 68 key Terms 69 Review Questions 70 exercises 71 ChaptEr 4 Completing the Problem-Solving Process 75 Finishing Step 4 in the Problem-Solving Process 76 Getting Data from the keyboard 76 Displaying Messages on the Computer Screen 78 arithmetic Operators in C++ 81 Type Conversions in arithmetic expressions 82 The static_cast Operator 83 assignment Statements 84 arithmetic assignment Operators 87 Step 5—Desk-Check the Program 88 Step 6—evaluate and Modify the Program 90 LaB 4-1 Stop and analyze 94 LaB 4-2 Plan and Create 95 LaB 4-3 Modify 98 LaB 4-4 What’s Missing? 98 LaB 4-5 Desk-Check 99 LaB 4-6 Debug 99 Chapter Summary 99 key Terms 101 Review Questions 102 exercises 104 viii ContEnts ChaptEr 5 The Selection Structure 113 Making Decisions 114 Flowcharting a Selection Structure 116 Coding Selection Structures in C++ 118 Comparison Operators 120 Swapping numeric Values 121 Displaying the area or Circumference 124 Logical Operators 126 Using the Truth Tables 129 a Different Version of the area or Circumference Program 130 Summary of Operators 132 Converting a Character to Uppercase or Lowercase 133 Formatting numeric Output 134 LaB 5-1 Stop and analyze 137 LaB 5-2 Plan and Create 138 LaB 5-3 Modify 143 LaB 5-4 What’s Missing? 143 LaB 5-5 Desk-Check 143 LaB 5-6 Debug 144 Chapter Summary 144 key Terms 145 Review Questions 146 exercises 147 ChaptEr 6 More on the Selection Structure 157 nested Selection Structures 158 Flowcharting a nested Selection Structure 161 Coding a nested Selection Structure 163 Logic errors in Selection Structures 165 First Logic error: Using a Compound Condition Rather Than a nested Selection Structure 167 Second Logic error: Reversing the Outer and nested Decisions 169 Third Logic error: Using an Unnecessary nested Selection Structure 169 Fourth Logic error: including an Unnecessary Comparison in a Condition 170 Multiple-alternative Selection Structures 173 The switch Statement 174 LaB 6-1 Stop and analyze 177 LaB 6-2 Plan and Create 178 LaB 6-3 Modify 182 LaB 6-4 What’s Missing? 182 LaB 6-5 Desk-Check 183 LaB 6-6 Debug 183 Chapter Summary 184 key Terms 184 Review Questions 185 exercises 187 ix ChaptEr 7 The Repetition Structure 201 Repeating Program instructions 202 Using a Pretest Loop to Solve a Real-World Problem 204 Flowcharting a Pretest Loop 206 The while Statement 208 Using Counters and accumulators 211 The Stock Price Program 213 Counter-Controlled Pretest Loops 217 The for Statement 219 The Total Payroll Program 220 The Tip Program 223 another Version of the Commission Program 224 The even integers Program 226 LaB 7-1 Stop and analyze 229 LaB 7-2 Plan and Create 230 LaB 7-3 Modify 234 LaB 7-4 What’s Missing? 234 LaB 7-5 Desk-Check 234 LaB 7-6 Debug 234 Chapter Summary 235 key Terms 236 Review Questions 236 exercises 239 ChaptEr 8 More on the Repetition Structure 247 Posttest Loops 248 Flowcharting a Posttest Loop 250 The do while Statement 252 nested Repetition Structures 254 The Clock Program 255 The Car Depreciation Program 258 LaB 8-1 Stop and analyze 262 LaB 8-2 Plan and Create 263 LaB 8-3 Modify 267 LaB 8-4 What’s Missing? 267 LaB 8-5 Desk-Check 267 LaB 8-6 Debug 268 Chapter Summary 268 key Terms 268 Review Questions 269 exercises 270 ChaptEr 9 Value-Returning Functions 279 Functions 280 Value-Returning Functions 281 The pow Function 281