April
1999 QUESTION 2 Total Marks: 20 Marks |
Click here to access other
questions
GRADE A
|
(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] | |
(i) Recursive function
is a kind of function that can call itself until the condition is met. (ii) Iterative function or procedure is a kind of function that have a loop that using while loop or for loop and the loop will stop until a condition is met.
|
||
(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] | |
(i) void levels (int a , int b)
} (ii) void levels (int a, int b)
}
|
||
(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] | |
(i) The argument to
the switch statement should be an Integral type, such as an integer. (ii) The strcmp is more suitable for achieving the desired functionally because strcmp can compare string mystring with other string with correctly.
|
||
(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] | |
(i) Running task (ii) Restarting task (iii) Wakeup task (iv) Delete task (v) Running task |