Table Of ContentPascal
at Work and Play
Pascal
at Work and Play
AN INTRODUCTION TO
COMPUTER PROGRAMMING IN PASCAL
RICHARD S. FORSYTH
Polytechnic of North London
Springer-Science+Business Media, B.Y.
© 1982 Richard Forsyth
Originally published by Chapman & Hall in 1982
ISBN 978-0-412-23380-7 ISBN 978-1-4899-3061-3 (eBook)
DOI 10.1007/978-1-4899-3061-3
This title is available in both hardbound and paperback editions. The
paperback edition is sold subject to the condition that it shall not, by way
of trade or otherwise, be lent, re-sold, hired out, or otherwise circulated
without the publisher's prior consent in any form of binding or cover other
than that in which it is published and without a similar condition including
this condition being imposed on the subsequent purchaser.
All rights reserved. No part of this book may be reprinted, or reproduced or
utilized in any form or by any electronic, mechanical or other means, now
known or hereafter invented, including photocopying and recording, or in
any information storage and retrieval system, without permission in writing
from the publisher.
Library of Congress Cataloging in Publication Data
Forsyth, Richard S.
Pascal at work and play.
Bibliography: p.
Includes index.
l. PASCAL (Computer program language)
I. Title.
QA76.73.P2F67 1982 00l.64'24 82-4515
ISBN 978-0-412-23380-7 AACR2
British Library Cataloguing in Publication Data
Forsyth, Richard S.
Pascal at work and play.
l. PASCAL (Computer program language)
I. Title
00l.64'24 QA 76.73.P2
ISBN 978-0-412-23380-7
Contents
Preface page ix
Acknowledgements x
Part 1 PASCAL AT LARGE
Introduction 3
1 Writing programs 6
1.1 High level languages 6
1.2 Algorithms and programs 10
1.3 Flowcharting 10
1.4 Errors - the calm approach 14
2 A preview of Pascal 16
2.1 A simple example [BIRTHDAY] 16
2.2 Syntax diagrams 23
2.3 Program structure 26
2.4 A word in edgeways 27
3 Declarations and types 29
3.1 Information and its representation 29
3.2 Constants 30
3.3 Variables 31
3.4 The fundamental data types 32
4 Assignment and expressions 36
4.1 Operators and operands 36
4.2 Precedence and brackets 37
4.3 Types of expression 39
4.4 Standard functions 40
5 Simple input/output 43
5.1 READ and WRITE 43
5.2 READLN and WRITELN 46
5.3 Example program [INFLATER] 47
vi CONTENTS
5.4 Exercises 51
5.5 Quiz 52
6 Looping and grouping 55
6.1 Control structures 55
6.2 Selection 56
6.3 Repetition 61
6.4 Embedding 64
6.5 Semicolons 65
6.6 The dreaded GOTO 66
6.7 Example program [DECLINE] 67
6.8 Exercises 70
7 Procedures and functions 72
7.1 Procedures
72
7.2 Parameters 75
7.3 Local variables 77
7.4 Functions 78
7.5 Recursion 79
7.6 Example program [CHANCES] 80
7.7 Exercises 84
8 Arrays 86
8.1 Array declaration 86
8.2 An array application 87
8.3 Vectors and matrices 90
8.4 Arrays of characters 93
8.5 Array of hope 95
8.6 Example program [SHOWOFF] 96
8.7 Exercises 102
9 Files 105
9.1 Types of file 105
9.2 Sequential files 106
9.3 Example program [SEARCHER] 112
9.4 Exercises 122
10 Sets and records 127
10.1 Sets and set operations 127
10.2 The use of records 133
10.3 Example program [BIGDEAL] 138
10.4 Exercises 147
11 Advanced topics 149
11.1 Passing functions as parameters 149
CONTENTS vii
11.2 Variant records 152
11.3 Dynamic data structures 155
11.4 Example program [CHOPPER] 157
12 Programming practice 165
12.1 Program design 166
12.2 Debugging 171
12.3 Design faults in Pascal 174
12.4 Projects 176
Part 2 PASCAL AT WORK 179
13 Case study 1 (Sorting) 181
13.1 Sorting mes 181
13.2 The classic four-tape sort 182
13.3 A program of sorts 184
13.4 Discussion 192
14 Case study 2 (Finding the shortest path) 197
14.1 The urban transit problem 197
14.2 The A-star algorithm 198
14.3 The route finder 200
14.4 Data representation 201
14.5 User interface 206
14.6 The program itself 207
14.7 Concluding remarks 224
Part 3 PASCAL AT PLAY 231
IS Case study 3 (Football simulation) 233
15.1 Computer games 233
15.2 Soccer simulation 234
15.3 The football program 239
15.4 Suggested improvements 251
16 Case study 4 (Go-Moku) 254
16.1 The Pygmalion factor 255
16.2 Minimax look-ahead 255
16.3 The Go-Moku program 257
16.4 The next step 273
Part 4 PASCAL AT A GLANCE 281
Appendix A ASCII code 283
Appendix B Basic Pascal facts 285
viii CONTENTS
Appendix C Catalogue of software 289
Appendix D Syntax diagrams 291
References 300
Solutions to selected exercises 302
Index 322
Preface
This is both a first and a second level course in Pascal. It starts at an elementary
level and works up to a point where problems of realistic complexity can be tackled.
It is aimed at two audiences: on the one hand the computer professional who has a
good knowledge of Cobol or Fortran but needs convincing that Pascal is worth
learning, and on the other hand the amateur computer enthusiast who may have a
smattering of Basic or may be an absolute beginner.
Its approach is based on two principles that are not always widely recognized.
The first is that computing is no longer a specialist subject. In the early days
of computing a priesthood arose whose function was to minister to those awesome,
and awesomely expensive, machines. Just as in the ancient world, when illiteracy
was rife, the scribes formed a priestly caste with special status, so the programmers
of yesteryear were regarded with reverence. But times are changing: mass computer
literacy is on its way. We find already that when a computer enters a classroom it
is not long before the pupils are explaining the finer points of its use to their
teacher - for children seem to have greater programming aptitude than adults.
This book, it is hoped, is part of that process of education by which the computer
is brought down to earth; and therefore it attempts to divest computing of the
mystique (and deliberate mystification) that still tends to surround the subject.
The other principle is that the second best way to achieve competence as a
programmer is to read non-trivial programs and see how they work. (The best way
is of course to write programs, and plenty of them.) So a large proportion of this
book is taken up by full descriptions and listings of four good-sized programs that
are far removed from the toy examples sometimes shown in programming textbooks.
This aspect, based on the fact that people learn by example, is too often neglected
by authors on programming, who tend to feel they have done their duty once they
have presented the rules of the language.
Pascal has been chosen because it is elegant enough to appeal to computer
scientists and professional programmers while still being simple enough to be
taught to almost anyone who is genuinely interested in programming. It is also
compact enough to run on many microcomputers - those Volkswagens of the
computer age. Indeed there are signs that Pascal will become the lingua franca
of the coming 'computer revolution' in schools, businesses and the home. This
book is for anyone who wants to join that revolution.
October 1981 Richard Forsyth
Acknowledgements
Various people have contributed in various ways to the making of this book. My
debt to other published authors is acknowledged in the Bibliography. Here I would
just like to thank the following people for their advice and assistance. (The order is
purely alphabetic.)
Maureen Ashman
Ashesh Datta
Cyril Drimer
Mei Lynn Forsyth
Tim Harris
Sallahe Hassan
Jeff Hillmore
Declan Kelly
Joan New
Maria Panayi
Therese Rieul
Bill Tuck
I am also grateful to the Polytechnic of North London Computer Service,
without whose co-operation this book could not have been written.
Description:This is both a first and a second level course in Pascal. It starts at an elementary level and works up to a point where problems of realistic complexity can be tackled. It is aimed at two audiences: on the one hand the computer professional who has a good knowledge of Cobol or Fortran but needs con