Pascal Tutorial - Chapter 16

COMPLETE SAMPLE PROGRAMS

Prior to this point, this tutorial has given you many example programs illustrating a point of some kind, but these have all been "nonsense" programs as far as being useful. It would be a disservice to you to simply quit with only tiny programs to study, so the following programs are offered to you as examples of good Pascal programming practice. They are useful programs, but they are still short enough to easily grasp their meaning. We will discuss them one at a time.

AMORTIZATION TABLE GENERATOR

This is not one program, but five. Each one is an improvement on the previous one, and the series is intended to give you an idea of program development.

AMORT1.PAS

This is the bare outline of the amortization program. Although it is an operating program, it doesn't do very much. After some thought and planning, the main program was written to allow for an initialization, then an annual repeating loop. The annual loop would require a header, a monthly calculation, and an annual balance. Finally, a procedure was outlined for each of these functions with a minimum of calculations in each procedure. This program can be compiled and run to see that it does do something for each month and for each year. It has a major problem because it does not stop when the loan is payed off but keeps going to the end of that year. The primary structure is complete.

AMORT2.PAS

This is an improvement over AMORT1. The monthly calculations are correct but the final payment is still incorrectly done. Notice that for ease of testing, the loan variables are simply defined as constants in the initialize procedure. To make the procedures easier to find, comments with asterisks were added. This program is nearly usable. Compile and run it.

AMORT3.PAS

Now we calculate the final payment correctly and we have a correct annual header with column headings. We have introduced a new variable to be used for an annual interest accumulation. This is neat to have at income tax time. This program can also be compiled and run.

AMORT4.PAS

This program does nearly everything we would like it to do. All of the information needed to build the table for any loan is now read in from the keyboard, greatly adding to the flexibility. After the information is available, the monthly payment is calculated in the newly added procedure Calculate_Payment. The annual header has a new line added to include the original loan amount and the interest rate in the information. Compile and run this program to see its operation.

AMORT5.PAS

The only additional feature in this program is the addition of a printout of the results. Examining the program, you will notice that many of the output statements are duplicated with the Lst included for the device selection. Compile and run this program, but be sure to turn your printer on to get a printout of the amortization table you ask for.

TOP DOWN PROGRAMMING

The preceding example is an example of a top-down approach to programming. This is where the overall task is outlined, and the details are added in whatever fashion makes sense to the designer. The opposite is a bottom-up programming effort, in which the heart of the problem is defined and the rest of the program is built up around it. In this case, the monthly payment schedule would probably be a starting point and the remainder of the program slowly built up around it. Use whichever method works best for you.

The final program AMORT5.PAS is by no means a program which can never be improved upon. Many improvements can be thought of. These will be exercises for you if you so desire.

  1. In the data input section, ask if a printout is desired, and only print if it was requested. This would involve defining a new variable and if statements controlling all write statements with Lst as a device selector.
  2. Format the printout with a formfeed every three years to cause a neater printout. The program presently prints data with no regard to the top of page.
  3. Modify the program to include semimonthly payments. Payments twice a month are becoming popular, but this program cannot handle them.
  4. Instead of listing the months as numbers, put in a case statement to cause the months to be printed out as three letter names. You could also include the day of the month when the payment is due.
  5. Any other modification you can think up. The more you modify this and other programs, the more experience and confidence you will gain.

LIST.PAS, to list your Pascal programs

LIST.PAS is a very useful program that you can use to list your Pascal programs on the printer. It can only be compiled with TURBO Pascal because it uses a TURBO extension, the string type variable.

The method used in the Initialize procedure to read the command line parameter should be no problem for you to understand at this point. To use this program to print out the last program, for example, you would enter the following at the DOS prompt LIST AMORT5.PAS. This program reads in the AMORT5.PAS from the command line and uses it to define the input file. It should be pointed out that this program cannot be run from a "compiled in memory" compilation with the TURBO Pascal compiler. It must be compiled to a Disk file, and you must quit TURBO Pascal in order to run it from the DOS command level.

The parameter read from the command line, AMORT5.PAS, is stored at computer memory location 80(hexadecimal) referred to the present code segment. If you didn't understand that, don't worry, you can still find the input parameter in any program using the method given in the initialize procedure for your version of TURBO Pascal.

TIMEDATE.PAS, to get today's time and date

This is a very useful program as an example of using some of the extensions of TURBO Pascal. It interrogates the inner workings of DOS and gets the present time and date for you, provided you entered them correctly when you turned your computer on. The procedure Time_And_Date can be included in any TURBO Pascal program you write to give you the time and date for your listings. As an exercise in programming, add the time and date to the program LIST to improve on its usefulness. It turns out to be an almost trivial program but is still a good illustration of how to use some of the newer Borland extensions to Pascal. The observant student will notice that the time and date procedures have already been added to LIST.PAS.

SETTIME.PAS, a useful utility program

This program is very interesting in that it changes the date and time stamp on any file in the current directory. It is the program used to set the time and date on all of the files in the current distribution of this tutorial. It sets the time to 12:00:00 and the date to Feb 4, 1991 but you can use it to set any desired time.

OT.PAS, The OAKTREE directory program

This program should be very useful to you. It will list the entire contents of any disk in a very easy to read and easy to use form. The program is documented in the file named OT.DOC. It uses many of the TURBO Pascal extensions and will probably not compile with any other Pascal compiler without extensive modifications.

This is a very useful program, so you should spend the time necessary to both understand it and modify it for your own needs.

You will find this program to be a good example of linked lists because it includes a sort routine using a dynamically allocated B-TREE and another sorting routine that uses a dynamically allocated linked list with a bubble sort. These methods are completely defined in Niklaus Wirth's book, "Algorithms + Data Structures = Programs", a highly recommended book if you are interested in advanced programming techniques.

It might also be pointed out that OT.PAS makes use of recursive methods for both sorting and handling subdirectories. It is definitely an example of advanced programming methods, and it would be a good vehicle for your personal study.

MOST IMPORTANT - Your own programs

Having completed this tutorial on Pascal, you are well on your way to becoming a proficient Pascal programmer. The best way you can improve your skills now is to actually write Pascal programs. Another way to aid in your building of skill and confidence is to study other Pascal programs. Many programming examples can be found in computing magazines and books. There are many books available devoted entirely to TURBO Pascal and you would do well to visit your local bookstore and review a few of them.

You already own one of the best books available for reference if you are using TURBO Pascal. Although the TURBO Pascal reference manual is worth very little as a learning tool, it is excellent as a language reference manual. Now that you have completed all 16 chapters of this tutorial, you have a good grasp of the terminology of Pascal and should have little trouble reading and understanding your reference manual. Your only limitation at this point is your own perseverance and imagination.

Whatever your programming level or needs may be, Pascal can fulfill them and do so in a very elegant way.

Happy Programming.

Return to the Table of Contents


Copyright © 1986-1997 Coronado Enterprises - Last update, March 16, 1997
Gordon Dodrill - dodrill@swcp.com