PRASHNIKAप्रश्निका
Back to the paper

The following function task() is a part of some class. Assume ‘m’ and ‘n’ are positive integers…

Computer Science20233 marksShort answer
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.
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);
}
(a)[2.0]
What will the function task() return when the value of $m=30$ and $n=45$?
(b)[1.0]
What function does task( ) perform, apart from recursion?

Answer

Answer (a)

AI
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)

AI
It finds the HCF (GCD) of m and n by repeated subtraction.
Recursion

From ISC 2023 Computer Science Paper 1, question 2(iv).