ebook img

Structured Programming Using Turbo BASIC PDF

337 Pages·1988·14.696 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 Structured Programming Using Turbo BASIC

Structured Programming Using Turbo BASIC WADE ELLIS, JR. and ED LODI West Valley College, Saratoga, California @ ACADEMIC PRESS, INC. Harcourt Brace Jovanovich, Publishers Boston San Diego New York Berkeley London Sydney Tokyo Toronto Copyright © 1988 by Academic Press, Inc. All rights reserved. No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopy, recording, or any information storage and retrieval system, without permission in writing from the publisher. ACADEMIC PRESS, INC. 1250 Sixth Avenue, San Diego, CA 92101 United Kingdom Edition published by ACADEMIC PRESS INC. (LONDON) LTD. 24-28 Oval Road, London, NW1 7DX Library of Congress Cataloging-in-Publication Data Ellis, Wade. Structured programming using Turbo BASIC/Wade Ellis, Jr., Ed Lodi. p. cm. Includes index. ISBN 0-12-237459-2 (hardcover) ISBN 0-12-237460-6 (paperback) 1. BASIC (Computer program language) 2. Turbo BASIC (Computer program) 3. Structured programming. I. Lodi, Ed. II. Title. QA76.73.B3E44 1988 005. 13'3-dcl9 87-38268 CIP 88 89 90 91 9 8 7 6 5 4 3 2 1 Printed in the United States of America PREFACE Structured Programming Using Turbo BASIC is an introduction to modern programming methods using the Turbo BASIC language. The book provides a practical approach to these methods through examples and problems from both business and mathematics. This approach allows you to learn programming while also learning about graphics, files, and simulations—topics not always covered in an introductory course. The problem-solving strategy developed in this book uses the top-down, structured programming method. This method consists of two main techniques: repeatedly breaking a problem into smaller, more manageable tasks and using the action block, the decision block, and the loop block—the three fundamental pro­ gramming structures—to perform each task. The use of subroutines or procedures greatly facilitates the process of breaking a problem into smaller parts. The style used to write programs is also an important part of the top-down programming method. Correct indentation and spacing improve readability, and internal descriptions of tasks make programs easier to follow and understand. These concepts, along with subroutines, are emphasized in the book. They are all introduced early and used throughout the book so that programs are easy to read and understand. Some programming constructs are introduced using graphics so that the tasks in a program can be connected with the resulting picture to help in understanding how a structure works. To understand all the fundamental structures of modern programming methods, you would need to be familiar with the fundamentals of algebra. We do include several challenging example programs and problems for those who have xvi Preface the interest and the necessary mathematical background. However, the only prerequisites for using this book are a familiarity with arithmetic and a desire to learn. The book has an introduction and 12 chapters. The first chapter presents the Turbo BASIC environment on an IBM PC (and compatible machines). The next seven chapters cover fundamental programming structures and concepts. Chapters 9 and 10 present the two data structures, arrays and files. The final two chapters introduce advanced graphics topics and computer simulations. Each chapter begins with a set of objectives followed by guided, hands-on computer exercises in the Exploratory Activities section. A summary and extension of these ideas is presented in the Concepts and Methods section. Beginning with Chapter 2, several program examples are carefully developed to illuminate the ideas studied in the chapter. Chapter 2 through 12 conclude with a variety of review questions and problems. To use this book you need the Turbo BASIC software package for the IBM PC, MS-DOS or PC-DOS 2.0 or later, an IBM PC computer (or compatible), one double-sided disk drive, a graphics adapter card (CGA, EGA, or equivalent), and a suitable monitor. If you have any questions about your equipment, you should consult your dealer or the computer lab director. This book can be used as a self-study guide to structured programming. When you use the book in this way, we encourage you to work through all the Exploratory Activities and Example Programs with great care. This book can also be used in an introductory lecture course in business, mathematics, or computer science. Students are encouraged to complete the Exploratory Activities using a computer before those activities are discussed in class. The first three chapters can be covered in one or two days each. The remaining chapters could be covered in four to seven class meetings each. Certain topics can be emphasized depending upon whether the course is taught from a mathematical or business perspective. Finally, the book can be used in an open-entry/open-exit computer lab­ oratory course. The student could work through the book in consultation with the laboratory assistants, and several progress exams could be given along with selected programming problems. Acknowledgements The authors would like to express their deep appreciation to several sources. Alice Peters, editor, and Carolyn Artin, managing editor, at Academic Press, Boston, gave us encouragement throughout the development, creation, and production of this book. Deborah Wickenden, copy editor, contributed many suggestions and corrections. The errors that remain are, of course, our own. Preface xvii We would also like to thank Herbert D. Peckham and Arthur Luehrmann of Computer Literacy Press for giving us the opportunity and encouragement to begin to write books on BASIC. Finally, we are deeply grateful to our wives, Jane and Rose Marie, for their patience and understanding throughout the writing and production of this book. Wade Ellis, Jr. Ed Lodi the Computer Tutors of San Jose P.O. Box 3709 Saratoga, CA 95070 INTRODUCTION Computers are a part of our everyday activities—for example, many home ap­ pliances, automobiles, and office machines are controlled by computers. Govern­ ment institutions and banking firms use them to store, retrieve, and manipulate information while industrial firms use them to solve manufacturing and scien- tifc problems. Each of us can think of other ways in which computers are used in our society. Moreover, you will probably be required to use a personal com­ puter or a computer terminal directly in your work. Most of the time you will be using powerful computer programs that are available commercially. In such cases, an understanding of programming provides a background that will allow you to learn quickly how to use these software packages to their maximum poten­ tial. In addition, many of the commercially available packages such as EXCEL , dBASE III™, Rbase 5000™, and LOTUS 1-2-3® have programming capabilities that require a programming background. You may, at some time in your career, be given the responsibility for choos­ ing a software package to be used by your company. Programming experience, obtained by working through this book, can give you the tools necessary to assess the capabilities of competing products and will assist you in choosing the product most appropriate for your company. You may be entering a career path in business or science that requires the regular use of computers at a high level of sophistication. The discipline of structured programming provides a solid basis for developing the needed skills for such careers. 2 Introduction The Development of the Discipline of Structured Programming The development of computer programming languages and methods began with the first electronic computers in the early 1940s. As with the early development of most things, the first programs were very successful but crude in comparison with current software packages. Early programmers began to experience difficulties getting their programs to run correctly as the length of the programs grew. The process of ferreting out errors in these pioneering programs became more and more difficult and tedious. The need for improved programming languages and methods became apparent. Computer scientists developed a set of ideas for systematically structuring programs to cope with difficulties encountered in using early versions of computer languages. The Development of Turbo BASIC Professors Kemeny and Kurtz, working with students at Dartmouth College in 1963 and 1964, created the BASIC language to provide a programming environ­ ment that was easy to learn, had substantial computational power, and would allow the user to enter information while the program was executing. They suc­ cessfully achieved their goal of bringing the use of computers within the reach of college students and faculty. In so doing, they created a language that can be easily used by professionals in business and science. The early versions of BASIC had the same problems mentioned above. These problems were addressed by the late 1970s with structured programming enhancements and graphics extensions to the language. However, Dartmouth BASIC could run on only large mainframe computers. At about this same time, versions of BASIC developed by Microsoft Inc. appeared on microcomputers. These versions were necessarily small and lacked the power of Dartmouth BASIC. In the mid-1980s, the size and speed of microcomputers had developed to the point where a robust, fully functional BASIC like Dartmouth BASIC could run effectively on them. Borland International developed the Turbo BASIC language to take advan­ tage of the original concept of BASIC as a programming language while adding structured programming concepts and their highly regarded programming en­ vironment. This language is easy to learn and has the structuring capabilities required to write long programs with few, if any, errors. Turbo BASIC also has strong graphics and data file handling facilities. Structured Programming in Turbo BASIC Turbo BASIC provides the necessary language structures for applying the dis­ cipline of structured programming. This book develops a method of writing programs that uses the structuring facilities and the word processing capabilities of Turbo BASIC to write programs that are well-structured, well-documented, Introduction 3 and easy to understand and modify. You must be willing to think carefully about problems away from the computer using paper and pencil to take full advantage of this method. Even after you begin to use the computer to enter your programs, the first step is to enter the general structure of the program. If you follow the method presented in this book, you will develop the discipline and techniques necessary to write long programs that can be easily modified or maintained by you or others. In addition, you can use this disciplined approach to solve problems with or without a computer. How to Use This Book Objectives are stated at the beginning of each chapter. Following these objectives, Exploratory Activities are presented as guided exercises for you to complete using your computer. It is important for you to work carefully through these exercises to learn basic concepts and gain experience with the language environment. You should attempt to answer any questions asked in these activities before proceed­ ing. Sometimes, you will be led through errors that can occur when using the computer with Turbo BASIC. You will find the answers to some of the harder questions in the text that follows the questions. In the Concepts and Methods section that follows the Exploratory Activ­ ities, the concepts you worked with in the activities are discussed. Additional material is presented in this section to enhance your understanding of the lan­ guage features of Turbo BASIC based upon your hands-on experience. Starting in Chapter 2, the Example Programs section presents solutions to typical programming problems, illustrating the concepts covered in the chapter. All the example programs are written using the top-down structured program­ ming method. At the end of every chapter except Chapter 1, there is a two-part section called Exercises containing review questions and problems. The review questions are intended to help you assess your understanding of the major concepts and lan­ guage features covered in the chapter. The answers to these questions appear in Appendix A. The problems are intended to give you practice in developing solu­ tions that use the top-down structured method. They will focus on the concepts and language features covered in the chapter. Solutions to the odd-numbered problems appear in Appendix B. Program Diskette for The Book A program disk is available containing all of the Example Programs and many of the programs in the Exploratory Activities. This disk will allow you to load these programs from diskette rather than taking the time and effort to type them in yourself. Each disk costs $5.00. For further information, please contact the Computer Tutors, P.O. Box 3709, Saratoga, CA 95070. CHAPTER 1 PROGRAMMING ENVIRONMENT A careful reading of the following objectives will give you an overview of the material in this chapter. Keeping the objectives firmly in mind, you can proceed through the chapter with confidence. OBJECTIVES Making a Work Disk You will learn how to make a copy of the Turbo BASIC Program Disk to use as your working diskette. Booting up Turbo BASIC You will learn how to enter the Turbo BASIC programming environment. Using Your Computer You will learn how to work with the Turbo BASIC programming environment to enter and run programs. 6 Programming Environment 1—1 EXPLORATORY ACTIVITIES Making a Working Diskette You should always make working copies of software packages so that the original master copy of the software can be used as a backup. If you already have a working copy of Turbo BASIC, go on to the next section. If you have a hard disk drive, consult Chapter 2 of the Turbo BASIC Owner's Handbook. If you have a system with one disk drive, consult the DOS reference manual on formatting and copying diskettes. To make a working copy of the Turbo BASIC Program Disk that comes with the Turbo BASIC package, place the DOS 2.0 (or later) diskette in drive A and turn on the computer (the switch is at the right rear of the cabinet on most computers). Turn on the monitor, if necessary. You may not see anything displayed on the screen for 30 to 60 seconds. If your computer asks for the date and time, enter them in the form requested or press the key marked [*-'[ twice if you do not wish to enter them. An A> should appear on the screen. The blinking underscore (_) next to the A> is called the cursor. Notice that the slash symbol (/) is located on the same key with the question mark symbol (?) on it. Now type format b:/s Note that the only space is between format and b. Now press I*—1!· The key marked \+~*\is also referred to as the Enter key. Follow the directions displayed on the screen by placing a blank diskette in drive B and press I*—1). The formatting process will take a minute or so. When this process is complete, type N and replace the DOS diskette in drive A with the Turbo BASIC Program Disk. To place copies of the files you will need on your working disk, type copy a:tb.exe b: and press |*—'[. When this process is complete (in about 30 seconds), type the following lines. Press I*—1! at the end of the first line and wait for the copy process to end before typing the second line and pressing I*—1!. copy a:tbhelp.tbh b: copy a:hanoi.bas b: Remove the Turbo BASIC Program Disk and store it in a safe place. Prepare a label with the title "Turbo BASIC Work Disk." Remove the diskette in drive B and place this label on it.

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.