


                                                       Chapter 31
                                            MORE EXAMPLE PROGRAMS


RANDOM NUMBERS
_________________________________________________________________

Occasionally when writing a computer program,    ================
you will have a need to use a random number         RANDOM.ADA
generator.  Very few compilers have a random     ================
number generator as a part of the system, so it
is necessary for you to either write one
yourself or find one that has been written and debugged by someone
else.  The example package named RANDOM.ADA is presented to you for
several reasons.  The first reason is to provide you with a useful
example of a complete generic package to serve as an illustration
of how to write one.  Secondly, it is a useful package that you can
use as a part of your own programs when you have a need to use a
random number generator.  Finally, it is an illustration of good
formatting style and it illustrates the inclusion of enough
comments in the package specification to completely define the
method used, and how to utilize this package as part of another
program.

No other comments need to be given about the operation or use of
this package, so you will be left on your own to study the listing
then compile this package in preparation for use with the next
example program.



TESTING THE RANDOM NUMBERS
_________________________________________________________________

Examine the program named TESTRAN.ADA which was   ===============
written solely to test the random number            TESTRAN.ADA
generator in the package named Random.  It        ===============
instantiates a copy of the generic package using
the type FLOAT in line 7, then declares a few
objects and an array type.  In the executable part of the program
the random number generator is initialized with the Set_Seed
procedure in line 21, and 12 random numbers are read and printed
to see that they do cover the range of 0.0 to 1.0 as defined in the
header of the package named Random.

The real test of the random number generator is in the loop
beginning in line 35 where ten thousand random numbers are
generated and converted into integer type values by multiplying by
100.  The integer values will therefore cover a range of 1 to 100,
and they are counted in the array named Events.  The count in each
element of the array should be about 100 since there are ten
thousand cases distributed over 100 elements.  Execution of the
program will reveal that the count in each array element is about

                                                        Page 31-1

                               Chapter 31 - More Example Programs

100 as expected, so we declare the random number generator to be
at least reasonably random.  A mathematician may decide that this
method is too crude to be called a good random number generator,
but for our purposes it is good enough.

Compile and execute this program, and you will find that each time
you run it, you should get different results because it uses the
system clock to set the seed, resulting in a new starting seed for
each execution.



A NEW DYNAMIC STRING PACKAGE
_________________________________________________________________

Examine the program named DYNSTRNG.ADA, which    ================
is included in part 2 of this tutorial for a       DYNSTRNG.ADA
better dynamic string package.  You will recall  ================
that when we studied the dynamic string package
in chapter 16 of part 1 of this tutorial, we
found a problem when using string constants in the Copy procedure
calls.  This was because the system found ambiguous procedures.
It could not tell if the string constant was of type STRING or of
our own declared type which we named DYNAMIC_STRING.  Since we had
not studied the discriminated record at that time, we could not
properly fix the problem.  The new DynStrng package, using a
discriminated record, is offered as a better package for the
problem of using dynamic strings.

The DYNAMIC_STRING type is declared in lines 32 through 36 and is
declared as a record this time so there is no confusion as to
whether it is a string or a record, and the overloading ambiguity
problem is gone.  The package specification is essentially
unchanged from the last dynamic string package, except for the type
of course, but the body is changed considerably to reflect the new
data structure.  You will be left on your own to compare the bodies
of these two packages if you so desire.



THE STRING CONSTANT PROBLEM IS FIXED
_________________________________________________________________

The program named TESTSTRN.ADA is designed to    ================
test the new package with a few string constants   TESTSTRN.ADA
to prove that it really does work as advertised. ================
You can compile and execute this file to see
that it really does work with string constants
in a Copy procedure call.

You can return to the program named TRYSTRNG.ADA from chapter 16
to prove that the new package still works with this old program.
You will find that a couple of changes must be made to reflect the
different data type.  Lines 11 and 12 must be modified to reflect

                                                        Page 31-2

                               Chapter 31 - More Example Programs

only the upper limit on the static length of the ================
dynamic string variables.  They will read as       TRYSTRNG.ADA
follows;                                         ================

     Name    : DYNAMIC_STRING(15);
     Stuff   : DYNAMIC_STRING(35);

In addition, because the type is changed, lines 21 and 22 must also
be modified as follows;

     Name.Dynamic_Length := 3;
     Stuff.Dynamic_Length := 7;

After making these two changes, this program should execute exactly
as it did when it used the old dynamic string package.



HOW OLD ARE YOU IN DAYS
_________________________________________________________________

This program is a repeat of the program given in ================
chapter 16, but it is improved somewhat here.        AGE2.ADA
Since we now know how to use the Calendar        ================
package, we can use it to get today's date for
us, and we do this in the new program named
AGE2.ADA.  Notice especially the way the data is read in and
checked for validity before continuing on.  If the data were read
into the corresponding variables, an invalid entry would cause an
exception, but since the data is read into an INTEGER type
variable, it can be checked for validity before being assigned to
the correct variable.  The program should be very simple for you
to understand, but it would be good for you to spend a little time
studying it before compiling and executing it.




THE DINING PHILOSOPHERS
_________________________________________________________________

Most books and articles on tasking or            ================
concurrency at least mention the problem of the     PHILOS.ADA
dining philosophers, so it would not be good to  ================
leave this tutorial without a little discussion
of this problem.  In fact, the program named
PHILOS.ADA is a program you can study and execute to see this
problem illustrated.

The problem is stated that five philosophers sit down to eat.  They
like to eat for awhile then think for awhile and repeat the pattern
forever.  In order to eat, they require a fork in both their left
and their right hands, and the table is set with a fork on each
side of their plates.  The problem occurs when we state that there

                                                        Page 31-3

                               Chapter 31 - More Example Programs

is only one fork between each adjacent philosopher, and he is
therefore required to share each fork with his adjacent colleague.

Each philosopher sits down, waits a random length of time, then
picks up the fork on his left, waits another random length of time,
and picks up the fork on his right.  He then proceeds to eat for
a random length of time, then returns both forks to the table and
thinks for a random length of time.  Once he picks up the left
fork, he stubbornly hangs on to it until he gets the right fork.
If we ever reach the condition where each philosopher has his left
fork, then none will ever return it and none can ever therefore
pick up his right fork.  The entire system is said to be deadlocked
because nothing else will ever be accomplished.  All five of the
uncooperative philosophers will eventually starve since none can
eat.



THE PROGRAM ILLUSTRATES DEADLOCK
_________________________________________________________________

The program uses a package to define a task type for one
philosopher with the required delays and the logic to acquire each
fork, eat, then return the forks to the table.  Following our study
of tasking, you should have no problem understanding the logic
presented here.

The main program named Philos, which starts in line 91 of the file
simply declares the five philosophers, starts them through their
cycles in lines 106 through 110, then loops while it is watching
for deadlock.  When deadlock is detected, a message is output to
the monitor, and the entire system is aborted.

Compile and execute this program, so you can observe deadlock
occurring and the system aborting operation.  If you run it several
times, you will see that quite often deadlock occurs immediately,
but many times it will run for several seconds before deadlock is
detected.

This is an interesting problem, but the more interesting point is
the fact that this program, which begins in line 91 uses Text_IO
and One_Man, and One_Man in turn uses Text_IO, Calendar, and
Random.  This program uses quite a bit of the resources available
in Ada, and uses several packages to accomplish its intended
mission.  Since we are using Random, which was developed for an
earlier project, we are actually illustrating the reusability of
Ada software.


THE GENERIC STACK
_________________________________________________________________

We promised we would include a generic stack when we studied the
character stack in chapter 16, and GENSTACK.ADA is a generic stack

                                                        Page 31-4

                               Chapter 31 - More Example Programs

to fulfill that promise.  It is really only a    ================
copy of CHARSTAK.ADA from chapter 16 made into     GENSTACK.ADA
a generic package according to the rules studied ================
in this tutorial.

The program TRYSTAK.ADA is nearly identical to    ===============
the program of the same name from chapter 16,       TRYSTAK.ADA
the only difference being the instantiation of    ===============
the generic package so it can be used in the
main program.












































                                                        Page 31-5

         ----------------end-of-author's-documentation---------------

                         Software Library Information:

                    This disk copy provided as a service of

                           Public (software) Library

         We are not the authors of this program, nor are we associated
         with the author in any way other than as a distributor of the
         program in accordance with the author's terms of distribution.

         Please direct shareware payments and specific questions about
         this program to the author of the program, whose name appears
         elsewhere in  this documentation. If you have trouble getting
         in touch with the author,  we will do whatever we can to help
         you with your questions. All programs have been tested and do
         run.  To report problems,  please use the form that is in the
         file PROBLEM.DOC on many of our disks or in other written for-
         mat with screen printouts, if possible.  PsL cannot debug pro-
         programs over the telephone, though we can answer questions.

         Disks in the PsL are updated  monthly,  so if you did not get
         this disk directly from the PsL, you should be aware that the
         files in this set may no longer be the current versions. Also,
         if you got this disk from another vendor and are having prob-
         lems,  be aware that  some files may have become corrupted or
         lost by that vendor. Get a current, working disk from PsL.

         For a copy of the latest monthly software library newsletter
         and a list of the 4,000+ disks in the library, call or write

                           Public (software) Library
                               P.O.Box 35705 - F
                            Houston, TX 77235-5705

                                1-800-2424-PSL
                             MC/Visa/AmEx/Discover

                          Outside of U.S. or in Texas
                          or for general information,
                              Call 1-713-524-6394

                          PsL also has an outstanding
                          catalog for the Macintosh.

