More Programs: Continued - Pascal programming

We’ll present two more examples of complete programs before the end of this. However, you should be aware that Pascal offers you a number of tools that make it possible for you to write program without having to “re-invent the wheel” every time. On the one hand, Pascal includes a number of pre-defined

subprograms that are always available. On the other hand Pascal allows you to define your own subprograms, and to use subprograms from program libraries.

Actions: Pre-Defined Standard Functions in Pascal

As a convenience to the programmer, commonly used actions are often made available in a programming language. Pascal has a number of pre-defined actions, as shown in Figure. These actions are all functions, analogous to trigonometric functions like sine, cosine, etc., that accept a single value and yield a single value of a given type. Functions can be used anywhere a variable of that type may be used.

Pascal pre-defined functions

Pre -defined Functions

There are other pre -defined actions, including ORD and CHR, which we will consider later when needed. Let’s now look at another simple Pascal program, shown in Figure, to produce a table of temperatures expressed in Celsius and Fahrenheit units.

Pascal program TempTable

Program TempTable outputs a table of Celsius temperatures and the corresponding Fahrenheit values. The Celsius temperature starts at 0°C. Within the loop, the Fahrenheit value is computed, both the Celsius and Fahrenheit values are written as integers, and the Celsius value is increased by 10 to get the next temperature. The looping continues while the Celsius value is less than or equal to 100, and it stops when Celsius exceeds 100 Notice that the temperature conversion formula is similar to the one in the Convert example of Figure in the last chapter.

Note however that in TempTable, the decimal points are not shown in order to make TempTable more readable. This does not change the computation which is done with REAL values because of the “/” operation. Usually it is better to write the constants as REAL and show the decimal points in order to make the program less error prone. The program produces the following output.

Celsius Fahrenheit 0 32 10 50 20 68 30 86 40 104 50 122 60 140 70 158 80 176 90 194 100 212

Libraries: Using Units in Pascal

Libraries of programs are extremely important in software development as they enable software to grow in a disciplined and controlled manner. Libraries make it possible to achieve modularization, one of the goals of software engineering. Software engineering is a field of Computer Science that establishes methods for the development of good software. Libraries are basically collections of subprograms and data types that can be viewed as useful extensions to a programming language. The Pascal libraries are called units. In this, we will not create units, that will only come later, but we will use them. The details of the units are hidden, but the subprograms within the units are available to be shared.

We have defined library IntLib as a collection of various operations on INTEGERs that are not provided in Pascal. All these operations are subprograms, but most of them are Pascal procedures, rather than Pascal functions. Procedures calls are independent statements, analogous to the invocation of subalgorithms in pseudocode, while function calls are only used in expressions, and are therefore only parts of statements. The following operations are available in library IntLib.

Incr(I, S) is a procedure that increments an INTEGER I by a step size of S, where S is also an INTEGER. For example:

Decr(I, S) is a procedure that decrements an INTEGER I by an INTEGER step size of S. It is very similar to the above increment operation.

Maximize2(A, B, C) is a procedure that finds the maximum of any two INTEGERs A and B and sets C to that value. For example:

results in M having 11 as its value.

Minimize2(A, B, C) is a procedure that finds the minimum value C of any two INTEGERs A and B. It is similar to Maximize2.

Order2(A, B) is a procedure that sorts the two input variables A, B in increasing order. For example, if the value of X is 11 and the value of Y is 7:

results in X having value 7 and Y having value 11.

Order3(A, B, C) is a procedure that sorts the three input variables A, B, and C in increasing order. For example, if the value of X is 11, the value of Y is 7, and the value of Z is 9:

results in X having value 7, Y having value 9 and Z having value 11.

Divide(N, D, Q, R) is a procedure that divides numerator N by denominator D and produces a quotient Q and a remainder of R. For example:

divides Sum by Num yielding a quotient Mean and a remainder Rem.

IntToReal(I, R) is a procedure that converts a given INTEGER I into its corresponding REAL value R. For example:

results in X having the REAL value of 7.0.

The fact that a program requires the use of one or more operations from a Library is indicated by naming the Library in the Uses clause, which follows the program header. To use any of the above procedures from IntLib requires only to add:

at the beginning of your program. We’ll now look at such a program, shown in

Figure,that implements the algorithm taken from Figure of the Principles.

Figure The Triangle program
PROGRAM Triangle;

The Triangle program is simple but involves nested selections. It uses procedure Order3 from library IntLib to put the side lengths in order. To do this, it has only to invoke it, because the “Uses IntLib;” statement has made all the contents of the IntLib library available to program Triangle. The program declares three integer variables for the triangle sides. It reads the three values and orders them by invoking Order3. Then, it checks to see if it is not a triangle and if so displays a message. If it is a real triangle the program checks to see if it is an equilateral triangle, and if not checks the isosceles condition. It then checks to see if the triangle is a right triangle. The indentation of the program is very important as it shows the structure of the program. In particular, it shows what statements are nested in others. For instance, the first ELSE includes the rest of the program so that, if the figure is not a triangle, none of the following statements are executed. On the other hand, the last ELSE includes two IF statements. Nested IF statements must be written with care, and more will be said about them in the next chapter. In the meantime you may note that ELSEs cannot be preceded by semicolons. A typical execution of the Triangle program follows.
Give the three sides 5 4 4
isosceles triangle

Note that the last line message was displayed by executing two Write statements.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Pascal programming Topics