ebook img

Pascal: Understanding Programming and Problem Solving PDF

745 Pages·1989·36.919 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pascal: Understanding Programming and Problem Solving

Pascal Und(3rstandiii Progiramming amd polvini Probl em SECOND EDmON Douglas W. Nance CENTRAL MICHIGAN UNIVERSITY WEST PUBUSHING COMPANY ST. PAUL NEW YORK LOS ANGELES SANFRANQSCO To Helen Copyeditor and Indexer: Janet Hunter Interior design: Paula Schlosser and Lucy Leslak Illustrations: Christine Dettner and Rolin Graphics Composition: Carlisle Communications COPYRIGHT ©1984 By WEST PUBLISHING COMPANY COPYRIGHT ©1989 By WEST PUBLISHING COMPANY 50 W. Kellogg Boulevard P.O. Box 64526 St. Paul, MN 55164-1003 All rights reserved Printed in the United States of America 96 95 94 93 92 91 90 89 8 7 6 5 4 3 2 1 0 Library of Congress Cataloging-in-Publication Data Nance, Douglas W. Pascal: understanding programming and problem solving / Douglas W. Nance.—2nd ed. p. cm. Includes index. ISBN 0-314-43051-2 1. Pascal (Computer program language) I. Title. QA76.73.P2N35 1989 005.13'3—dcl9 88-29307 CIP X Contents n 1 Computer Science, Computer CHAPTER Architecture, and Computer Languages Computer Science: A Preview 1 1.1 Computer Architecture 4 1.2 A Note of Interest: Microprocessors 8 Computer Languages 9 1.3 A Note of Interest: Why Learn Pascal? 12 Summary 13 CHAPTER 2 ■Writing Your First Programs 14 Program Development—Top-Down Design 15 2.1 A Note of Interest: Software Engineering 21 2.2 Writing Programs 22 A Note of Interest: Blaise Pascal 32 Data Types and Output 33 2.3 Style Tip • 37 Style Tip 42 Summary 45 Programming Problems 46 3 "Arithmetic, Variables, Input, Constants, CHAPTER and Standard Functions 47 3.1 Arithmetic in Pascal 47 3.2 Using Variables 52 3.3 Input 59 vi Contents 3.4 Using Constants 68 A Note of Interest: Defined Constants and Space Shuttle Computing 69 3.5 Standard Functions 71 A Note of Interest: Herman Hollerith 74 Style Tip 78 Summary 78 Programming Problems 80 n CHAPTER 4 Designing and Writing Complete Programs 82 4.1 Writing Code 82 A Note of Interest: Documentation Employment 91 4.2 Procedures for Output 96 Style Tip 103 4.3 Beginners' Errors 111 4.4 Mciking a Program Run 120 Style Tip 121 A Note of Interest: Debugging 123 4.5 Writing a Complete Pascal Program 128 A Note of Interest: Debugging or Sleuthing? 130 Summary 136 A Note of Interest: Debugging or Sleuthing: Answers 137 Programming Problems 138 n CHAPTER 5 Conditional Statements 145 5.1 Boolean Expressions 145 A Note of Interest: George Boole 153 5.2 IF ... THEN Statements 156 5.3 IF ... THEN ... ELSE Statements 164 5.4 Nested IF Statements 169 Style Tip 173 5.5 CASE Statements 180 Style Tip 182 A Note of Interest: "Cross-Over" Scholars 185 5.6 Assertions (Optional) 188 Focus on Programming 189 Running and Debugging Tips 194 Summary 195 Programming Problems 196 n CHAPTER 6 Looping Statements 203 6.1 Fixed Repetition Loops 203 Style Tip 211 A Note of Interest: Ada Augusta Byron 212 Contents Vll 6.2 Pretest Loops 214 A Note of Interest: Debugging Space Flight Programs 222 6.3 Posttest Loops 228 A Note of Interest: Charles Babbage 231 6.4 Comparison of Loops 235 6.5 Nested Loops 239 Style Tip 241 6.6 Loops with Conditionals 247 A Note of Interest: A Digital Matter of Life and Death 249 Focus on Programming 253 Running and Debugging Tips 258 Summary 258 Programming Problems 261 chapter 7"Functions and Procedures ^ 7.1 Program Design 264 A Note of Interest: Structured Programming 265 7.2 Functions 265 7.3 Procedures 273 A Note of Interest: Ada 283 7.4 Scope of Identifiers 287 7.5 Using Subprograms 297 A Note of Interest: Niklaus Wirth: Pascal to Modula-2 300 7.6 Forward Reference and Nesting 307 Style Tip 308 Focus on Programming 314 Running and Debugging Tips 319 Summary 319 Programming Problems 322 CHAPTER 8"Text Files and User-Defined Data Types 324 8.1 Text Files 324 8.2 TYPE Definitions in Pascal 335 A Note of Interest: The Software System Life Cycle 339 8.3 Subrange as a Data Type 341 Style Tip 344 8.4 Operations on Ordinal Data Types 346 A Note of Interest: There's a Virus in My Software! 348 Focus on Programming 351 Running and Debugging Tips 355 Summary 355 Programming Problems 357 Vlll Contents n CHAPTER 9 One-Dimensional Arrays 360 9.1 Basic Ideas and Notation 360 Style Tip 364 A Note of Interest: Monolithic Idea: Invention of the Integrated Circuit 368 9.2 Using Arrays 370 Style Tip 376 9.3 Selection Sort 379 A Note of Interest: Transition to a Computer System 380 9.4 Arrays and Subprograms 385 9.5 Packed Arrays 393 A Note of Interest: Data Abstraction 398 9.6 Searching Algorithms 403 Focus on Programming 408 Running and Debugging Tips 414 Summary 415 Programming Problems 417 CHAPTER 10"A rrays of More Than One Dimension 422 10.1 Two-Dimensional Arrays 422 Style Tip 424 A Note of Interest: Telesecurity 433 10.2 Arrays of String Variables 436 10.3 Parallel Arrays (Optional) 440 Style Tip 440 A Note of Interest: Software Destruction 442 10.4 Higher-Dimensional Arrays 447 A Note of Interest: Computer Security 450 Focus on Programming 452 Running and Debugging Tips 460 Summary 460 Programming Problems 462 CHAPTER 11 ■Records 469 11.1 Record Definitions 469 Style Tip 473 A Note of Interest: Computer Industry Growth 473 11.2 Using Records 478 A Note of Interest: Using Key Fields in Records 486 11.3 Data Structures with Records 490 A Note of Interest: Neural Nets Catch the ABCs of DNA 493 11.4 Record Variants 501 Focus on Programming 506 Running and Debugging Tips 512 Summary 512 Programming Problems 514 Contents IX CHAPTER 12 n Files 518 12.1 File Definition 518 12.2 Working with Files 521 A Note of Interest: Relational Databases 528 12.3 Files with Structured Components 532 A Note of Interest: Backup and Recovery 537 Focus on Progranmiing 539 Running and Debugging Tips 547 Summary 548 Programming Problems 550 CHAPTER 13■Recursion, Sorting, and Merging 554 13.1 Recursion 554 13.2 Sorting Algorithms 564 A Note of Interest: Sex Differences in the Association between Secondary School Students' Attitudes toward Mathematics and toward Computers 566 13.3 Merging Algorithms 574 Running and Debugging Tips 578 Summary 578 Suggestions For Further Reading 579 Programming Problems 579 CHAPTER 14 ■ Sets 581 14.1 Declarations and Terms 581 A Note of Interest: Developing Educational Software 583 14.2 Set Operations and Relational Operators 585 14.3 Using Sets 588 Style Tip 589 A Note of Interest: Time Is Cure for Computerphobia 592 Focus on Programming 593 Running and Debugging Tips 597 Summary 598 Programming Problems 599 CHAPTER 15■Dynamic Variables and Data Structures 603 15.1 Pointer Variables 604 Style Tip 606 15.2 Linked Lists 610 Style Tip 610 15.3 Working with Linked Lists 617 Style Tip 617 A Note of Interest: Using Pointers 627 X Contents 15.4 Other Dynamic Data Structures 630 Running and Debugging Tips 641 Summary 642 Suggestions for Further Reading 645 Programming Problems 645. 'Appendixes A.l Appendix 1 Reserved Words A.2 Appendix 2 Standard Identifiers A.3 Appendix 3 Syntax Diagrams A.5 Appendix 4 Character Sets A.16 Appendix 5 Compiler Error Messages A.1 8 Appendix 6 Turbo Pascal Notes A.21 Appendix 7 GOTO Statements A.27 Glossary G.l Answers to Selected Exercises AN.i Index I.i Preface Those who teach entry-level courses in computer science are fa miliar with the problems that beginning students encounter. Ini tially, students can get so involved in learning a language that they may fail to grasp the significance of using the language to solve problems. Conversely, it is possible to emphasize problem solving to the extent that using a particular language to solve problems becomes almost incidental. The intent of this text is to provide a happy medium between these ap proaches. Students should understand language concepts and subse quently be able to use them to solve problems. Overview and As with the first edition, the material in Chapters 1-4 is presented at a Organization deliberate pace. If students in the class have already had some program ming experience, these chapters may be covered rapidly. However, stu dents must be able to solve problems using top-down design with stepwise refinement. Using this approach, students will be exposed to the concept and to restricted use of subprograms very early in the course. If this is overlooked,s tudents will have difficulty designing solutions to more com plex problems later. Throughout the text, I have attempted to explain and develop concepts carefully. These are illustrated by frequent examples and diagrams. New concepts are then used in complete programs to show how they aid in solving problems. An early and consistent emphasis has been placed on good writing habits and on producing neat, attractive output. I firmly believe program documentation and readability are important. Thus, I frequently discuss them in the text, and I offer style tips where appropriate. This text should provide a complete one-semester course in Pascal. There are at least three general scenarios for which this text would be appropriate. 1. A deliberately paced, thorough presentation of concepts would al low you to get through records and/or files in a one-semester course. XI xii Preface 2. An accelerated pace with students who have previous computing experience would allow you to get into Chapter 15 in a one- semester course. 3. A deliberate pace with a thorough presentation would allow you to present the material in Chapters 1-15 in a two-quarter sequence. Subprograms have been treated in a manner slightly different from other presentations. Procedures for headings only (no parameters) are intro duced in Chapter 4. Formal development of procedures is then delayed until Chapter 7 after conditional statements and loops have been dis cussed. At this stage, students are better able to understand the use of parameters. All subsequent work utilizes subprograms in problem solution and program design. In this edition, Chapter 8 presents text files as well as user-defined data types. Since larger data sets are typically used with arrays and files, it is assumed that most data will be read from data files (rather than entered interactively) after this chapter. Chapters 9 and 10 develop arrays. Due to the significance of this con cept, these chapters contain numerous examples, illustrations, and ap plications. A selection sort has been used to sort array elements. However, a bubble sort and an insertion sort have been developed in Chapter 13, and either could be used in place of the sorting algorithm presented in Section 9.3. Records and files are discussed in Chapters 11 and 12, re spectively. Their placement there is traditional. These chapters, combined with Chapters 9 and 10, present a detailed treatment of static data structures. Chapter 13 is an optional chapter that discusses recursion, sorting, and merging. Recursion has been moved to this chapter so that an expanded presentation would be more appropriately placed. This also allows the quick sort to be developed. For advanced classes, material in this chapter could be used to motivate additional work with data structures. It is possible to use this material with preceding chapters. For example,s orting could be discussed in conjunction with Chapter 9, and merging with Chapter 12. Chapter 14, Sets, could be presented any time after Chapter 8. Although a full chapter has been devoted to this topic, a working knowledge could be given to students in one or two days. Dynamic variables and data structures are introduced in Chapter 15. A reasonable discussion and development of pointers, linked lists, and binary trees is included. How ever, a full development of these concepts would have to come from a second course with a different text. Pascal statements in this text conform to standard Pascal. Due to the increasing use of Turbo Pascal with personal computers, Thrbo Pascal references are included in the margins to indicate where Thrbo differs from standard Pascal. Both interactive and batch mode examples are used in this text. Interactive examples are indicated by a logo in the left margin, as shown here. Features This text has a number of noteworthy pedagogical features. n Objectives—a concise list of topics and learning objectives in each section

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.