April
1999 QUESTION 2 Total Marks: 20 Marks |
Click here to access other
questions
Click to access
|
(a) | (i) Define what is meant by a recursive function or procedure. | [2] |
(ii) Define what is meant by an iterative
function or procedure.
|
[2] | |
(b) | Assume the following output: Downwards: 0 of 3
|
|
(i) Using while loops, write an iterative definition of the procedure levels, that takes two integer arguments, such that a call to levels(4, 3) produces nothing and a call to levels(0, 3) produces the output above. | [4] | |
(ii) Write a recursive definition of the
procedure levels, that takes two integer arguments, such that an initial call to levels(4, 3) produces nothing and a call to levels(0, 3) produces the output above.
|
[4] | |
(c) | Assume the following function, which is intended
to return a range of values depending on the contents of the string mystring: int
foobar(char* mystring) {
|
|
(i) Explain why this function is incorrect? | [1] | |
(ii) Identify a construct which would be more
suitable for achieving the desired functionality, and explain why this is the case.
|
[2] | |
(d) | Given the following procedure: void foobar(int state, int signal, int timeout) { default: case 2: trace the following calls, and produce their output:
|
|
(i) foobar(2, 0, 0); | [1] | |
(ii) foobar(1, 1, 0); | [1] | |
(iii) foobar(1, 0, 10); | [1] | |
(iv) foobar(3, 0, 10); | [1] | |
(v) foobar(2, 1, 10); | [1] |