Table Of ContentIntroduction 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....................................................... 139
7.9 Exercises ........................................................................... 139
7.10 Technical Terms .............................................................. 142
8 PIXEL GRAPHICS...................................................................... 145
8.1 Pixel Positions in the Execution Window........................... 146
8.2 Plotting Dots in the Execution Window.............................. 147
8.3 Changing the Execution Window Size............................... 149
8.4 Drawing Lines.................................................................... 150
8.5 Drawing Circles and Ellipses.............................................. 152
8.6 Animation........................................................................... 153
8.7 Drawing Arcs...................................................................... 154
8.8 Plotting a Mathematical Function....................................... 155
8.9 Using Text with Pixel Graphics........................................... 156
8.10 Background Color............................................................ 157
8.11 Sound with Graphics........................................................ 157
8.12 Current Values of Graphic Parameters............................ 158
8.13 Exercises ......................................................................... 158
8.14 Technical Terms .............................................................. 161
9 SELECTION............................................................................... 163
9.1 Simple Selection................................................................ 164
9.2 Three-way Selection.......................................................... 166
9.3 Multi-way Selection............................................................ 168
9.4 Case Construct.................................................................. 170
9.5 Commands for Action ........................................................ 172
9.6 Selecting from a Menu of Commands................................ 173
9.7 Exercises ........................................................................... 174
9.8 Technical Terms ................................................................ 176
Table of Contents v
10 STORING DATA ON THE DISK..................................................179
10.1 Data Files on Disk.............................................................180
10.2 Input Data from Disk Files................................................181
10.3 End-of-file for Data ...........................................................182
10.3.1 End-of-file with Strings...................................184
10.4 Reading Lines of Text from a File.....................................184
10.5 Exercises..........................................................................185
10.6 Technical Terms...............................................................187
11 HANDLING STRINGS.................................................................191
11.1 Length of a String.............................................................192
11.2 Joining Strings Together...................................................193
11.3 Selecting Part of a String..................................................194
11.4 Searching for a Pattern in a String....................................197
11.4.1 Counting Patterns in a Word.........................199
11.5 Substituting One Pattern for Another................................200
11.6 Eliminating Characters from Strings.................................201
11.7 Bullet-Proofing Programs..................................................203
11.8 Exercises..........................................................................204
11.9 Technical Terms...............................................................208
12 PROCESSING TEXT..................................................................211
12.1 Token-oriented Input ........................................................212
12.2 Inputting a Fixed Number of Characters...........................213
12.3 Line-oriented Input............................................................215
12.4 Files on Disk.....................................................................216
12.5 Reading one File and Writing Another..............................218
12.6 Text Formatting.................................................................220
12.7 Simple Language Translation...........................................221
12.8 Exercises..........................................................................222
12.9 Technical Terms...............................................................225
13 PROGRAM STRUCTURE...........................................................227
13.1 Structure Diagrams...........................................................228
13.2 Nested Structures.............................................................229
13.2.1 A Loop Nested Inside a Loop........................230
13.2.3 More Complicated Nesting of Structures.......231
13.3 Structure Diagram for elsif................................................232
vi Introduction to Programming in Turing
13.4 Declaration of Variables and Constants Inside Constructs235
13.5 Design of Programs......................................................... 236
13.5.1 Controlling Complexity.................................. 237
13.6 Exercises ......................................................................... 238
13.7 Technical Terms .............................................................. 239
14 ARRAYS AND OTHER DATA TYPES........................................ 241
14.1 Array Data Types............................................................. 242
14.2 Manipulating Lists............................................................ 243
14.3 When to Use an Array...................................................... 244
14.4 Initialization of Arrays....................................................... 246
14.5 Sorting an Array............................................................... 246
14.6 Related Lists.................................................................... 248
14.7 Subrange Data Types...................................................... 250
14.8 Boolean Data Types ........................................................ 250
14.9 Tables.............................................................................. 252
14.10 Named Data Types........................................................ 254
14.11 Exercises ....................................................................... 255
14.12 Technical Terms ............................................................ 260
15 MUSIC........................................................................................ 263
15.1 Playing Musical Notes...................................................... 264
15.1.2 Resting for a While ....................................... 266
15.2 Playing a Series of Notes................................................. 266
15.3 Using the Keyboard to Make Music ................................. 266
15.4 Animation with Music....................................................... 268
15.5 Exercises ......................................................................... 272
15.6 Technical Terms .............................................................. 273
16 SUBPROGRAMS........................................................................ 275
16.1 Functions ......................................................................... 276
16.1.1 Predefined Functions.................................... 276
16.1.2 Type Transfer Functions............................... 278
16.1.3 User-created Functions................................. 279
16.1.4 A String-valued Function............................... 282
16.2 A Procedure with No Parameters..................................... 283
16.3 A Procedure with One Parameter.................................... 286
16.4 Variable Parameters in Procedures................................. 288
Table of Contents vii
16.4.1 Procedures to Bullet-proof Input....................289
16.5 Predefined Procedures and Functions.............................290
16.6 Recursive Subprograms...................................................292
16.7 Functions versus Procedures...........................................293
16.8 Exercises..........................................................................294
16.9 Technical Terms...............................................................296
17 SUBPROGRAMS WITH ARRAY PARAMETERS.......................299
17.1 Functions with Array Parameters......................................300
17.2 Array Parameters in Procedures ......................................301
17.3 Dynamic Formal Parameters............................................303
17.3.1 Another Example of a Procedure ..................304
17.3.2 An Example Using a Function and Procedures306
17.4 Local and Global Variables and Constants.......................308
17.5 Maintaining a Sorted List..................................................311
17.6 Exercises..........................................................................315
17.7 Technical Terms...............................................................317
18 RECORDS AND FILES...............................................................319
18.1 Declaration of Records.....................................................320
18.2 Inputting and Outputting Records.....................................321
18.3 Arrays of Records.............................................................322
18.4 Binding to Records...........................................................323
18.5 An Example using a File of Records.................................323
18.6 Moving Records in Memory..............................................326
18.7 Text Files..........................................................................329
18.8 Binary Files.......................................................................329
18.9 Random Access to Records on Disk................................331
18.9.1 An Example of Random Access to a Binary File331
18.10 Modification of Records on Disk.....................................334
18.11 Deletion of Records on Disk...........................................343
18.12 Exercises........................................................................345
18.13 Technical Terms.............................................................346
19 ADVANCED TOPICS..................................................................349
19.1 Binary Search...................................................................350
19.2 Sorting by Merging............................................................353
19.3 Files of Records in Linked Lists........................................355
viii Introduction to Programming in Turing
19.4 Highly Interactive Graphics.............................................. 359
19.5 Exercise........................................................................... 363
19.6 Technical Terms .............................................................. 363
20 ADVANCED PIXEL GRAPHICS................................................. 365
20.1 Advanced Graphics Concepts.......................................... 366
20.2 Drawing a Tilted Box........................................................ 366
20.3 Repeating a Pattern......................................................... 367
20.4 Animation Using a Buffer................................................. 370
20.5 Bar Charts........................................................................ 372
20.6 Pie Charts........................................................................ 374
20.7 Graphing Mathematical Equations................................... 376
20.8 Exercises ......................................................................... 379
20.9 Technical Terms .............................................................. 380
21 ANIMATION AND GUIS.............................................................. 381
21.1 The Mouse in Turing........................................................ 382
21.2 Animation using the Pic Module....................................... 386
21.3 Animation using the Sprite Module................................... 391
21.4 The GUI Module............................................................... 395
21.5 Playing Music from Sound Files....................................... 404
21.6 Exercises ......................................................................... 408
21.7 Technical Terms .............................................................. 409
APPENDICES..................................................................................... 413
Appendix A:Simplified Turing Syntax...................................... 414
Appendix B:Predefined Subprograms..................................... 423
Appendix C:Predefined Subprograms by Module ................... 431
Appendix D:Reserved Words.................................................. 446
Appendix E:Operators............................................................. 447
Appendix F: File Statements.................................................... 450
Appendix G:Control Constructs............................................... 451
Appendix H:Colors in Turing.................................................... 452
Appendix I: Keyboard Codes for Turing.................................. 453
Appendix J: ASCII Character Set............................................ 455
Appendix K:Glossary............................................................... 456
INDEX................................................................................................. 472