retagged by
5,357 views
12 votes
12 votes

Consider the following $\text{ANSI C}$ 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 $\textrm{SomeFunction(15, 255)}$ is __________

retagged by

5 Answers

Best answer
6 votes
6 votes
This function is calculating the $\text{GCD}$ of the two numbers by repeated subtraction.

$\text{GCD}(15, 255) = 15.$ So it’ll return $15.$
edited by
2 votes
2 votes

1st call SomeFunction(15, 255)

2nd call SomeFunction(15, 240)

3rd call SomeFunction(15, 225)

……...

Now for each call 15 is subtracted from 255 and 255 is a multiple of 15.

once the call reaches SomeFunction(15, 15) it’ll return x i.e 15.

So output is 15.

Note: This is a method to find GCD by repeated subtraction.

0 votes
0 votes

1st call SomeFunction(15, 255)

2nd call SomeFunction(15, 240)

similarly ,

………….. function calling till it reaches to call Somefunction(15,15).

so, It will Return 15 as output .

Answer:

Related questions

8.7k
views
3 answers
16 votes
Arjun asked Feb 18, 2021
8,696 views
Consider the following $\text{ANSI C}$ program#include <stdio.h> int foo(int x, int y, int q) { if ((x<=0) && (y<=0)) return q; if (x<=0) return ... 15, 10); printf( %d , r); return 0; }The output of the program upon execution is _________
30.9k
views
11 answers
41 votes
Arjun asked Feb 18, 2021
30,900 views
Consider the following $\text{ANSI C}$ program.#include <stdio.h> int main() { int arr[4][5]; int i, j; for (i=0; i<4; i++) { for (j=0; j<5; j++) ... $20$24$30$
7.5k
views
3 answers
15 votes
Arjun asked Feb 18, 2021
7,513 views
In a directed acyclic graph with a source vertex $\textsf{s}$, the $\textit{quality-score}$ of a directed path is defined to be the product of the ... .The sum of the quality-scores of all vertices on the graph shown above is _______
11.6k
views
3 answers
14 votes
Arjun asked Feb 18, 2021
11,633 views
Consider a complete binary tree with $7$ nodes. Let $A$ denote the set of first $3$ elements obtained by performing Breadth-First Search $\text{(BFS)}$ starting ... $\mid A-B \mid $ is _____________