-
Suppose you are given arrays p[1 ...N] and q[1...N] both uninitialized that is, each location may contain an arbitrary value), and a variable count, initialized to 0. Consider the following procedures set and iset :
Set (i) {
count = count + 1;
q [count] = i;
p[i] = count;
}
is_set(i) {
if (p[i] ≤ 0 or p[i] > count)
return false;
if (q[p[i]] ≠ i)
return false;
return true;
}
(a) Suppose we make the following sequence of calls: set (7); set (3); set(9); After these quence of calls, what is the value of count, and what do q[1], q[2], q[3], p[7], p[3] and p[9] contain?
(b) Complete the following statement “The first count elements of ................ contain values i such that set (....................) has been called”.
(c) Show that if set (i) has not been called for some i, then regardless of what p[i] contains, is_set (i) will return false.
-
- 0
- 1
- 2
- None of these
Correct Option: D
(a) Count = 3
q[1] = 7 q[2] = 3, q[3] = 9
p[7] = 1 p[3] = 2, p[9] = 3
(b) The first count elements of q contain values (i) such that set (i) has been called.
(c) Suppose p[i] has a value < = 0 or > count, then to set (i) return false.
Otherwise consider q[p[i]]. Using (b), q[p[i]] contains a value (i) such that set (i) has been called. Since set (i) has not been called, q[p[i]] cannot be equal to (i) and is set (i) returns false.