                  CHAPTER 13 - 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  program  fragments 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  - 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  - 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 - 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  - 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 



                                 Page 68









                  CHAPTER 13 - Complete sample programs


                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 - 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 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 right across the paper folds  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.  Or include the day of the month when  the 

            payment is due also.



                                 Page 69









                  CHAPTER 13 - Complete sample programs



        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, to list your Pascal programs

            LIST  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 TURBO extensions.  

        The two extensions it uses are the STRING type variable  and 

        the  ABSOLUTE type variable.   The ABSOLUTE variable in line 

        13  and  the  coding in the  "initialize"  procedure  is  an 

        example  of how you can read in the parameters given on  the 

        command line.  For example, to use this program to print out 

        the  last program,  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  COM 

        file, and you must quit TURBO Pascal in order to run it from 

        the DOS command level.

             The parameter, 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.

            If  you do not have TURBO Pascal,  but you are using MS-

        DOS or PC-DOS,  you can still use this program because it is 

        on  your disk already compiled as LIST.COM,  and can be  run 

        like any other .COM or .EXE program.

                      LIST2, another listing program

            LIST2 is the same as LIST, except that it does not parse 

        the command line for the filename.  Instead, it requests the 

        desired  filename to output,  and you input it as user  data 

        after  the  program  begins executing.   It  is  written  in 

        "generic" Pascal and should execute and run with any  Pascal 

        compiler.

                  TIMEDATE, to get today's time and date

            This  is  a very useful program for those of  you  using 

        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  Pascal 



                                 Page 70









                  CHAPTER 13 - Complete sample programs


        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.

                        AREAS, an example of menus

            This program is not very useful,  but it illustrates one 

        way to handle menus in a Pascal program.   You can study the 

        structure  and  imagine  many ways a menu  can  be  used  to 

        improve the usefulness of your own programs.

                     OT, The OAKTREE directory program

             This  program should be very useful to you,  especially 

        if you have a hard disk.   It will list the entire  contents 

        of  your  hard disk (or floppy) in a very easy to  read  and 

        easy to use form.   The program is documented in OT.DOC, and 

        is  precompiled for you in OT.COM in case you are not  using 

        TURBO  Pascal.   It uses many of the TURBO Pascal extensions 

        and will probably not compile with any other Pascal compiler 

        without extensive modifications.

             You  will  find  the 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 also 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.   One of the best books  available  is 

        "Programming  in  Pascal" by Peter Grogono,  and another  is 

        "Oh! Pascal!" by Doug Cooper and Michael Clancy.

            Happy programming.



                                 Page 71

