



eXercise #14
Compound Data Structures
 The SET
 Assuming the definition:
Type byteset = set of byte;
Write simple but efficient subprograms (procedures or functions)
that perform the following functions:
 Procedure
PrintSet(theSet:byteset)
Given a set as a value parameter, output all of the elements that
are contained within the set
 Procedure
AddElement(var theSet:byteset; element:byte)
Given a set as a REFERENCE parameter, and a valid element as a
value parameter, return the set with the element added to it (use
set union '+')
 Function Cardinality(theSet:byteset):byte;
Given a set as a reference parameter, return the number of elements
contained within the set (use a simple iterative tallying process)
 Procedure
RemoveElement(var theSet:byteset; element:byte)
Given a set as a REFERENCE parameter, and a valid element as a
value parameter, return the set with the element removed from
it, if it was there in the first place (use set difference '')
 Procedure
CreateRandomSet(var theset:byteset; num:byte)
Get this procedure to generate a set of 'num' random elements,
use your subprograms above to verify this process is working
well. For example, CreateRandomSet(ken,5) should return
a set that contains 5 different elements.
 Using the definitions
from the previous question:
Write a LOTTO Syndicate Game Generator program that does the following
things (in this order):
 prompt for and accept
the number of numbers the syndicate can afford
 ask for that many
numbers and store them in a set  this section of code should
be 'clever' enough to ensure that only different
numbers are stored in the set
 prompt for and accept
how many numbers are required in each game
 prompt for and accept
the number of required games
 generate and print
onto the screen the number of required games, ensuring that each
game contains the required number of numbers
Please note, a working
copy of this program is available for you to trial.

 Assuming the definitions
Type days = (sun,mon,tue,wed,thu,fri,sat);
DaySets = set of days;
declare the following
four set variables in Pascal.
EnglishDays,
MathematicsDays,
ComputerDays and
BiologyDays
(These variables are
going to contain all those days on which you have English, Mathematics,
IPT and Biology.)
 Suppose your timetable
looked like the table which follows (you should be so lucky!).
Note that '' means a spare.
Sunday:
Monday: en ma ma ip bi
Tuesday: ma ip  bi en
Wednesday:   bi bi 
Thursday: en en bi ip ip
Friday:  ip ip ma ma
Saturday:
Now, complete the following
exercises.
(a) Make appropriate
assignments to the four set variables EnglishDays. MathematicsDays,
IPTDays and BiologyDays which will inform the machine of your
timetable.
(b) Write Pascal
boolean expressions which would indicate whether or not you:
(i) had
English on Monday
(ii) had Biology on both Wednesday and Thursday
(iii) had IPT on either Monday or Wednesday
(c) Write small program
fragments which would indicate those days on which you had
(i) Mathematics
(ii) both Mathematics and Biology
(iii) either English or IPT
(iv) Mathematics but not IPT
(v) nothing
(d) Write a small
function which determines the number of days on which you have
English.
 Write a BOOLEAN function
called Yes which returns true if the user types 'y' or 'Y', and false
if s/he types 'n' or 'N'. (Note that any other character must be ignored
by the function.)
 Write a simple INTEGER
function called Cardinality that accepts a set of byte and returns
the number of elements in it.
 Write a simple procedure
that operates on a set of char, changing all of the lower case letters
present in the set to their uppercase equivalents.
 Mastermind is a simple
game where you have to guess the unknown contents of a set of three
letters.
Write a simple but polite program that generates and stores in a set
3 different uppercase letters. Allow the user up to 10 guesses of
three letters each guess  the feedback the user gets should ONLY
include the number of correct letters (unless they guess all three)
Solutions






