|
-
Mar 31st, 2006, 12:45 PM
#1
Thread Starter
Junior Member
recursion trace task - new member
i am unsure if this is the right place to post but what i am about to ask looks very simple compared to what you guys post.
i have been set the following task:
This is an algorithm trace task. No implementation is required.
Read the following algorithm for the recursive function called Curiosity that needs two integers as
inputs and returns an integer value.
In the algorithm, MOD is an operator. It works with two integers to give the remainder when the first
integer is divided by the second.
This means that (example)
17 MOD 7 is 3
72 MOD 15 is 12
FUNCTION Curiosity(X:Integer;Y:Integer):Integer;
IF Y = 0 THEN
Curiosity = X
ELSE
Output Y and (X MOD Y)
Curiosity = Curiosity(Y,X MOD Y)
ENDIF
END Curiosity
(a) Write down the output when the function is called with
(i) Output ‘The answer is ’, Curiosity(120,42)
(ii) Output ‘The answer is ’, Curiosity(221,153)
(b) The function is called with
Output ‘The answer is ’, Curiosity(30,24)
Write down in full each line that is executed, but replace each variable with its actual value
and indicate on the IF statement line whether the result of the test is true or false.
(c) Describe the purpose of the algorithm.
(d) Rewrite the recursive function as an iterative algorithm.
i would be grateful if you could answer any parts, just to let you know i have the answers 6 and 17 for the first parts, however i am confused if this is correct as it is a recursive function. pls help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|