Exercise Lover

March 30, 2007

Exercises 2.1-4

Filed under: 2.1 — yuhanlyu @ 4:38 am

carry = 0
i = 1
while i <= n
    c[i] = a[i] + b[i] + carry
    if c[i] > 1
        carry = 1
        c[i] = c[i] - 2
    else
        carry = 0
    i = i + 1
c[n+1] = carry

Exercises 2.1-3

Filed under: 2.1 — yuhanlyu @ 4:35 am

i = 1
while i <= n and a[i] ≠ v
    i = i + 1
if a[i] = v then return i, otherwise return NIL

Loop invariant: a[j] ≠ v for all j < i

Exercises 2.1-2

Filed under: 2.1 — yuhanlyu @ 4:31 am

Change step 5 to “while i > 0 and A[i] < key”

Exercises 2.1-1

Filed under: 2.1 — yuhanlyu @ 4:30 am

Blog at WordPress.com.