You are on page 1of 7

1

CPS Problem Solving ICA

Student Name:

Student ID:

Course Name:

Date:
2

Part 1 – Nervous Couples


Question 1 (5%):

1B6C || 1V

1B4C || 2C1V

5C || 2C

Initia 1B3C || 1V1C


l
4C1V || 2C1B

1B3C1V || 2C1V
7C|| 1B3C || 2V

2B2C || 1V,1C
1V3C || 1C1B
2B2C || 1C1V
3

Question 2 (5%):
1B3C1V || 2C1V
Question 3 (5%):
11
Question 4 (10%):
1. , with length equal to .
2. , with length equal to .
3. , with length equal to .
4. , with length equal to .
Method used is breadth first search

Part 2 – Invariants
Question 1 (15%):

Assignment Expression Invariant


i , j:=i+5 , j− 3 5i – 3 j 22(j+1)-1
i , j:=i+5 , j− 3 3i – 5 j 22(j+2)-1
n , m:=n+10 ,m+15 3 n>2 m 22(j+3)-1
x , y :=x /2 , y ∗ 2 xy=k 22(j+4)-1
4

x :=2 x even ( x ) 22(j+i)-1

Question 2 (10%):
Approach:
1. What is the desired end result?
A. The number of boxes in total.
2. What is the provided input?
A. i. 11 big empty boxes
ii. At the end, there are 102 empty boxes.
iii. There are a few huge ones with eight medium boxes apiece.
iv. Several medium ones with eight little boxes apiece.
1. We can't truly utilize the big, medium, and tiny attributes from question 1.
2. The quantity of medium or small boxes is also not necessary since we simply need to keep 8
boxes.
As a result, iii and iv inputs may be disregarded.
After disregarding other properties, there are only two sorts of boxes in the problem: empty and full.
And we need the sum of the two.
So, for empty and filled boxes, introduce the variables e and f.
Finding the invariant:
Step:
A single box is chosen, and eight boxes are placed within it.
So, at each step, e is decremented by one, f is increased by one, and e is increased by eight.
i.e., since the box has been filled with some boxes, e must be decremented by one.
The box is now completely filled. As a result, f is increased by one.
Eight additional (empty) boxes have been added. As a result, e is increased by 8.
Model:
As a result, the task should be:
e := e-1
f := f+1
e := e+8
We may also omit the first step and write as
5

f := f+1
e := e+7
And at the start of the procedure, e=11 and f=0.
Now,
After a certain number of repetitions of the same process, e is provided as 102.
So,
e:= e+7 is repeated a number of times with an initial e=11, yielding 102. That is, the procedure is
repeated 13 times using basic math.
As a result, beginning from 0, the value of f is 13.
The total number of boxes is 102 plus 13 = 115.

Part 3 – Calculating Programs


Question 1 (5%):
2nC2 = 2n(n-1)/2

= 23(3-1)/

= 23

Question 2 (5%):
state | 0 | 1 | accepting
--------------------------
A | B | A | no
B | C | A | no
C | A | D | no
D | A | E | no
E | A | A | yes

Question 3 (15%):
Assume P and k≥0.

R(sum := sum + k)

= < defn of R >

P(k:=k+1)(sum := sum + k)

= < defn of P >


6

(sum = (+i | 0≤i<k : i))(k:=k+1)(sum := sum + k)

= < textual substitution >

(sum = (+i | 0≤i<k+1 : i)(sum := sum + k)

= < textual substitution >

sum + k = (+i | 0≤i<k+1 : i)

= < split off term (Gries 8.23) (relying upon assumption


k≥0 to guarantee that range is non-empty) >

sum + k = (+i | 0≤i<k : i) + k

= < algebra: subtract k from both sides >

sum = (+i | 0≤i<k : i)

= < assumption P >

true

Part 4 – Induction
Question 1 (5%):

Assume P (i.e., x=X ∧ y=Y).

R'(temp:=x)

= < defn. of R' >

R(x:=y)(temp:=x)

= < defn of R >

Q(y:=temp)(x:=y)(temp:=x)

= < defn of Q >

(x=Y ∧ y=X)(y:=temp)(x:=y)(temp:=x)

= < textual substitution >

(x=Y ∧ temp=X)(x:=y)(temp:=x)

= < textual substitution >


7

(y=Y ∧ temp=X)(temp:=x)

= < textual substitution >

y=Y ∧ x=X

= < assumption >

true

Question 2 (20%):
Base case

call to/return value for(3) for(2) for(1) for(0) returns 0 returns 1 returns 3 returns 6

for(1) for(1)

for(2) for(2) for(2) for(2)

for(3) for(3) for(3) for(3) for(3) for(3)

awaiting return ... main() main() main() main() main() main() main() main()

Induction hypothesis

k(k + 1)

2 + (k + 1) (by induction hypothesis)

= k(k + 1) + 2(k + 1)

2 (by algebra)

= (k + 1)((k + 1) + 1)

2 (by algebra).

Thus, (1) holds for n = k + 1, and the proof of the induction step is complete.

You might also like