100%(1)100% found this document useful (1 vote) 303 views312 pagesPascal Programing
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Programming
FundamentalsALLIED PUBLISHERS LIMITED
Regd. Off. : 15 J.N. Heredia Marg, Ballard Estate, Mumbai 400001
Prarthna Flats (2nd Floot), Navrangpura, Ahmedabad 380009
3-2-844/6 & 7 Kachiguda Station Road, Hyderabad 500027
16-A Ashok Marg, Patiala House, Lucknow 226001
Sth Main Road, Gandhinagar, Bangalore 560009
1/13-14 Asaf Ali Road, New Delhi 110002
17 Chittaranjan Avenue, Kolkata 700072
81 Hill Road, Ramnagar, Nagpur 440010
‘751 Anna Salai, Chennai 600002
First published, 1988
© P.S, Grover, 1988
8th Reprint, 2001
ISBN 81-7764-193-X
Published by Sunil Sachdev and printed by Ravi Sachdev at Allied Publishers
Limited, Printing Division, A-104 Mayapuri, Phase-I], New Delhi - 110 064
2001i
Preface
Contents
Chapter 1 : Introduction
1.1 Computers 1
1.2 Computer Systems 2
1.3 Computer Generations
14 Bit, Byte, Word and Number Systems 6
see Tone
1.6 Modes of Computer System Operations 18
Exercises _20
Chapter 2 : Problem Solving and Pascal
2.1 Algorithms 23
2.2 Data Assignment 25
2.3 Flowcharts 27
2.4 mming Languages and Pascal 35
2.5 Source and Object Programs 37
2.6 Features of Computer Programs 39
2.7 Syntax Di 41
Exercises 45
Chapter 3; Elementary Concepts and Primitive Data Types
3.1 Character Set_ 49
3.2 Numbers 49
3.3 Identifiers 51
3.4 Data Types 52
3.5 Type Declarations 54
3.6 Constants 56
3.7 Variables _58
3.8 Standard Built-in Functions _60
Exercises 63
Chapter 4; Expressions
4.1 Arithmetic Expressions 66
4.3 Boolean Expressions 73
4.4 Relational Expressions 75
Exercises 78
23
49
66Chapter 5:
Chapter 6:
CONTENTS
Simple Statements and Programs 81
3:1 Structure of Pascal Program _81
5. ents in a 84
x Assignment Statement 84
5.4 Data Output Statements 87
5.5 Formatting of Output Data 91
5éyData Input Statements 94 put Statements 94
3.7 The Label Declaration 98
5.8 The Compound Statement _99
Exercises 102
Enumerated and Subrange Data Types 105
6.1 Enumerated Data T) 105
6.2 Subrange Data Types 109
6.3 Uses and Limitations of Subrange Data Types 113
Exercises 114
+ Program Execution Control 116
7.1 The ifStatement 116
7.2 The case Statement 122
7.3 The goto Statement 126
Exercises 131
: Repetitive Computations 134
8.1 The for-do Statement 134
8.2 The while-do Statement 139
8.3 The repeat-until Statement_144
8.4 Differences between while-do and repeat-until
Statements 148
Exercises 148
: Structured Data Type: Arrays 152
9.1 Subscripted Variables 152
9.2 Arrays 153
9.3 Two-dimensional Arrays. 159
E tidimensional Arrays 163
le Array 165
96 Packed Arrays 160
27 Character Sei 17
EeeCONTENTS xiii
Chapter 10: Sul : Functions and Procedures 182
10.4 Local and Global Identifiers 196
10.5 Value and Variable Parameters 199
10.6 Arrays as Subprogram Parameters 203
10.7 Functions/Procedures as Subprogram Parameters 206
10.8 Recursion 207
10.9 Uses of Subprograms 210
Exercises 211
11: Record Data 218
LL1 Records and Files 218
11.2 Record Data 220
11,3 Accessing Fields of a Record 223
11.4 Hierarchical Records 224
11.5 Shared Record Variables 228
11.6 Arrays of Records 229
1L7 The with Statement__235
118 Variant Records 236
1.9 Packed Records 240
11.10 Differences between Arrays and Records 242
Exercises 242
Chapter 12: File Types 246
12.1 File Data Type 246
12.3 Communication with Files _250
12.4 Files as Parameters in Subprograms 256
12.5 Text Files 261
Exercises 263
Chapter 13; Set Operations and Data Types 268
13,1 Set Definition and Elementary Operations 268
13.2_Set Constants 270
13,3 Set Type and Variables 271
13.4 Set Operators and Expressions 273
13,5 Set Operator Hierarchy 278
Exercises 279xiv
Chapter 14:
Chapter 15 :
‘CONTENTS:
Pointers and Dynamic Data Structures 282
14.1 Pointer Data Type 282
14.2 rations on Pointers 285
14,3 Differences between Static and Dynamic
Variables 27
14.4 Creation and Annihilation of Dynamic Variables 287
14.5 Dynamic Data Structures 290
Exercises 297
Structured Program Design Concepts 300
15.1 Pseudocode 300
15.2 Modular Design 303
15.3 Structured Pro; 304
125 Basie Structured Constructs —305
15.5 Structured Programs 30!
15.6 Eetenaion of Seetured Constructs and use of pote
Appendix I: Pascal Operators and their Precedence 327
Appendix If; Standard Identifiers and Pascal Reserved Words 328
TL: Pascal suj Functions and Procedures 329
IV: Syntax of P: 1
Diagrams 335
Appendix V; Syntax Diagrams _335
Appendix VI: ASCII and EBCDIC Character Sets 343
Bibliography 345
Index _347
Urheberrechtlich geschitztes MaterialChapter 1
Introduction
A computer is an information processing machine. It can perform arithmetic
operations (addition, subtraction, multiplication and division) and take logical
decisions. It has a memory and can store fot of information. The stored
information may be retrieved, moved and operated upon as desired.
_ Computations are done at an extremely fast speed with complete reliability and
accuracy. The speed of execution of operations by modern computers ranges
from several hundred million operations per second for fast computers to tens
of thousands of operations per second for slow computers.
1.1 Computers
There are basically two types of computers—analog and digital.
(a) The analog computer accepts, processes and generates continuous
(unbroken) data. Computations are carried out with physical quantities, such as
length, voltage, current, etc. Slide rule, voltmeter, ammeter, potentiometer, are
examples of analog devices. You know that when current is passed through an
ammeter, the deflecton of the needle indicates the amount of current passing.
Deflection is more for higher currents and less for lower currents. Now current
and deflection are both continuous quantities. The ammeter receives current
(input) and gives deflection (output) after detecting current (processing of
input). Thus, ammeter is an analog device. Analog computers use this principle,
though they are much more complicated and can perform sophisticated
processing of data.
Analog computers are comparatively slow and less accurate. They are
designed for special applications only. We cannot use a given analog computer
for all purposes.
(b) The digital computer accepts, processes and produces discrete
(discontinuous) data. Computations are done with discrete quantitics, such as
numerical digits. Usual facit. machines, electronic calculators are simple
examples of digital devices.
Digital computers are much faster than analog computers and the
computations are far more accurate. They come in various sizes-starting from
pocket size to large systems which occupy few normal-sized rooms, Digital
computers can be designed either for. special or for gencral purposes.
Another type of computer which combines the salient features of both analog
and digital computers, is referred to as Hybrid computer. It is faster than analog2 PASCAL PROGRAMMING FUNDAMENTALS
but much slower than digital computer. Hybrid computers find applications in
special areas only.
Normally, when we speak of a computer, it is understood as a digital
computer. Nowadays these are the most widely used machines.
1,2, Computer Systems
A computer system consists of a computer and supporting devices for input and
output of data. The data to be processed are supplied to the computer with the
help of input devices. The processing unit performs the desired operations on
the information and the results of calculations/processing are obtained on the
output devices. Several types of input/output devices can be attached to the
computer. A computer consists of electronic circuits only, while the
input/output devices have both electronic and mechanical components. A
representative organization of a c-mputer system may be as shown in Fig. 1.1.
Fig. 1.1: Organization of a computer system
The input device supplies: data, us obtain from us, to the computer. Most
commonly used input devices are: teletypewriter and cathode ray tube (In the
past, it used to be card reader but not now). We specify the data in a form which
we use in our everyday lite, that is, in the numeric and alphabetic form. These
are converted into the form which the computer can ‘understand’. Data are
stored in the computer in binary form (see later). (The conversion to the binary
form is performed with the help of electronic circuits called Encoders). After
the computer ‘has processed the data, results are tained in the human
readable form on the output devices, such as printer, visual display unit (VDU).
Computer data (as stored inside it) is changed into this form by electronic
circuits called Decoders.
In addition to encoders and decoders, there are other electronic circuits alsoINTRODUCTION 3
which perform several functions, such as correct transfer of data from the input
device to the computer and from the computer to the output device, regulate
flow of data, act as temporary data storage (buffering), ete. This is done with
additional electronic circuits, referred to as control input/output unit, All
input/output devices are connected to the computer via control units.
The computer does all the computing and data processing work. Its
components are:
(a) Arithmetic and logical unit (ALU)
(b) Control unit (CU)
(c) Main memory unit (MMU).
Information is transferred to and from among these units for all processing
and.computing work as indicated by arrows in Fig, 1.2.
Fig. 1.2; Information flow in the computer
Anithmetic and Logical Unit
This unit consists of a complicated clectronic circuits designed using the
concepts of Boolean algebra. All arithmetic operations-addition, subtraction,
multiplication, division and logical operations-comparison, decision, ¢tc. are
performed by this unit.
Control Unit
The control unit also consists of electronic circuits. It acts as a supervisor in a
computer system. It obtains instructions from the main memory, interprets
them, decides the action to be taken and directs other units to execute them, It
keeps check on correct information flow in the computer system. Normally, the
instructions are executed sequentially (one after another) in the machine. The
control umt also provides the facility to alter this sequence.
The ALU and the CU are also referred to as Central Processing Unit (CPU).
Main Memory Unit
This unit stores all the data which are to be processed and the program4 PASCAL PROGRAMMING FUNDAMENTALS
instructions for carrying out the processing/computing work. The main memory
is also referred to as primary or main storage. It is extremely fast (high speed
memory). Information can be entered/retrieved at random from this memory.
Commonly used memories have been magnetic cores (older systems) and
semiconducting elements (modern systems). With these, it is possible to have
very large and fast memories, Semiconductor memory consists of electronic
circuits prepared on silicon chips. The electronic circuit is called a Flip-flop. A
flip-flop circuit can generate cither 1 or O, that is, it is a two-state element. A
flip-flop is also called a Storage Cell. Thousands of these storage cells can be
prepared on a single silicon chip. Due to this, the physical size of the
semiconductor memories is very small. Moreover, their cost is decreasing every
year as the fabrication technology is advancing. The common type of
semiconductor storage devices are: Random Access Memory (RAM) and Read
only Memory (ROM). Besides these, there are other forms of semiconducting
memories as well.
In RAM, information may be read or written into the memory at random, It is
also called a Read/Write memory. It is a volatile memory, that is, information
stored there is lost when the electrical power to the circuit is switched off.
Normally, user programs and data are stored in RAM.
In ROM, information is written permanently into the memory. It cannot be
changed easily. Data can be read from the memory but cannot be written there.
This is why the name ROM. Moreover, it is a non-volatile memory. ROMs are
normally used to store information that the computer may need frequently for
its own operation.
1.3 Computer Generations
Several types of computers, having wide range of characteristics have been
designed. The design, speed, size and performance of computers have been
changing continuously. Due to this, it has become customary to divide computers
into what has come to be known as “generations”. Broadly speaking, following
are the generations of the computers.
First Generation (~ 1946-1959)
The computers of first gencration used vacuum tubes, They were bulky and
slow, Their memory was limited and used punched-card and punched paper
tape for input and output of data. These machines used low-level programming
languages and involved manual controls. They were special purpose machines
with limited applications. Examples of first generation computers include
ENIAC, EDSAC, UNIVAC I, IBM 650.
Second Generation (~ 1959-1965)
The second generation computers are characterized by their use of tiny
transistors as active elements. Due to this, these were compact and substantiallyINTRODUCTION 5
smaller in size. They required less power to operate. They were much more
reliable as compared to the first generation computers. Second generation
computers had more speed (about 10° operations per second), larger memory
and faster input/output devices. They accepted procedure-oriented languages,
such as Fortran, Cobol and other utility programs,
Several companies started manufacturing computers. Systems were designed for
special applications, such as business and scientific data processing. Prominent
second generation computers have been IBM 1401, IBM 7090/7094 series,
IBM 1620, Burroughs B5000, CDC 3600, GE 635, Honeywall 400 series,
UNIVAC II, and several others,
Third Generation (~ 1965-1975)
The third generation computers used integrated circuits (electronic circuits
designed on silicon chips) instead of transistors, The size of such circuits is
hundreds of times smaller than the transistor circuit size. Moreover, the
associated electronic circuitry is also reduced in dimensions many times. This
leads to several advantages: (i) small size and increased processing speed (* 10°
operations/second), (ii) more reliability and higher accuracy, (iii) easy
maintenance and simple repair requirements. Moreover, these machines have
very large storage capacity. Faster and more versatile input/output devices, such
as video display, graphic terminals, plotters, magnetic disks, drums, tapes, etc.
may be used with them. They have highly sophisticated operating systems,
application software and packages.
Third generation computers are mostly general purpose, that is, they may be
used for processing business, scientific or text-oriented problems. Some
examples of third generation systems are IBM 360 series, Burroughs
6700/7700 series, CDC 6000/7000 series, digital equipment PDP-8/11 series,
UNIVAC 1108/9 series, ICL 1900/2900 series, and so on.
Fourth Generation (~ 1972-1982)
The fourth generation computers use very large scale integrated (VLSI) circuits
in their design. As compared to the third generation systems, these systems
possess much larger computing powers. They have extremely large memories
and are very versatile. In addition to conventional input/output devices, other
minicomputers, magnetic ink readers, laser printers, optical readers, audio
response terminals, ctc. can be attached to them. Some examples of fourth
generation computers are IBM 370, AMADAHL 470, CRAY XMP, CYBER
and many other systems.
Fifth Generation and Beyond (~ 1981 onwards)
The computer technology has made phenomenai progress, starting from first to
fourth generation systems. Computers have been used to solve almost any type
of prablem whose algorithm can be described explicitly. However, still there are6 PASCAL PROGRAMMING FUNDAMENTALS.
areas, where computers have not been used successfully. This is because the
problems in those areas involve human intelligence, that is, reasoning,
understanding, drawing inferences, adapting to new situations, making out
relationship between facts, discovering meanings, recognizing truth, and so on.
Attenipt is being made to incorporate such characterisitics into computers and
computer programs. Such computers (though not commercially available as yet)
have been said to belong to Fifth generation. Lot of work is being done on the
hardware and software of such systems, These are also known as Knowledge
Information Processing Systems (KIPS). They will have knowledge bases and
are expected to be able to draw inferences from the knowledge and solve
problems as humans do. These are the goals and their successful completion
may completely revolutionize the computer field.
1.4 Bit, Byte, Word and Number Systems
Binary number system is used for information representation and storage in
computers. This system uses two digits: 0 and 1. Any object which can assume
two states can be used to represent these digits. As for example an electric
switch, an electric bulb, a magnetic core or some electric circuit, such as
flip-flop, and so on.
A magnetic core can be magnetized either clockwise or anticlowise. We can
say that the clockwise state of magnetization represents 1 and the anticlockwise
state of magnetization indicates 0.
Thus, a collection of magnetic cores can be used to store binary digits. A
binary digit is also called a Binary bit. Thus, each core represents a binary bit.
Binary bits are grouped together. A collection of 4 bits is called a Nibble,
while a group of 8 bits is referred to as a Byte. Normally, a byte is treated as a
single memory location. It generally represents a single character of information.
As for example, if we wish to store the character A in the computer memory, a
byte (or 8 bits) may be required. Further bytes are grouped together to form a
bigger unit. This unit is referred to as a Word. A word may consist of a single
nibble (4-bit word), two nibbles (8-bit word), 3 nibbles (12-bit word), 4
nibbles/2 bytes (16 bit word) and so on, This depends on the make of the
computer. The size of the computer memory is commonly expressed in terms of
bytes/words. As for example, it is common to say that the memory size is 64
KB (kilo byte), 128 KB, 256 KB, and so on. Here k = 2!" = 1024 and indicates
kilo.
A memory of 16 KB will have 16 X 1024 X 8 = 131072 bits. Thus a
semiconductor memory should have these many flip-flop circuits as each flip-flop
represents one binary bit.
Nowadays, very large memories are also available. Memory sizes in the range
1—8MB (or higher) are common. (MB = million bytes). Moreover,
computers are available with different word sizes such as 8—, 16—, 32—, 64—,
96-bits and so on. Larger word size affords several advantages :
® bigger main memory size_