^CSE2304^

# Tute 2, CSE2304, CSSE, Monash, Semester 1, 2002

## Group A: week 4, 25-28 March (Fri 29th -> 12 April), Group B: week 5,   8-11 April (Fri 12th -> 19 April)

Tutors: (i) The purpose of the tutorials is not to solve the prac's! (ii) The purpose of the tutorials is to check answers, and to discuss particular sticking points, not to simply make answers available. It will only be possible to cover all questions if the class has prepared them all in advance.

1. x, y and z are integer variables. Give code to determine if exactly two of x, y and z are even. Use as few tests (==, !=, <, <=, > >=, odd(), even()) as possible. (For this question it is "unethical" to treat an int as a boolean or v.v..)

2. A[0..N-1] is an array of N integers.
Give efficient code to find the position of the second largest element in A. Resolve ties arbitrarily.
Give assertions, pre- & post-conditions, loop-invariants and arguments why your code terminates and is correct.

3. A[0..N-1] is an array of N integers.
Give efficient code to find the second largest value in A.
You cannot assume that the elements of A are all different, but you can assume that they do not all hold the same value.
Give assertions, pre- & post-conditions, loop-invariants and arguments why your code terminates and is correct.

4. for i from Lo1 to Hi1 do
for j from Lo2 to Hi2 do
body
end_for
end_for
How many times is `body' executed if
1. Lo1 = 0, Hi1 = n, Lo2 = 0, Hi2 = i
2. Lo1 = -n, Hi1 = n, Lo2 = i-2, Hi2 = i+2
3. Lo1 = 0, Hi1 = n-1, Lo2 = -i, Hi2 = i

5. "Words" made of matched parentheses can be ordered lexicographically if we consider  `"(" < ")"`.
e.g.` ((())) < (()()) < (())() < ()(()) < ()()()`
If the string S contains a word of `n' pairs of matched parantheses, give an algorithm to change S so that it contains the next word in lexicographical order.

© L. Allison 2002, School of Computer Science and Software Engineering, Monash University, Australia 3800.
Created with "vi (Linux & IRIX)",   charset=iso-8859-1