‹ Back to the paper
Question 2(iv)
3 marks
Computer ScienceISC 2023 Computer Science Paper 1Case basedRecursion
Answer the following.
int task(int m, int n) { if(m==n) return m; else if(m>n) return task(m-n, n); else return task(m…
int task(int m, int n)
{ if(m==n)
return m;
else if(m>n)
return task(m-n, n);
else
return task(m, n-m);
}The following function task() is a part of some class. Assume ‘m’ and ‘n’ are positive integers, greater than 0. Answer the questions given below along with dry run / working.
(a)[2]
What will the function task() return when the value of $m=30$ and $n=45$?
(b)[1]
What function does task( ) perform, apart from recursion?
Answer
Answer (a)
task(30, 45): m < n, so it calls task(30, 45 - 30) = task(30, 15)
task(30, 15): m > n, so it calls task(30 - 15, 15) = task(15, 15)
task(15, 15): m == n, so it returns 15
The function returns 15.
Answer (b)
It finds the HCF (GCD) of m and n by repeated subtraction.