Algorithms
and Programming eXercises #14
Compound Data Structures
The SET - Solutions
The following
represent strategies/algorithms and NOT actual coded solutions - it is
thought there is definite advantage here for students to actually wrestle
with the actual code to get a working solution. There may be MANY different
ways to encode these solutions - get creative and try to be efficient
1
- Procedure PrintSet(theSet:byteset);
var loop : byte;
begin
...for loop := 1 to 255 do
......if loop in theSet
.........then write(loop,', ')
end; {PrintSet}
- Procedure AddElement(var theSet:byteset; element : byte);
begin
...theset := theset+ [element]
end; {AddElement}
- Function Cardinality(theset:byteset):byte;
var counter, loop : byte;
begin
...counter := 0;
...for loop := 0 to 255 do
......if loop in theset
.........then inc(counter);
...Cardinality := counter;
end; {Cardinality}
- Procedure RemoveElement(var theSet:byteset; element : byte);
begin
...theset := theset- [element]
end; {AddElement}
- Procedure CreateRandomSet (var theset:byteset; num : byte);
var thing, counter : byte;
begin
...randomize;
...counter := 0;
...repeat
......thing := random(256);
......if not(thing in theset)
.........then
............begin
...............theset := theset + [thing];
...............inc(counter)
............end
...until counter = num
end; {dreate random set}
2. This program requires a simple set structure used over aand over using
a loop. Each game, you clear the set, randomly insert a number of numbers
into it (see 1(e) above) and then output the set before clearing it and
starting again
3
- EnglishDays, MathematicsDays, ComputerDays, BiologyDays: Daysets
- EnglishDays := [mon, tue, thu]; and so on
- write(mon in EnglishDays)
write((wed in BiologyDays) and (thu in BiologyDays))
|