Grid is the set of pairs {1, 2, 3, 4} x {1, 2, 3, 4}.
Image is the power set of Grid.
An element of Image is a subset of Grid and can be represented by a diagram on a 4 by 4 grid (as in Figure).
Let A and B be the elements of Image, whose diagrams are as shown in Figure.
(a) The set, A can be enumerated as below. Enumerate the set B in a similar way.
A = {(1,1), (2,1), (2,2), (3,2), (3, 3), (4, 3), (4,4)}
(b) Now suppose that A and B are variables of type Set of Pair of Int,Int whose states correspond to the diagrams shown in Figure 2. Each of the following method calls will then return a value of type Set of Pair of Int,Int. Give the diagram corresponding to each of the sets of pairs in (i)-(iii)
(i) A.intersect(B)
(ii) B.minus(A)
(iii) B.union(A)
(c) A set C is defined below by comprehension.
C = {(x,y) in Grid : (x == y) ∨ (y == x + 1)}
Enumerate the set C (as a set of pairs), and give its diagram.
(c) Suppose that the code fragment below is executed. The final state of mySet will be a set of pairs forming an element of Image. Give the corresponding diagram.
var mySet in Set of Pair of Int,Int
var p in Pair of Int,Int var x in Int
var y in Int
for (x <-- 1 to 4)
{
for (y <-- 1 to 4)
{
if ((x = = 4)\/(y = = 1)) then
{ p.setPair(x,y) mySet.addElement(p)
}
}
}
The diagrams required for this question can be included in an electronic document.