A Beginner S Introduction To Computer Programming

Transcription

A Beginner’s Introduction to ComputerProgrammingYou Can Do It!Francis Glassborowwith Roberta Allen

A Beginner’s Introduction to ComputerProgrammingFrancis Glassborowwith Roberta Allen

A Beginner’s Introduction to ComputerProgrammingYou Can Do It!Francis Glassborowwith Roberta Allen

Copyright 2004John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester,West Sussex PO19 8SQ, EnglandTelephone ( 44) 1243 779777Email (for orders and customer service enquiries): cs-books@wiley.co.ukVisit our Home Page on www.wileyeurope.com or www.wiley.comAll Rights Reserved. No part of this publication may be reproduced, stored in a retrievalsystem or transmitted in any form or by any means, electronic, mechanical, photocopying,recording, scanning or otherwise, except under the terms of the Copyright, Designs andPatents Act 1988 or under the terms of a licence issued by the Copyright Licensing AgencyLtd, 90 Tottenham Court Road, London W1T 4LP, UK, without the permission in writing ofthe Publisher, with the exception of any material supplied specifically for the purpose ofbeing entered and executed on a computer system for exclusive use by the purchase of thepublication. Requests to the Publisher should be addressed to the Permissions Department,John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ,England, or emailed to permreq@wiley.co.uk, or faxed to ( 44) 1243 770620.This publication is designed to provide accurate and authoritative information in regard tothe subject matter covered. It is sold on the understanding that the Publisher is not engagedin rendering professional services. If professional advice or other expert assistance isrequired, the services of a competent professional should be sought.Other Wiley Editorial OfficesJohn Wiley & Sons Inc., 111 River Street, Hoboken, NJ 07030, USAJossey-Bass, 989 Market Street, San Francisco, CA 94103-1741, USAWiley-VCH Verlag GmbH, Boschstr. 12, D-69469 Weinheim, GermanyJohn Wiley & Sons Australia Ltd, 33 Park Road, Milton, Queensland 4064, AustraliaJohn Wiley & Sons (Asia) Pte Ltd, 2 Clementi Loop #02-01, Jin Xing Distripark, Singapore 129809John Wiley & Sons Canada Ltd, 22 Worcester Road, Etobicoke, Ontario, Canada M9W 1L1Wiley also publishes its books in a variety of electronic formats. Some content that appearsin print may not be available in electronic books.Library of Congress Cataloging-in-Publication DataGlassborow, Francis.A beginner’s introduction to computer programming : you can do it! /Francis Glassborow.p. cm.Includes bibliographical references and index.ISBN 0-470-86398-6 (Paper : alk. paper)1. Computer programming. I. Title.QA76.6.G575 2003005.1 – dc222003020686British Library Cataloguing in Publication DataA catalogue record for this book is available from the British LibraryISBN 0-470-86398-6Typeset in 10/11pt Joanna by Laserwords Private Limited, Chennai, IndiaPrinted and bound in Great Britain by Biddles Ltd, King’s LynnThis book is printed on acid-free paper responsibly manufactured from sustainable forestryin which at least two trees are planted for each one used for paper production.

DedicationThis book is dedicated to the many colleagues and numerous students who moulded me into a better teacher.In particular my first Head of Department, Gerry Astell, who taught me never to teach something that I knewI would later retract. False simplicity is never helpful to the student in the long run; it just makes a poorteacher’s life easier for a moment.

ContentsHow to Use This Book . . .Study Elements . . . . . .End of Chapter ElementsEnd of the Book . . . . . .The CD . . . . . . . . . . .Why fgw? . . . . . . . . .xiiixivxivxvxvxviIntroduction . . . . . . . . . . . . . .Before Purchase . . . . . . . . . . .Why C ? . . . . . . . . . . . . .Getting the Best from This Book .What You Will Achieve . . . . . .Notes for Students . . . . . . . . .Notes for Instructors . . . . . . . .Personal Introductions . . . . . . .xviixviixviiixviiixxxxxxxxiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii1 You Can Program . . . . . . . . . . . . . . .What Is Programming? . . . . . . . . . . . .Introduction to Your Programming Tools .Our First Program . . . . . . . . . . . . . . .Elements of C Programs . . . . . . . . .A Playpen Doesn’t Have To Be White . . .Plotting a Point . . . . . . . . . . . . . . . . .Mixing Colors . . . . . . . . . . . . . . . . . .Modern Art? . . . . . . . . . . . . . . . . . . .Tasks, Exercises and Fun . . . . . . . . . . . .Roberta’s Comments . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . .11261011131515161818

viiiCONTENTS2 You Can Loop . . . . . . . . .Drawing a Cross . . . . . . . .for-Loops . . . . . . . . . . .Drawing a Cross Revisited .Practicing Looping . . . . . .Simple Arithmetic OperatorsRoberta’s Comments . . . . .Solutions to Exercises . . . .Summary . . . . . . . . . . . .2121222627283132333 You Can Write a Function . . . . . . .Drawing a Square . . . . . . . . . . . . .The Function Concept . . . . . . . . . .Functions in C . . . . . . . . . . . .Writing a Function . . . . . . . . . . . .Header and Implementation Files . . .Drawing Lines . . . . . . . . . . . . . . .Drawing Sets of Lines . . . . . . . . . .Creating Your Own Utility FunctionsRoberta’s Comments . . . . . . . . . . .Solutions to Exercises . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . .3535363738424447505151524 You Can Communicate . . . . . .Names and Namespaces . . . . . .Interaction . . . . . . . . . . . . . .The char and int Types . . . . .Streams . . . . . . . . . . . . . . . .The string Type . . . . . . . . . .Creating a Simple Dialog . . . . .Sequence Containers . . . . . . . .Walkthrough . . . . . . . . . . . . .Getting ints from the KeyboardHandling the Unexpected . . . . .Roberta’s Comments . . . . . . . .Hints . . . . . . . . . . . . . . . . . .Solutions to Exercises . . . . . . .Summary . . . . . . . . . . . . . . .5555565759616163656970747474815 You Can Create a Type . . . . . . . . . . .On Not Being Underrated . . . . . . . . . .Designing a Type . . . . . . . . . . . . . . .The double Type . . . . . . . . . . . . . . .Creating a Two-Dimensional Point TypeRoberta’s Comments . . . . . . . . . . . . .Solutions to Exercises . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . .83838485871011011016 You Can Use point2d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Adding Functionality with Free Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Supporting I/O for point2d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105105107.

08 You Can Write a Menu . . .Offering a Set of Choices . .Dealing with Dependencies .Functions that Fill a PolygonRoberta’s Comments . . . . .Solutions to Tasks . . . . . . .Solutions to Exercises . . . .Summary . . . . . . . . . . . .1351351451481511511531539 You Can Keep Data . . . . . . . . . . . . . .Saving and Restoring Images . . . . . . . . .Using Captured Data . . . . . . . . . . . . . .A Menu-Driven Program with Persistence .Further Practice . . . . . . . . . . . . . . . . .Iterators . . . . . . . . . . . . . . . . . . . . . .Before the Next Chapter . . . . . . . . . . . .Hints . . . . . . . . . . . . . . . . . . . . . . . .Solutions to Tasks . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . .15515515716116916917317417418210 Lotteries, Ciphers and Random ChoicesRandom and Pseudo-Random SequencesAlgorithms for Random Numbers . . . . .Understanding a Lottery Program . . . . .Sending Hidden Messages . . . . . . . . . .Over to You . . . . . . . . . . . . . . . . . .Roberta’s Comments . . . . . . . . . . . . .Solutions to Tasks . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . .18518518618719620120220420511 Keyboards and Mice .A Keyboard Type . . .Using a Mouse . . . . .Refactoring Code . . .More Practice . . . . .Roberta’s Comments .207207213217220222Drawing Lines and PolygonsDrawing Regular Polygons .A Type and an Origin . . . .Roberta’s Comments . . . . .Hints . . . . . . . . . . . . . . .Solutions to Tasks . . . . . . .Solutions to Exercises . . . .Summary . . . . . . . . . . . .7 You Can Have Fun . . .Valuing Your Skills . . .Just for Fun . . . . . . . .Fun Programming IdeasLooking Forward . . . .

xCONTENTSSolutions to Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 A Pot Pourri Spiced with Bitset .Computing a List of Primes . . . .A Weaving Simulation . . . . . . .Dr Conway’s Game of Life . . . .Roberta’s Comments . . . . . . . .Solutions to Tasks . . . . . . . . . .Summary . . . . . . . . . . . . . . .22922923523924624624713 How Many. . .? in Which Set and Map Lend a HandWhat Is an Associative Container? . . . . . . . . . . .

A beginner s introduction to computer programming : you can do it! / Francis Glassborow. p. cm. Includes bibliographical re ferences and index. ISBN 0-470-86398-6 (Paper : alk. paper) 1. Computer programming. I. Title. QA76.6.G575 2003 005.1 dc22 2003020686