Introduction to Programming in Turing
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Programming in Turing First Edition - Second Printing J. N. P. Hume Holt Software Associates, Inc. Toronto, Canada © 2001 by the author Toronto, Ontario, Canada All rights reserved. No part of this book may be reproduced, in any way or by any means, without permission of the author. Publisher: HOLT SOFTWARE ASSOCIATES INC. 203 College St., Suite 305 Toronto, Ontario, Canada M5T 1P9 (416) 978-6476 1-800-361-8324 http://www.holtsoft.com ISBN: 0-921598-42-4 First Edition - Second Printing Printed in Canada by the University of Toronto Press Table of Contents PREFACE ............................................................................................. IX ACKNOWLEDGMENTS....................................................................... XV 1 COMPUTING ESSENTIALS ...........................................................1 1.1 Introduction.............................................................................2 1.2 A Brief History of Computer Hardware ...................................2 1.3 A Brief History of Programming ..............................................7 1.3.1 A New Way of Organizing Large Programs.......13 1.4 What is a Computer?............................................................14 1.4.1 The Central Processing Unit..............................15 1.4.2 Memory..............................................................16 1.4.3 Output Devices ..................................................18 1.5 Number Systems: Decimal and Binary .................................18 1.6 Hardware and Networks .......................................................21 1.6.1 Different Kinds of Computers for Different Needs24 1.6.2 The Silicon Chip ................................................25 1.7 Software ...............................................................................27 1.7.1 Operating Systems ............................................27 1.7.2 Programming Environments ..............................27 1.7.3 Applications .......................................................28 1.8 The Social Impact of Computers ..........................................30 1.8.1 Employment.......................................................31 1.8.2 Privacy...............................................................32 1.8.3 Access to Information ........................................32 1.8.4 Leisure Activities................................................34 1.8.5 Illegal Activity.....................................................34 1.8.6 Computers: Good or Bad?.................................35 1.9 Exercises ..............................................................................36 1.10 Technical Terms .................................................................38 ii Introduction to Programming in Turing 2 THE TURING ENVIRONMENT .................................................... 43 2.1 Introduction .......................................................................... 44 2.2 The Editor Window............................................................... 44 2.2.1 Cut, Copy, and Paste........................................ 46 2.2.2 Undo ................................................................. 47 2.3 Saving Programs on Disk..................................................... 47 2.4 Running Programs ............................................................... 49 2.4.1 Input/Output Redirection ................................... 51 2.5 Indenting Programs and Syntax Coloring............................. 53 2.5.1 Indenting Programs........................................... 53 2.5.2 Syntax Coloring................................................. 53 2.6 Starting and Stopping the Environment ............................... 54 2.7 Opening an Existing Program .............................................. 55 2.8 Searching for Text in a File .................................................. 56 2.8.1 Finding Text in a File......................................... 57 2.8.2 Replacing Text in the File ................................. 58 2.9 Printing a Program ............................................................... 59 2.10 Example Turing Programs ................................................. 60 2.11 Exercises ........................................................................... 63 2.12 Technical Terms ................................................................ 64 3 PROGRAM DESIGN AND STYLE................................................ 66 3.1 Programming and Programmers.......................................... 67 3.2 Programming Style............................................................... 68 3.3 The Software Development Process.................................... 70 3.4 Procedure-Oriented Programming ....................................... 74 3.5 Exercises ............................................................................. 75 3.6 Technical Terms .................................................................. 76 4 SIMPLE PROGRAMS................................................................... 77 4.1 A One-Line Program ............................................................ 78 4.2 Changing the Program......................................................... 78 4.3 Substituting One String of Characters for Another............... 79 4.4 A Program that Computes ................................................... 79 4.5 Integers and Real Numbers ................................................. 80 4.6 Arithmetic Expressions......................................................... 81 4.7 Combining Calculations and Messages ............................... 82 4.8 Output of a Series of Items .................................................. 82 Table of Contents iii 4.9 A Series of Output Statements .............................................83 4.10 Exercises ............................................................................84 4.11 Technical Terms .................................................................86 5 VARIABLES AND CONSTANTS...................................................87 5.1 Storing Information in the Computer.....................................88 5.2 Declaring Variables...............................................................88 5.2.1 Names of Variables ...........................................89 5.3 Inputting Character Strings ...................................................89 5.3.1 Strings Containing Blanks..................................90 5.4 Mistakes in Programs ...........................................................91 5.5 Inputting Numbers ................................................................92 5.5.1 Mistakes in Data ................................................92 5.6 Inputting Real Numbers........................................................93 5.7 Constants .............................................................................94 5.8 Assignment of Values to Variables .......................................95 5.9 Understandable Programs....................................................96 5.10 Comments in Programs......................................................97 5.11 Exercises ............................................................................98 5.12 Technical Terms ...............................................................100 6 REPETITION...............................................................................103 6.1 Loops..................................................................................104 6.2 Conditional Loops...............................................................105 6.2.1 Comparisons ...................................................106 6.2.2 Comparing Strings...........................................107 6.2.3 An Example Conditional Loop .........................108 6.2.4 Another Conditional Loop ................................110 6.3 Counted Loops ...................................................................111 6.3.1 Counting in Multiples .......................................113 6.3.2 Indenting the Body of Loops............................114 6.3.3 Loops that Count Backwards...........................114 6.3.4 Counted Loop Examples .................................115 6.3.5 Counted Loops with Exits ................................117 6.4 Random Exit from Loop......................................................118 6.5 Compound Conditions ........................................................119 6.6 Exercises ............................................................................120 6.7 Technical Terms .................................................................124 iv Introduction to Programming in Turing 7 CHARACTER GRAPHICS.......................................................... 127 7.1 Character Locations in the Execution Window................... 128 7.2 Creating a Graphical Pattern with Characters.................... 129 7.2.1 Interactive Graphics ........................................ 130 7.2.2 Diagonal Lines and Patterns........................... 131 7.3 Drawing in Color................................................................. 133 7.4 Background Color .............................................................. 135 7.5 Hiding the Cursor ............................................................... 136 7.6 Animation with Graphics .................................................... 137 7.7 Controlling the Speed of Animation ................................... 138 7.8 Pausing for User Input ......................................................