Consider the following function:
int SomeFunction (int x, int y)
{
if ((x==1) || (y==1)) return 1;
if (x==y) return x;
if (x > y) return SomeFunction(x-y, y);
if (y > x) return SomeFunction (x, y-x);
}
The value returned by is __________
View worked solution
Answer: 15 : 15
The given C function implements the subtraction-based Euclidean algorithm to find the Greatest Common Divisor (GCD) of two positive integers and , with an additional base case for when either number is (which also correctly returns , since ).
Let's trace the execution of SomeFunction(15, 255):
- Since , the function executes
SomeFunction(x, y-x), which translates toSomeFunction(15, 240). - This subtraction step continues repeatedly because at each step:
- When the arguments reach
SomeFunction(15, 15), the conditionif (x == y)is satisfied, and the function returnsx, which is .
Mathematically, the function computes:
Therefore, the value returned by SomeFunction(15, 255) is 15.
Free practice · Your answers on this page are not saved.
