What sequence of numbers would be printed by the followingrecursive procedure if
ID: 3611177 • Letter: W
Question
What sequence of numbers would be printed by the followingrecursive procedure if we started it with N assigned the value1?procedure Exercise(N)
print the value of N;
if (n<3) then (apply the procedure Excersise to thevalue N+1);
print the value of N.
The answer to this given is 1,2,3,3,2,1,..(can u please explain mewhy the numbers are given backwards?, is that because of the copiesthat return back the result to the original copy or somethingelse?
then after that is required what could be the termination conditionin the recursive procedure in the thing above.
Explanation / Answer
The numbers are printed back as the third line of the procedurealso has print the value of Nstatement. Now if the function is called with N=1, the overall fucntion callsand output will happen like this: Exercise(1): Prints (1) calls Exercise(1+1) ----------> Prints(2) calls Exercise(2+1)----------------------------> prints(3) N is not less than 3..no further Function call Prints(N which is 3 in this call)----| Print(N..which is 2 inRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.