Kemp / Hahn | Pascal for Students (including Turbo Pascal) | E-Book | www2.sack.de
E-Book

E-Book, Englisch, 384 Seiten

Kemp / Hahn Pascal for Students (including Turbo Pascal)


3. Auflage 1995
ISBN: 978-0-08-092870-8
Verlag: Elsevier Science & Techn.
Format: EPUB
Kopierschutz: 6 - ePub Watermark

E-Book, Englisch, 384 Seiten

ISBN: 978-0-08-092870-8
Verlag: Elsevier Science & Techn.
Format: EPUB
Kopierschutz: 6 - ePub Watermark



The third edition of this best-selling text has been revised to present a more problem oriented approach to learning Pascal, without substantially changing the original popular style of previous editions. With additional material on Turbo Pascal extensions to the standard Pascal, including binary files and graphics, it continues to provide an introduction which is as suitable for the programming novice as for those familiar with other computer languages.

Brian Hahn was a professor in the Department of Mathematics and Applied Mathematics at the University of Cape Town. In his career, Brian wrote more than 10 books for teaching programming languages to beginners.
Kemp / Hahn Pascal for Students (including Turbo Pascal) jetzt bestellen!

Autoren/Hrsg.


Weitere Infos & Material


1;Front Cover
;1
2;Pascal for Students: Including Turbo Pascal
;2
3;Copyright Page
;3
4;Table of Contents;8
5;Preface to the first edition;4
6;Preface to the second edition;5
7;Preface to the third edition;6
8;Introduction;14
9;Section 1: Pre-defined simple types and control structures;16
9.1;Chapter 1. First steps;18
9.1.1;Introduction;18
9.1.2;1.1 Some simple programs;18
9.1.3;1.2 Constructing a program;25
9.1.4;1.3 Program errors and debugging;32
9.1.5;1.4 Program testing;33
9.1.6;Summary;34
9.1.7;Solutions to quick quizzes;35
9.1.8;Exercises;36
9.2;Chapter 2. Comparisons and characters;39
9.2.1;Introduction;39
9.2.2;2.1 Arithmetic comparisons;39
9.2.3;2.2 Boolean variables and functions;41
9.2.4;2.3 Two programs using Boolean variables;43
9.2.5;2.4 Characters;46
9.2.6;Summary;51
9.2.7;Solutions to quick quizzes;51
9.2.8;Exercises;52
9.3;Chapter 3. Repetition;54
9.3.1;Introduction;54
9.3.2;3.1 The repeat statement;54
9.3.3;3.2 The whi Ie statement;59
9.3.4;3.3 Developing a program;64
9.3.5;3.4 The for statement;69
9.3.6;3.5 Use of eoln and eofin repetition statements;70
9.3.7;Summary;71
9.3.8;Solutions to quick quizzes;72
9.3.9;Exercises;74
9.4;Chapter 4. Conditional execution and transfers;76
9.4.1;Introduction;76
9.4.2;4.1 The if statement;76
9.4.3;4.2 The case statement;85
9.4.4;4.3 The goto statement;88
9.4.5;4.4 Data validation;91
9.4.6;Summary;96
9.4.7;Solutions to quick quizzes;97
9.4.8;Exercises;100
9.5;Chapter 5. Subprograms;102
9.5.1;Introduction;102
9.5.2;5.1 Functions;102
9.5.3;5.2 Procedures;107
9.5.4;5.3 Value and variable parameters;112
9.5.5;5.4 Further features of subprograms#;117
9.5.6;Summary;123
9.5.7;Solutions to quick quizzes;123
9.5.8;Exercises;126
10;Section 2: Structured and enumerated data types;128
10.1;Chapter 6. Arrays;130
10.1.1;Introduction;130
10.1.2;6.1 Arrays of scalars;130
10.1.3;6.2 Sorting and searching;135
10.1.4;6.3 Manipulating complete arrays;141
10.1.5;6.4 Arrays of arrays;147
10.1.6;Summary;154
10.1.7;Exercises;154
10.2;Chapter 7. User-defined values, subranges and record structures;156
10.2.1;Introduction;156
10.2.2;7.1 Enumerated types;156
10.2.3;7.2 Subrange;165
10.2.4;7.3 Records;165
10.2.5;7.4 Arrays of records;167
10.2.6;7.5 Record variants#
;178
10.2.7;Summary;180
10.2.8;Exercises;180
10.3;Chapter 8. Files;181
10.3.1;Introduction;181
10.3.2;8.1 Defining a sequential file;181
10.3.3;8.2 Initialising and writing to a file;182
10.3.4;8.3 Reading from a file;185
10.3.5;8.4 File update;188
10.3.6;8.5 Textfiles;193
10.3.7;8.6 Input and output;196
10.3.8;Summary;198
10.3.9;Exercises;198
10.4;Chapter 9. Pointer structures;200
10.4.1;Introduction;200
10.4.2;9.1 Lists and pointers;200
10.4.3;9.2 Ordered lists;206
10.4.4;9.3 Recursion revisited;209
10.4.5;9.4 Binary search trees;213
10.4.6;9.5 Other pointer structures;218
10.4.7;Summary;218
10.4.8;Exercises;218
10.5;Chapter 10. Set structures and tackling a larger problem;220
10.5.1;Introduction;220
10.5.2;10.1 Sets;220
10.5.3;10.2 Getting it together;224
10.5.4;Summary;238
10.5.5;Exercises;238
11;Section 3: Turbo Pascal;240
11.1;Chapter 11. General Turbo Pascal extensions and exceptions;242
11.1.1;11.1 General syntax;242
11.1.2;11.2 Types;244
11.1.3;11.3 String type;246
11.1.4;11.4 Expressions;250
11.1.5;11.5 Standard input and output;251
11.1.6;11.6 Statements;253
11.1.7;11.7 Procedures and functions;253
11.1.8;11.8 Memory matters;254
11.1.9;11.9 Units;255
11.1.10;11.10 The system unit;257
11.1.11;11.11 The crt unit;259
11.1.12;11.12 The dos unit;264
11.1.13;11.13 Compiler directives;264
11.1.14;Summary;265
11.2;Chapter 12. Turbo Pascal files;266
11.2.1;12.1 Text files;266
11.2.2;12.2 Random access with typed files;273
11.2.3;12.3 Untyped files;276
11.2.4;12.4 Device files;278
11.2.5;Summary;279
11.3;Chapter 13. Turbo Pascal graphics;280
11.3.1;13.1 Some basics;280
11.3.2;13.2 myworld: a world coordinate graphics unit;287
11.3.3;13.3 Fractals;295
11.3.4;13.4 Some other graphics goodies;298
11.3.5;Summary;298
11.3.6;Exercises;299
11.4;Section 4: Mathematical applications of Turbo Pascal;302
11.5;Chapter 14. Simulation;304
11.5.1;14.1 Random number generation;304
11.5.2;14.2 Spinning coins;305
11.5.3;14.3 Rolling dice;305
11.5.4;14.4 A random walk;306
11.5.5;14.5 Traffic flow;307
11.5.6;Summary;310
11.5.7;Exercises;310
11.6;Chapter 15. Matrices and their applications;312
11.6.1;15.1 Matrices;312
11.6.2;15.2 Reachability of spies;314
11.6.3;15.3 Leslie matrices: population growth;317
11.6.4;15.4 Markov chains;319
11.6.5;15.5 Solution of linear equations;322
11.6.6;Summary;324
11.6.7;Exercises;324
11.7;Chapter 16. Introduction to numerical methods;326
11.7.1;16.1 Equations;326
11.7.2;16.2 Integration;330
11.7.3;16.3 Numerical differentiation;332
11.7.4;16.4 First-order differential equations;333
11.7.5;16.5 Runge-Kutta methods;337
11.7.6;16.6 A differential equation modelling package;338
11.7.7;16.7 Partial differential equations: a tridiagonal system;344
11.7.8;Summary;348
11.7.9;Exercises;348
12;Appendices;350
12.1;A: Syntax diagrams for Pascal;350
12.2;B: Reserved words and required identifiers;361
12.3;C: Required functions and procedures;362
12.4;D: Turbo Pascal functions and procedures;364
12.4.1;D.1 system unit;364
12.4.2;D.2 crt unit;365
12.4.3;D.3 dos unit;366
12.4.4;D.4 graph unit;366
12.5;E: Common codes;368
12.6;F: Portability;369
12.7;G: Glossary;373
13;Index;379


1

First steps


Introduction


The very simplest kinds of computer program are introduced in this chapter. Even so there is much to learn—the basic structure of Pascal programs, how to feed in data and layout results, but most of all how to get the computer to do what you want it to and find out what’s going wrong when it doesn’t.

1.1 Some simple programs


Computers, like the Delphic Oracle, are used to answer questions, but, as with the Oracle, you have to be careful how you phrase your request or you may get a misleading answer. In fact the computer may give you no answer at all if it doesn’t like the question.

The first question we shall ask the computer is: ‘given that a stone takes 3 seconds to reach the bottom of a well how deep is the well?’ It would be nice if we could phrase the question exactly in this way and expect the computer to get the right answer. It will be a few years before that becomes possible. In the meantime the best way to get the computer to provide an answer is to write a computer program to do the job. Program 1.1 does just this. Before we examine the program in detail have a preliminary look at it to see if you can work out what’s happening.

Program 1.1

Ignoring much of the punctuation for now let’s look at the program line by line. (Note that the line numbers that appear on the left-hand side are for reference only and are not part of the program.)

On the first line the program is given a title. The user chooses an appropriate name but, as with all Pascal names, there must be no break in the middle—Well depth would not work. The name output in parentheses, signifies that the answer that the computer gives should be transmitted to the device called output. This might be a printer*, vdu* or teletype*. Lines 3 to 10 contain a description of the purpose of the program. This description enclosed between braces ‘{’ and ‘}’ is known as a In all programs, whatever the language, it is important to have a piece of text at or near the beginning describing the purpose of the program and, possibly, giving other information such as the name of the writer and the date of completion. In Pascal, any information enclosed between ‘{’ and ‘}’ is disregarded by the compiler* (even if it extends over several lines) and so explanatory text may be inserted at appropriate points#. In this example the rectangle of asterisks has no significance for the computer but helps to highlight the enclosed introductory description for the reader.

A blank line follows, and then, on lines 13 and 14, the constants to be used in the program are given values. In order to work out the depth of the well the computer needs to know how long a stone takes to reach the bottom and also how fast it is moving. The time taken was 3 seconds and a name timetaken has been invented to represent this. Note, again, that it would not be acceptable to have a name in two parts (time taken). There must be no spaces in the middle. The second constant 9.8 is given the name gravity. The stone actually falls because of the gravitational pull of the earth and if we are to determine the depth of the well this will be needed too.

After the next blank line, the word var (short for ‘variable’) indicates that the name following it denotes a variable in the program. The word that follows is depth and represents the depth of the well. At this point you may be wondering why depth is called a variable giving the impression that it is something that alters. Surely the depth remains unchanged in the same way that the time taken and the gravitational constant do? The distinction occurs because we know the values of the constants before we start but the value of the depth is to be calculated in the program itself. In Pascal any names used to denote values that are computed during the running of a program are designated as variables.

The fact that the depth will be a number which will probably contain a fractional part is indicated by placing the word real after the variable name.

None of the code up to line 17 actually commands the computer to carry out the task we are interested in. However, the directions on lines 12 to 17 are important to the organisation of information within the computer and to the execution of the code contained between the words begin and end in the ‘statement part’.

The code on line 21 computes the depth from the given information using a formula. The order of the elements on the line is misleading since it does not correspond to the order in which the computations are performed. First, the formula to the right of the ‘: = ‘is evaluated using the values defined earlier in the program (note that multiplication is denoted by an asterisk and not by the more usual ‘×’ symbol and that sqr indicates that the squaring operation is to be performed). The result is then assigned to the variable depth on the left-hand side. The assignment operator ‘: = ‘may be read as ‘becomes’ or ‘takes the value’.

The complete instruction on this line is called an and tells the computer to halve the value of the gravitational constant, multiply this by the square of the time and assign the result to the variable called depth. When this instruction is executed the value of depth becomes approximately 44.1 (the result of 0.5 × 9.8 × 3 × 3)—this will be the computer’s estimate of the depth of the well in metres. The actual precision of the result will vary from computer to computer. Note that the symbol ‘=’ is not used for assignment as it was for giving values to constants. The link between a variable and its value is a looser attachment and we will return to this point later.

Even though the result has been computed, this is not the end of the problem: the value obtained will still be inside the computer. In order to inform the user of the result, the value must be transmitted to the output unit* and printed. The instructions on lines 23 to 29 tell the computer to send the result and other information to the printer.

When a writeln statement is carried out it has the effect of sending specified information to one line of the output device. The statement on line 23 has the effect of printing one item—a piece of text that is to be the title of the output. The second writeln causes another piece of text to be output on the next line, underlining the title. The word writeln on line 25 has no items in parentheses following it and simply causes a blank line to be output.

The statement on line 27 indicates that three items are to be output—a piece of text between quotation marks (quotes), the of the constant timetaken and then another piece of text. If an item is not in quotes then its value is output rather than the actual word itself. The last two writeln statements will similarly cause three items to be printed—two pieces of text and a value.

Now you know the program does but you may be wondering it is written in this elaborate way. The result could be worked out with a calculator in much less time than it takes to read the program and certainly much less time than it takes to run it. There are two answers to this. Firstly, the program is purely illustrative and is included to show what a simple one looks like. Secondly, as we shall see later, the program needs only a slight modification to make it much more powerful.

Even for the task that it does you may feel that the program is too verbose. Certainly it could be shortened in several ways so let’s examine each of these possible short cuts in turn. Before we do this though, let’s be clear what the purpose of a program is. Principally it is for getting the computer to solve a problem. A small program may only take a few minutes to write but as you become more ambitious you may write programs that have hundreds or even thousands of lines and take several weeks or months to complete. It is quite likely that unless your program is carefully written and has adequate documentation* you will, over a period of time, forget what different parts of it are supposed to do and changes will therefore be very difficult to make. If you write software for a living then you will probably be writing programs that others have to amend and also modifying other people’s programs. For these reasons it is important that as you write your program you spend as much time as possible making it easy to follow.

The first way the program could be shortened would be to remove those comments enclosed between ‘{’ and ‘}’. Hopefully you will agree now that this is not a good idea. They don’t make the program any less efficient and are of great assistance in helping the reader to find out what the program is supposed to do and in explaining the purpose of the names used.

What about those names though? Surely it would be possible to get rid of most of them. The assignment statement could then be abbreviated to

or (since everyone knows what 3 × 3 gives)

The usefulness of constant names like gravity and timetaken is not so obvious as that of the comments. In fact, in part, they fulfil a similar purpose,...



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.