An Introduction to Programming with C 8th Edition Diane Zak pdf download
An Introduction to Programming with C 8th Edition Diane Zak pdf download
https://ebookname.com/product/an-introduction-to-programming-
with-c-8th-edition-diane-zak/
https://ebookname.com/product/clearly-visual-basic-programming-
with-microsoft-visual-basic-2012-3rd-edition-diane-zak/
https://ebookname.com/product/cgi-perl-web-warrior-series-1st-
edition-diane-zak/
https://ebookname.com/product/microsoft-visual-c-2010-an-
introduction-to-object-oriented-programming-4th-edition-joyce-
farrell/
https://ebookname.com/product/the-protection-of-archaeological-
heritage-in-times-of-economic-crisis-1st-edition-elena-korka-ed/
Policy Concertation and Social Partnership in Western
Europe Lessons for the Twenty First Century 1st Edition
Stefan Berger
https://ebookname.com/product/policy-concertation-and-social-
partnership-in-western-europe-lessons-for-the-twenty-first-
century-1st-edition-stefan-berger/
https://ebookname.com/product/lectures-on-quantum-groups-2nd-
edition-pavel-etingof/
https://ebookname.com/product/functional-food-ingredients-and-
nutraceuticals-processing-technologies-1st-edition-john-shi/
https://ebookname.com/product/the-i-l-peretz-reader-peretz/
https://ebookname.com/product/the-most-controversial-decision-
truman-the-atomic-bombs-and-the-defeat-of-japan-1st-edition-
wilson-d-miscamble-c-s-c/
Gale Encyclopedia of Science 3rd Edition K. Lee Lerner
And Brenda Wilmoth Lerner
https://ebookname.com/product/gale-encyclopedia-of-science-3rd-
edition-k-lee-lerner-and-brenda-wilmoth-lerner/
An Introduction to
Programming with C++
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Eighth Edition
An Introduction to
Programming with C++
Diane Zak
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
This is an electronic version of the print textbook. Due to electronic rights restrictions,
some third party content may be suppressed. Editorial review has deemed that any suppressed
content does not materially affect the overall learning experience. The publisher reserves the right
to remove content from this title at any time if subsequent rights restrictions require it. For
valuable information on pricing, previous editions, changes to current editions, and alternate
formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for
materials in your areas of interest.
Important Notice: Media content referenced within the product description or the product
text may not be available in the eBook version.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
An Introduction to Programming with C++, © 2016 Cengage Learning
Eighth Edition
WCN: 02-200-203
Diane Zak
Product Director: Kathleen McMahon ALL RIGHTS RESERVED. No part of this work covered by the copyright
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
permissionrequest@cengage.com
Managing Art Director: Jack Pendleton
Cover image(s):
Library of Congress Control Number: 2015940474
© Rudchenko Liliia/Shutterstock.com
ISBN: 978-1-285-86011-4
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
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
v
Brief Contents
Pref ace . . . . . . . . . . . . . . . . . . . . . . . . . xi v
Read T h is B ef o re You Begi n . . . . . . . . . . . . . . . xv i i i
Chapt e r 1 A n I n t ro du ct io n to Programmi ng . . . . . . . . . . . . . . 1
Chapt e r 2 B eg in n in g t h e Probl em- Sol v i ng Process . . . . . . . . . . . 23
Chapt e r 3 Var iables an d Constants . . . . . . . . . . . . . . . . . 51
Chapt e r 4 Co m plet in g t h e Probl em- Sol v i ng Process . . . . . . . . . . 75
Chapt e r 5 T h e Select io n Structure . . . . . . . . . . . . . . . . . 113
Chapt e r 6 M o re o n t h e Selecti on Structure . . . . . . . . . . . . . 157
Chapt e r 7 T h e Repet it io n Structure . . . . . . . . . . . . . . . . 201
C HAPT E R 8 M o re o n t h e Repeti ti on Structure . . . . . . . . . . . . . 247
C HAPT E R 9 Valu e-Ret u r n in g Functi ons . . . . . . . . . . . . . . . . 279
C HAPT E R 10 Vo id F u n ct io n s . . . . . . . . . . . . . . . . . . . . . 329
C HAPT E R 11 On e-Dim en s io n a l Array s . . . . . . . . . . . . . . . . . 369
C HAPT E R 12 Tw o -Dim en s io n a l Array s . . . . . . . . . . . . . . . . . 425
C HAPT E R 13 St r in g s . . . . . . . . . . . . . . . . . . . . . . . . 461
C HAPT E R 14 Sequ en t ial A ccess Fi l es . . . . . . . . . . . . . . . . . 511
C HAPT E R 15 Clas s es an d Obj ects . . . . . . . . . . . . . . . . . . 551
A pp endix A C+ + Keyw o rds . . . . . . . . . . . . . . . . . . . . . 593
A pp endix B A SC II Co des . . . . . . . . . . . . . . . . . . . . . . 595
A pp endix C Co m m o n Syn t ax Errors . . . . . . . . . . . . . . . . . 597
A pp endix D Ho w To B o xes . . . . . . . . . . . . . . . . . . . . . 599
I n dex . . . . . . . . . . . . . . . . . . . . . . . . . 603
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
vi
Contents
Pref ace . . . . . . . . . . . . . . . . . . . . . . . . . xi v
R ead T h is B ef o re Yo u Begi n . . . . . . . . . . . . . . . xv i i i
Chapt e r 1 A n I n t ro du ct io n t o Programmi ng . . . . . . . . . . . . . . 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
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
vii
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
viii
Contents
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
ix
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
x
Contents
Chapt er 10 Vo id F u n ct io n s . . . . . . . . . . . . . . . . . . . . . 329
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Creating Program-Defined Void Functions . . . . . . . . . . . . . . . . . . . . 331
Passing Variables to a Function . . . . . . . . . . . . . . . . . . . . . . . . 337
Reviewing Passing Variables by Value . . . . . . . . . . . . . . . . . . . . . 338
Passing Variables by Reference . . . . . . . . . . . . . . . . . . . . . . . 341
LAB 10-1 Stop and Analyze . . . . . . . . . . . . . . . . . . . . . . . . . 345
LAB 10-2 Plan and Create . . . . . . . . . . . . . . . . . . . . . . . . . . 347
LAB 10-3 Modify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
LAB 10-4 What’s Missing? . . . . . . . . . . . . . . . . . . . . . . . . . . 356
LAB 10-5 Desk-Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
LAB 10-6 Debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xi
Chapter 13 St r in g s . . . . . . . . . . . . . . . . . . . . . . . . 461
The string Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Getting String Input from the Keyboard . . . . . . . . . . . . . . . . . . . . . 462
The Primrose Auction House Program . . . . . . . . . . . . . . . . . . . . . 464
The ignore Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Determining the Number of Characters in a string Variable . . . . . . . . . . . 471
Accessing the Characters in a string Variable . . . . . . . . . . . . . . . . . 473
Searching the Contents of a string Variable . . . . . . . . . . . . . . . . . . 477
Removing Characters from a string Variable . . . . . . . . . . . . . . . . . . 481
Replacing Characters in a string Variable . . . . . . . . . . . . . . . . . . . 484
Inserting Characters Within a string Variable . . . . . . . . . . . . . . . . . 485
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xii
Contents
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xiii
In dex . . . . . . . . . . . . . . . . . . . . . . . . . 603
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xiv
Preface
An Introduction to Programming with C++, Eighth Edition uses the C++ programming language
to teach programming concepts. This book is designed for a beginning programming course.
Although the book provides instructions for using several specific C++ compilers (such as
® ®
Microsoft Visual C++ , Dev-C++, and Code::Blocks), it can be used with most C++ compilers,
often with little or no modification.
Approach
An Introduction to Programming with C++, Eighth 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 six labs: Stop and Analyze,
Plan and Create, Modify, What’s Missing?, 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++, Eighth Edition is an exceptional textbook because it
also includes the following features:
READ THIS BEFORE YOU BEGIN This section is consistent with Cengage Learning’s
unequaled commitment to helping instructors introduce 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 (except Chapter 1) contains six 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 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
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xv
Organization and Coverage
modify an existing program. In the fourth lab, which is the new What’s Missing? lab, students are
asked to find one or more missing instructions in a program. However, before they can
accomplish this task, they must put the existing instructions in the proper order. The fifth lab is
the Desk-Check lab, in which students follow the logic of a program by desk-checking it. The
sixth lab is the Debug lab. This lab gives students an opportunity to find and correct the errors in
an existing program. Answers to the labs are provided in the Answers.pdf file available at
CengageBrain.com. Providing the answers allows students to determine whether they have
mastered the material covered in the chapter.
HOW TO BOXES The How To boxes in each chapter summarize important concepts and
rovide a quick reference for students. The How To boxes that introduce new statements,
p
operators, stream manipulators, or functions contain the syntax and examples of using the syntax.
STANDARD C++ SYNTAX Like the previous edition of the book, this edition uses the standard
C++ syntax in the examples, sample programs, and exercises in each chapter.
PSEUDOCODE AND FLOWCHARTS Both planning tools are shown for many of the programs
within the chapters.
TIP These notes provide additional information about the current concept. 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.
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 the Answers.pdf file,
allowing students to determine whether they have mastered the material covered thus far before
continuing with the chapter.
WANT MORE INFO? FILES These notes direct students to files that accompany
each chapter in the book. The files contain additional examples and further
explanations of the concepts covered in the chapter. The files are in PDF format and
are available online at CengageBrain.com. Search for the ISBN associated with your
book (from the back cover of your book) using the search box at the top of the page.
This will take you to the product page where free companion resources can be found.
SUMMARY A Summary section follows the labs in each chapter. The Summary section recaps
the programming concepts and commands covered in the chapter.
KEY TERMS Following the Summary section in each chapter is a listing 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 followed 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 Exercises are provided at the end of the chapter. The
ADVANCED Exercises provide practice in applying cumulative programming k nowledge or
allow students to explore alternative solutions to programming tasks. The SWAT THE BUGS
Exercises provide an opportunity for students to detect and correct errors in one or more lines
of code.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xvi
Contents Organization and Coverage
VIDEOS These notes direct students to videos that accompany each chapter in the
book. Many of the videos have been revised from the previous edition. The videos
explain and/or demonstrate one or more of the chapter’s concepts. The videos are
available online at CengageBrain.com. Search for the ISBN associated with your book (from the
back cover of your book) using the search box at the top of the page. This will take you to the
product page where free companion resources can be found.
INSTALLATION VIDEOS These videos, which have been revised from the previous
edition, show students how to install various C++ compilers (such as Microsoft Visual
C++, Dev-C++, and Code::Blocks). The videos are named Ch04-Installation
developmentTool, where developmentTool is the name of the C++ development tool covered in
the video. The videos are available online at CengageBrain.com. Search for the ISBN associated
with your book (from the back cover of your book) using the search box at the top of the page.
This will take you to the product page where free companion resources can be found.
STEP-BY-STEP INSTRUCTIONS This book is accompanied by files that contain step-by-step
instructions for completing Labs 4-2, 4-3, 4-4, 4-6, 5-2, 5-3, and 5-6 using various
C++ compilers. The files, which have been revised from the previous edition, are named
Ch04-Lab4-X developmentTool.pdf and Ch05-Lab5-X developmentTool.pdf, where X represents
the lab number, and developmentTool is the name of the C++ development tool covered in the
file. The files are in PDF format and are available online at www.cengagebrain.com. Search for
the ISBN associated with your book (from the back cover of your book) using the search box at
the top of the page. This will take you to the product page where free companion resources can
be found.
APPENDICES Appendices B, C, D, and E are now Appendices A, B, C, and D. The information
in Appendix A from the previous edition is now contained in the Answers.pdf file.
POW FUNCTION The pow function is now covered along with the built-in value-returning
functions in Chapter 9 (rather than in Chapter 8).
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xvii
Acknowledgments
Instructor Resources
The following resources are available on the Instructor Companion Site (sso.cengage.com) to
instructors who have adopted this book. Search for this title by ISBN, title, author, or keyword.
From the Product Overview page, select the Instructor’s Companion Site link to access your
complementary resources.
INSTRUCTOR’S MANUAL The Instructor’s Manual follows the text chapter by chapter to assist
you in planning and organizing an effective, engaging course. The manual includes learning
objectives, chapter overviews, ideas for classroom activities, and additional resources. A sample
course Syllabus is also available.
TEST BANK Cengage Learning Testing Powered by Cognero is a flexible, online system that
allows you to:
•• author, edit, and manage test bank content from multiple Cengage Learning solutions
•• create multiple test versions in an instant
•• deliver tests from your LMS, your classroom or wherever you want
POWERPOINT PRESENTATIONS This book comes with Microsoft 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 are encouraged to customize the slides to fit their course needs, and may add slides
to cover additional topics using the complete Figure Files from the text, also available on the
Instructor Companion Site.
SOLUTION FILES Solutions to the Labs, Review Questions, Pencil and Paper Exercises, and
Computer Exercises are available. The Solution Files also contain the sample programs that
appear in the figures throughout the book.
DATA FILES Data Files are required to complete many Labs and Computer Exercises in this
book. They are available on the Instructor Companion Site as well as on CengageBrain.com.
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 Alyssa Pratt (Senior Content Developer), Jennifer
K. Feltri-George (Senior Content Project Manager), Marisa Taylor (Senior Project Manager),
and Nicole Ashton, Serge Palladino, Chris Scriver (Quality Assurance). 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 Fred D’Angelo, Pima Community College East Campus;
Charles Nelson, Rock Valley College; and Mark Shellman, Gaston College for their invaluable
ideas and comments.
Diane Zak
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xviii
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 at
CengageBrain.com. Search for the ISBN associated with your book (from the back cover of your
text) using the search box at the top of the page. This will take you to the product page where
free companion resources can be found.
Each chapter in this book has its own set of data files, which are stored in a separate folder
within the Cpp8 folder. The files for Chapter 4 are stored in the Cpp8\Chap04 folder. Similarly,
the files for Chapter 5 are stored in the Cpp8\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 complete the Labs and
Computer Exercises in this book.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Chapter 1
An Introduction to
Programming
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
Chapter 1 An Introduction to Programming
Programming a Computer
In essence, the word programming means giving a mechanism the directions to accomplish a
task. If you are like most people, you’ve already programmed several mechanisms, such as your
digital video recorder (DVR), cell phone, or coffee maker. Like these devices, a computer also is a
mechanism that can be programmed.
The directions (typically called instructions) given to a computer are called computer programs
or, more simply, programs. The people who write programs are called p rogrammers. Program-
mers use a variety of special languages, called programming languages, to communicate with the
computer. Some popular programming languages are C++, Visual Basic, C#, Java, and Python. In
this book, you will use the C++ programming language.
Employment Opportunities
When searching for a job in computer programming, you will encounter ads for “computer
programmers” as well as for “computer software engineers.” Although job titles and descriptions
vary, computer software engineers typically are responsible for designing an appropriate solution
Ch01-Programmer to a user’s problem, while computer programmers are responsible for translating the solution
Qualities into a language that the computer can understand—a process called coding. Software engineer-
ing is a higher-level position that requires the ability to envision solutions. Using a construction
analogy, software engineers are the architects, while programmers are the carpenters.
Keep in mind that, depending on the employer and the size and complexity of the user’s problem,
the design and coding tasks may be performed by the same employee, no matter what his or her
job title is. In other words, it’s not unusual for a software engineer to code his or her solution, just
as it’s not unusual for a programmer to have designed the solution he or she is coding.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
A Brief History of Programming Languages
Programmers and software engineers need to have strong problem-solving and analytical skills,
as well as the ability to communicate effectively with team members, end users, and other non-
technical personnel. Typically, computer software engineers are expected to have at least a bach-
elor’s degree in software engineering, computer science, or mathematics, along with practical
work experience, especially in the industry in which they are employed. Computer programmers
usually need at least an associate’s degree in computer science, mathematics, or information sys-
tems, as well as proficiency in one or more programming languages.
Computer programmers and software engineers are employed by companies in almost every
industry, such as telecommunications companies, software publishers, financial institutions,
insurance carriers, educational institutions, and government agencies. The Bureau of Labor
Statistics predicts that employment of computer software engineers will increase by 22 p ercent
from 2012 to 2022. The employment of computer programmers, on the other hand, will
increase by 8 percent over the same period. In addition, consulting opportunities for freelance
programmers and software engineers are expected to increase as companies look for ways to
reduce their payroll expenses.
There is a great deal of competition for programming and software engineering jobs, so jobseekers
will need to keep up to date with the latest programming languages and technologies. A competitive
edge may be gained by obtaining vendor-specific or language-specific certifications, as well as knowl-
edge of a prospective employer’s business. More information about computer programmers and
computer software engineers can be found on the Bureau of Labor Statistics Web site at www.bls.gov.
Machine Languages
Within a computer, all data is represented by microscopic electronic switches that can be either
off or on. The off switch is designated by a 0, and the on switch is designated by a 1. Because
computers can understand only these on and off switches, the first programmers had to write
the program instructions using nothing but combinations of 0s and 1s; for example, a program
might contain the instruction 00101 10001 10000. Instructions written in 0s and 1s are called
machine language or machine code. The machine languages (each type of machine has its
own language) represent the only way to communicate directly with the computer. As you can
imagine, programming in machine language is very tedious and error-prone and requires highly
trained programmers.
Assembly Languages
Slightly more advanced programming languages are called assembly languages. The assembly
languages simplify the programmer’s job by allowing the programmer to use mnemonics in
place of the 0s and 1s in the program. Mnemonics are memory aids—in this case, alphabetic
abbreviations for instructions. For example, most assembly languages use the mnemonic ADD
to represent an add operation and the mnemonic MUL to represent a multiply operation. An
example of an instruction written in an assembly language is ADD bx, ax.
Programs written in an assembly language require an assembler, which also is a program,
to convert the assembly instructions into machine code—the 0s and 1s the computer can
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
Chapter 1 An Introduction to Programming
understand. Although it is much easier to write programs in assembly language than in machine
language, programming in assembly language still is tedious and requires highly trained
programmers. Programs written in assembly language are machine specific and usually must be
rewritten in a different assembly language to run on different computers.
High-Level Languages
High-level languages represent the next major development in programming languages.
igh-level languages are a vast improvement over machine and assembly languages because
H
they allow the programmer to use instructions that more closely resemble the English language.
An example of an instruction written in a high-level language is grossPay = hours * rate. In
addition, high-level languages are more machine independent than are machine and assembly
languages. As a result, programs written in a high-level language can be used on many different
types of computers.
Programs written in a high-level language usually require a compiler, which also is a program,
to convert the English-like instructions into the 0s and 1s the computer can understand. Some
high-level languages also offer an additional program called an interpreter. Unlike a compiler,
which translates all of a program’s high-level instructions before running the program, an
interpreter translates the instructions line by line as the program is running.
Like their predecessors, the first high-level languages were used to create procedure-oriented
programs. When writing a procedure-oriented program, the programmer concentrates on
the major tasks that the program needs to perform. A payroll program, for example, typically
performs several major tasks, such as inputting the employee data, calculating the gross pay,
calculating the taxes, calculating the net pay, and outputting a paycheck. The programmer must
instruct the computer every step of the way, from the start of the task to its completion. In a
procedure-oriented program, the programmer determines and controls the order in which the
computer processes the instructions. In other words, the programmer must determine not only
the proper instructions to give the computer but the correct sequence of those instructions as
well. Examples of high-level languages used to create procedure-oriented programs include
COBOL (Common Business-Oriented Language), BASIC (Beginner’s All-Purpose Symbolic
Instruction Code), and C.
Most objects More advanced high-level languages can be used to create object-oriented programs in addition
in an object- to procedure-oriented ones. Different from a procedure-oriented program, which focuses
oriented on the individual tasks the program must perform, an object-oriented program requires
program are the programmer to focus on the objects that the program can use to accomplish its goal. The
designed objects can take on many different forms. For example, programs written for the Windows
to perform multiple
environment typically use objects such as check boxes, list boxes, and buttons. A payroll
tasks. These tasks are
program, on the other hand, might utilize objects found in the real world, such as a time card
programmed using the
same techniques used object, an employee object, or a check object.
in procedure-oriented Because each object in an object-oriented program is viewed as an independent unit, an object
programming.
can be used in more than one program, usually with little or no modification. A check object
used in a payroll program, for example, also can be used in a sales revenue program (which
receives checks from customers) and an accounts payable program (which issues checks to
creditors). The ability to use an object for more than one purpose enables code reuse, which
saves programming time and money—an advantage that contributes to the popularity of
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
Control Structures
bject-oriented programming. Examples of high-level languages that can be used to create both
o
procedure-oriented and object-oriented programs include C++, Visual Basic, Java, and C#. In this
book, you will learn how to use the C++ programming language to create procedure-oriented
and object-oriented programs.
The answers
Mini-Quiz 1-1 to Mini-Quiz
questions are
contained in the
1. Instructions written in 0s and 1s are called _________________________ language.
Answers.pdf file.
2. When writing a(n) _________________________ program, the programmer
concentrates on the major tasks needed to accomplish a goal.
a. procedure-oriented
b. object-oriented
Control Structures
All computer programs, no matter how simple or how complex, are written using one or more
of three basic structures: sequence, selection, and repetition. These structures are called c
ontrol
structures or logic structures because they control the flow of a program’s logic. In other
words, they control the order in which the computer executes the program’s instructions. You
will use the sequence structure in every program you write. In most programs, you also will
use the selection and repetition structures. This chapter gives you an introduction to the three
control structures. More detailed information about each structure, as well as how to implement
these structures using the C++ language, is provided in subsequent chapters.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Discovering Diverse Content Through
Random Scribd Documents
dismissed without a character. She too wanders off forlorn into a
world that has no haven of rest or voice of welcome for her—
wanders off, without so much as a dirty bundle in her hand—
wanders off, voiceless, with the unchanging grin on the smut-
covered face. How shocked we should all be, if we opened a book
about a savage country, and saw a portrait of Number Two in the
frontispiece as a specimen of the female population!
Number Three comes to us all the way from Wales; arrives late one
evening, and is found at seven the next morning, crying as if she
would break her heart, on the door-step. It is the first time she has
been away from home. She has not got used yet to being a forlorn
castaway among strangers. She misses the cows of a morning, the
blessed fields with the blush of sunrise on them, the familiar faces,
the familiar sounds, the familiar cleanliness of her country home.
There is not the faintest echo of mother's voice, or of father's sturdy
footfall here. Sweetheart John Jones is hundreds of miles away; and
little brother Joe toddles up door-steps far from these to clamour for
the breakfast which he shall get this morning from other than his
sister's hands. Is there nothing to cry for in this? Absolutely nothing,
as Mrs. Glutch thinks. What does this Welsh barbarian mean by
clinging to my area-railings when she ought to be lighting the fire;
by sobbing in full view of the public of Smeary Street when the
lodgers' bells are ringing angrily for breakfast? Will nothing get the
girl in-doors? Yes, a few kind words from the woman who passes by
her with my breakfast will. She knows that the Welsh girl is hungry
as well as home-sick, questions her, finds out that she has had no
supper after her long journey, and that she has been used to
breakfast with the sunrise at the farm in Wales. A few merciful
words lure her away from the railings, and a little food inaugurates
the process of breaking her in to London service. She has but a few
days allowed her, however, to practise the virtue of dogged
resignation in her first place. Before she has given me many
opportunities of studying her character, before she has done knitting
her brows with the desperate mental effort of trying to comprehend
the mystery of my illness, before the smut has fairly settled on her
rosy cheeks, before the London dirt has dimmed the pattern on her
neat print gown, she, too, is cast adrift into the world. She has not
suited Mrs. Glutch (being, as I imagine, too offensively clean to form
an appropriate part of the kitchen furniture)—a friendly maid-of-all-
work, in service near us, has heard of a place for her—and she is
forthwith sent away to be dirtied and deadened down to her proper
social level in another Lodging-house.
With her, my studies of character among maids-of-all-work come to
an end. I hear vague rumours of the arrival of Number Four. But
before she appears, I have got the doctor's leave to move into the
country, and have terminated my experience of London lodgings, by
making my escape with all convenient speed from the perpetual
presence and persecutions of Mrs. Glutch. I have witnessed some
sad sights during my stay in Smeary Street, which have taught me
to feel for my poor and forlorn fellow-creatures as I do not think I
ever felt for them before, and which have inclined me to doubt for
the first time whether worse calamities might not have overtaken me
than the hardship of falling ill.
SKETCHES OF CHARACTER.—II.
A SHOCKINGLY RUDE ARTICLE.
II.
THE GREAT (FORGOTTEN) INVASION.
Preamble.
It happened some sixty years ago; it was a French invasion; and it
actually took place in England. Thousands of people are alive at the
present moment, who ought to remember it perfectly well. And yet it
has been forgotten. In these times, when the French invasion that
may come, turns up perpetually, in public and in private, as a subject
of discussion—the French invasion that did come, is not honoured
with so much as a passing word of notice. The new generation
knows nothing about it. The old generation has carelessly forgotten
it. This is discreditable, and it must be set right; this is a dangerous
security, and it must be disturbed; this is a gap in the Modern
History of England, and it must be filled up.
Fathers and mothers, read and be reminded; British youths and
maidens, read and be informed. Here follows the true history of the
great forgotten Invasion of England, at the end of the last century;
divided into scenes and periods, and carefully derived from proved
and written facts recorded in Kelly's History of the Wars:
I. Of the French Invasion as seen from Ilfracombe.
On the twenty-second day of February, in the year seventeen
hundred and ninety-seven, the inhabitants of North Devonshire
looked towards the Bristol Channel, and saw the French invasion
coming on, in four ships.
The Directory of the French Republic had been threatening these
islands some time previously; but much talk and little action having
characterised the proceedings of that governing body in most other
matters, no great apprehension was felt of their really carrying out
their expressed intention in relation to this country. The war
between the two nations was, at this time, confined to naval
operations, in which the English invariably got the better of the
French. North Devonshire (as well as the rest of England) was aware
of this, and trusted implicitly in our supremacy of the seas. North
Devonshire got up on the morning of the twenty-second of February,
without a thought of the invasion; North Devonshire looked out
towards the Bristol Channel, and there—in spite of our supremacy of
the seas—there the invasion was, as large as life.
Of the four ships which the Directory had sent to conquer England,
two were frigates and two were smaller vessels. This formidable
fleet sailed along, in view of a whole panic-stricken, defenceless
coast; and the place at which it seemed inclined to try the invading
experiment first, was Ilfracombe. The commander of the expedition
brought his ships up before the harbour, scuttled a few coasting
vessels, prepared to destroy the rest, thought better of it, and
suddenly turned his four warlike sterns on North Devonshire, in the
most unaccountable manner. History is silent as to the cause of this
abrupt and singular change of purpose. Did the chief of the invaders
act from sheer indecision? Did he distrust the hotel accommodation
at Ilfracombe? Had he heard of the clotted cream of Devonshire, and
did he apprehend the bilious disorganisation of the whole army, if
they once got within reach of that luscious delicacy? These are
important questions, but no satisfactory answer can be found to
them. The motives which animated the commander of the invading
Frenchmen, are buried in oblivion: the fact alone remains, that he
spared Ilfracombe. The last that was seen of him from North
Devonshire, he was sailing over ruthlessly to the devoted coast of
Wales.
II. Of the French Invasion as seen by Welshmen in general.
In one respect it may be said that Wales was favoured by
comparison with North Devonshire. The great fact of the French
invasion had burst suddenly on Ilfracombe; but it only dawned in a
gradual manner on the coast of Pembrokeshire. In the course of his
cruise across the Bristol Channel, it had apparently occurred to the
commander of the expedition, that a little diplomatic deception, at
the outset, might prove to be of ultimate advantage to him. He
decided, therefore, on concealing his true character from the eyes of
the Welshmen; and when his four ships were first made out, from
the heights above Saint Bride's Bay, they were all sailing under
British colours.
There are men in Wales, as in the rest of the world, whom it is
impossible to satisfy; and there were spectators on the heights of
Saint Bride's who were not satisfied with the British colours, on this
occasion, because they felt doubtful about the ships that bore them.
To the eyes of these sceptics all four vessels had an unpleasantly
French look, and manœuvred in an unpleasantly French manner.
Wise Welshmen along the coast collected together by twos and
threes, and sat down on the heights, and looked out to sea, and
shook their heads, and suspected. But the majority, as usual, saw
nothing extraordinary where nothing extraordinary appeared to be
intended; and the country was not yet alarmed; and the four ships
sailed on till they doubled Saint David's Head; and sailed on again, a
few miles to the northward; and then stopped, and came to single
anchor in Cardigan Bay.
Here, again, another difficult question occurs, which recalcitrant
History once more declines to solve. The Frenchmen had hardly
been observed to cast their single anchors in Cardigan Bay, before
they were also observed to pull them up again, and go on. Why?
The commander of the expedition had doubted already at
Ilfracombe—was he doubting again in Cardigan Bay? Or did he
merely want time to mature his plans; and was it a peculiarity of his
nature that he always required to come to anchor before he could
think at his ease? To this mystery, as to the mystery at Ilfracombe,
there is no solution; and here, as there, nothing is certainly known
but that the Frenchman paused—threatened—and then sailed on.
III. Of One Welshman in Particular, and of what he saw.
He was the only man in Great Britain who saw the invading army
land on our native shores—and his name has perished.
It is known that he was a Welshman, and that he belonged to the
lower order of the population. He may be still alive—this man, who is
connected with a crisis in English History, may be still alive—and
nobody has found him out; nobody has taken his photograph;
nobody has written a genial biographical notice of him; nobody has
made him into an Entertainment; nobody has held a
Commemoration of him; nobody has presented him with a
testimonial, relieved him by a subscription, or addressed him with a
speech. In these enlightened times, this brief record can only single
him out and individually distinguish him—as the Hero of the
Invasion. Such is Fame.
The Hero of the Invasion, then, was standing, or sitting—for even on
this important point tradition is silent—on the cliffs of the Welsh
coast, near Lanonda Church, when he saw the four ships enter the
bay below him, and come to anchor—this time, without showing any
symptoms of getting under weigh again. The English colours, under
which the Expedition had thus far attempted to deceive the
population of the coast, were now hauled down, and the threatening
flag of France was boldly hoisted in their stead. This done, the boats
were lowered away, were filled with a ferocious soldiery, and were
pointed straight for the beach.
It is on record that the Hero of the Invasion distinctly saw this; and
it is not on record that he ran away. Honour to the unknown brave!
Honour to the solitary Welshman who faced the French army!
The boats came on straight to the beach—the ferocious soldiery
leapt out on English soil, and swarmed up the cliff, thirsting for the
subjugation of the British Isles. The Hero of the Invasion, watching
solitary on the cliffs, saw the Frenchmen crawling up below him—
tossing their muskets on before them—climbing with the cool
calculation of an army of chimney-sweeps—nimble as the monkey,
supple as the tiger, stealthy as the cat—hungry for plunder,
bloodshed, and Welsh mutton—void of all respect for the British
Constitution—an army of Invaders on the Land of the Habeas
Corpus!
The Welshman saw that, and vanished. Whether he waited with
clenched fist till the head of the foremost Frenchman rose parallel
with the cliff-side, or whether he achieved a long start, by letting the
army get half-way up the cliff, and then retreating inland to give the
alarm—is, like every other circumstance in connection with the Hero
of the Invasion, a matter of the profoundest doubt. It is only known
that he got away at all, because it is not known that he was taken
prisoner. He parts with us here, the shadow of a shade, the most
impalpable of historical apparitions. Honour, nevertheless, to the
crafty brave! Honour to the solitary Welshman who faced the French
army without being shot, and retired from the French army without
being caught!
IV. Of what the Invaders did when they got on shore.
The Art of Invasion has its routine, its laws, manners, and customs,
like other Arts. And the French army acted strictly in accordance with
established precedents. The first thing the first men did, when they
got to the top of the cliff, was to strike a light and set fire to the
furze-bushes. While national feeling deplores this destruction of
property, unprejudiced History looks on at her ease. Given Invasion
as a cause, fire follows, according to all known rules, as an effect. If
an army of Englishmen had been invading France under similar
circumstances, they, on their side, would necessarily have begun by
setting fire to something; and unprejudiced History would, in that
case also, have looked on at her ease.
While the furze-bushes were blazing, the remainder of the invaders
—assured by the sight of the flames, of their companions' success so
far—was disembarking, and swarming up the rocks. When it was
finally mustered on the top of the cliff, the army amounted to
fourteen hundred men. This was the whole force which the Directory
of the French Republic had thought it desirable to despatch for the
subjugation of Great Britain. History, until she is certain of results,
will pronounce no opinion on the wisdom of this proceeding. She
knows that nothing in politics, is abstractedly rash, cruel,
treacherous, or disgraceful—she knows that Success is the sole
touchstone of merit—she knows that the man who fails is
contemptible, and the man who succeeds is illustrious, without any
reference to the means used in either case; to the character of the
men; or to the nature of the motives under which they may have
proceeded to action. If the Invasion succeeds, History will applaud it
as an act of heroism: if it fails, History will condemn it as an act of
folly.
It has been said that the Invasion began creditably, according to the
rules established in all cases of conquering. It continued to follow
those rules with the most praiseworthy regularity. Having started
with setting something on fire, it went on, in due course, to
accomplish the other first objects of all Invasions, thieving and killing
—performing much of the former, and little of the latter. Two rash
Welshmen, who persisted in defending their native leeks, suffered
accordingly: the rest lost nothing but their national victuals, and
their national flannel. On this first day of the Invasion, when the
army had done marauding, the results on both sides may be thus
summed up. Gains to the French:—good dinners, and protection
next the skin. Loss to the English:—mutton, stout Welsh flannel, and
two rash countrymen.
V. Of the British Defence, and of the way in which the women
contributed to it.
The appearance of the Frenchmen on the coast, and the loss to the
English, mentioned above, produced the results naturally to be
expected. The country was alarmed, and started up to defend itself.
On the numbers of the invaders being known, and on its being
discovered that, though they were without field-pieces, they had
with them seventy cart-loads of powder and ball, and a quantity of
grenades, the principal men in the country bestirred themselves in
setting up the defence. Before nightfall, all the available men who
knew anything of the art of fighting were collected. When the ranks
were drawn out, the English defence was even more ridiculous in
point of numbers than the French attack. It amounted, at a time
when we were at war with France, and were supposed to be
prepared for any dangers that might threaten—it amounted,
including militia, fencibles, and yeomanry cavalry, to just six hundred
and sixty men, or, in other words, to less than half the number of
the invading Frenchmen.
Fortunately for the credit of the nation, the command of this
exceedingly compact force was taken by the principal grandee in the
neighbourhood. He turned out to be a man of considerable cunning,
as well as a man of high rank; and he was known by the style and
title of the Earl of Cawdor.
The one cheering circumstance in connection with the heavy
responsibility which now rested on the shoulders of the Earl,
consisted in this: that he had apparently no cause to dread internal
treason as well as foreign invasion. The remarkably inconvenient
spot which the French had selected for their landing, showed, not
only that they themselves knew nothing of the coast, but that none
of the inhabitants, who might have led them to an easier place of
disembarkation, were privy to their purpose. So far so good. But still,
the great difficulty remained of facing the French with an equality of
numbers, and with the appearance, at least, of an equality of
discipline. The first of these requisites it was easy to fulfil. There
were hosts of colliers and other labourers in the neighbourhood,—
big, bold, lusty fellows enough; but so far as the art of marching and
using weapons was concerned, as helpless as a pack of children.
The question was, how to make good use of these men for show-
purposes, without allowing them fatally to embarrass the
proceedings of their trained and disciplined companions. In this
emergency, Lord Cawdor hit on a grand Idea. He boldly mixed the
women up in the business—and it is unnecessary to add, that the
business began to prosper from that lucky moment.
In those days, the wives of the Welsh labourers wore, what the
wives of all classes of the community have been wearing since—red
petticoats. It was Lord Cawdor's happy idea to call on these patriot-
matrons to sink the question of skirts; to forego the luxurious
consideration of warmth; and to turn the colliers into military men
(so far as external appearances, viewed at a distance, were
concerned), by taking off the wives' red petticoats and putting them
over the husbands' shoulders. Where patriot-matrons are concerned,
no national appeal is made in vain, and no personal sacrifice is
refused. All the women seized their strings, and stepped out of their
petticoats on the spot. What man in that make-shift military but
must think of "home and beauty," now that he had the tenderest
memento of both to grace his shoulders and jog his memory? In an
inconceivably short space of time every woman was shivering, and
every collier was turned into a soldier.
VI. Of how it all ended.
Thus recruited, Lord Cawdor marched off to the scene of action; and
the patriot women, deprived of their husbands and their petticoats,
retired, it is to be hoped and presumed, to the friendly shelter of
bed. It was then close on nightfall, if not actually night; and the
disorderly marching of the transformed colliers could not be
perceived. But, when the British army took up its position, then was
the time when the excellent stratagem of Lord Cawdor told at its
true worth. By the uncertain light of fires and torches, the French
scouts, let them venture as near as they might, could see nothing in
detail. A man in a scarlet petticoat looked as soldier-like as a man in
a scarlet coat, under those dusky circumstances. All that the enemy
could now see were lines on lines of men in red, the famous uniform
of the English army.
The council of the French braves must have been a perturbed
assembly on that memorable night. Behind them, was the empty bay
—for the four ships, after landing the invaders, had set sail again for
France, sublimely indifferent to the fate of the fourteen hundred.
Before them, there waited in battle array an apparently formidable
force of British soldiers. Under them was the hostile English ground
on which they were trespassers caught in the fact. Girt about by
these serious perils, the discreet commander of the Invasion fell
back on those safeguards of caution and deliberation of which he
had already given proofs on approaching the English shore. He had
doubted at Ilfracombe; he had doubted again in Cardigan Bay; and
now, on the eve of the first battle, he doubted for the third time—
doubted, and gave in. If History declines to receive the French
commander as a hero, Philosophy opens her peaceful doors to him,
and welcomes him in the character of a wise man.
At ten o'clock that night, a flag of truce appeared in the English
camp, and a letter was delivered to Lord Cawdor from the prudent
chief of the invaders. The letter set forth, with amazing gravity and
dignity, that the circumstances under which the French troops had
landed, having rendered it "unnecessary" to attempt any military
operations, the commanding officer did not object to come forward
generously and propose terms of capitulation. Such a message as
this was little calculated to impose on any man—far less on the artful
nobleman who had invented the stratagem of the red petticoats.
Taking a slightly different view of the circumstances, and declining
altogether to believe that the French Directory had sent fourteen
hundred men over to England to divert the inhabitants by the
spectacle of a capitulation, Lord Cawdor returned for answer that he
did not feel himself at liberty to treat with the French commander,
except on the condition of his men surrendering as prisoners of war.
On receiving this reply, the Frenchman gave an additional proof of
that philosophical turn of mind which has been already claimed for
him as one of his merits, by politely adopting the course which Lord
Cawdor suggested. By noon the next day, the French troops were all
marched off, prisoners of war—the patriot-matrons had resumed
their petticoats—and the short terror of the invasion had happily
passed away.
The first question that occurred to everybody, as soon as the alarm
had been dissipated, was, what this extraordinary burlesque of an
invasion could possibly mean. It was asserted, in some quarters,
that the fourteen hundred Frenchmen had been recruited from those
insurgents of La Vendée who had enlisted in the service of the
Republic, who could not be trusted at home, and who were
therefore despatched on the first desperate service that might offer
itself abroad. Others represented the invading army as a mere gang
of galley-slaves and criminals in general, who had been landed on
our shores with the double purpose of annoying England and ridding
France of a pack of rascals. The commander of the expedition,
however, disposed of this latter theory by declaring that six hundred
of his men were picked veterans from the French army, and by
referring, for corroboration of this statement, to his large supplies of
powder, ball, and hand-grenades, which would certainly not have
been wasted, at a time when military stores were especially
precious, on a gang of galley-slaves.
The truth seems to be, that the French (who were even more
densely ignorant of England and English institutions at that time
than they are at this) had been so entirely deceived by false reports
of the temper and sentiments of our people, as to believe that the
mere appearance of the troops of the Republic on these Monarchical
shores, would be the signal for a revolutionary rising of all the
disaffected classes from one end of Great Britain to the other.
Viewed merely as materials for kindling the insurrectionary spark,
the fourteen hundred Frenchmen might certainly be considered
sufficient for the purpose—providing the Directory of the Republic
could only have made sure beforehand that the English tinder might
be depended on to catch light!
One last event must be recorded before this History can be
considered complete. The disasters of the invading army, on shore,
were matched, at sea, by the disasters of the vessels that had
carried them. Of the four ships which had alarmed the English coast,
the two largest (the frigates) were both captured, as they were
standing in for Brest Harbour, by Sir Harry Neale. This smart and
final correction of the fractious little French invasion was
administered on the ninth of March, seventeen hundred and ninety-
seven.
Moral.
This is the history of the Great (Forgotten) Invasion. It is short, it is
not impressive, it is unquestionably deficient in serious interest. But
there is a Moral to be drawn from it, nevertheless. If we are invaded
again, and on a rather larger scale, let us not be so ill-prepared, this
next time, as to be obliged to take refuge in our wives' red
petticoats.
CURIOSITIES OF LITERATURE.—I.
THE UNKNOWN PUBLIC.
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.
ebookname.com