retagged by
409 views
3 votes
3 votes
#include<stdio.h>
int test(int *a, int *b) {
    int c = *a-*b;
    if (c<0)
        return 0;
    else
        return (1 + test(&c, b));
}

void main()
{
    int x = 15;
    int y = 4;
    int a = test(&x,&y);
    printf("%d", a);
}

  1. $2$
  2. $3$
  3. $4$
  4. Run time error since we can not pass the address of local variable c in the function test.
retagged by

1 Answer

4 votes
4 votes

test(int *a, int *b) $\implies$ In this function, we’re recursively subtracting *b from *a until (*a – *b) becomes less than 0.

ie int c = *a – *b – *b – *b – … – *b < 0 $\equiv$ *a – n * *b < 0 $\equiv$ *a < n * *b (n = smallest integer that satisfies this condition)

test( … ) returns ( 1 + next_call_of test( … ) ) or 0 ie test( … ) returns 1 + 1 + 1 + … + 1 ( n – 1 times) + 0 = n – 1.

test(&x, &y) will return (n – 1) such that n is smallest integer that satisfies x < n * y.

Examples, 

  • Consider, x = 5, y = 4, then test(&x,&y) will return 1, since x < 2 * y.
  • Consider, x = 99, y = 4, then test(&x,&y) will return 24, since x < 25 * y.
  • Consider, x = 100, y = 4, then test(&x,&y) will return 25, since x < 26 * y.

In given question, x = 15, y = 4, then test(&x,&y) will return 3, since x < 4 * y.

Answer :- B.

Answer:

Related questions