in Programming in C edited by
1,871 views
2 votes
2 votes

​​Consider the following $\mathrm{C}$ function definition.

int $\mathrm{f}$ (int $\mathrm{x}$, int $\mathrm{y})$ {

for (int $i=0 ; i<y ; i++$ ) {

$\mathrm{x}=\mathrm{x}+\mathrm{x}+\mathrm{y}$;

}

return $\mathrm{x}$;

}

Which of the following statements is/are TRUE about the above function?

  1. If the inputs are $x=20, y=10$, then the return value is greater than $2^{20}$
  2. If the inputs are $x=20, y=20$, then the return value is greater than $2^{20}$
  3. If the inputs are $x=20, y=10$, then the return value is less than $2^{10}$
  4. If the inputs are $x=10, y=20$, then the return value is greater than $2^{20}$
in Programming in C edited by
by
1.9k views

1 Answer

1 vote
1 vote
Let $t_i$ be the value of $x$ after finishing the $i^{th}$ loop. Therefore, the returned value is $t_{y-1}$.

$t_0 = 2x + y$

$t_1 = 2t_0 + y = 2(2x + y) + y = 4x + 3y$

$t_2 = 2t_1 + y = 2(4x + 3y) + y = 8x + 7y$

$t_i = 2^{i+1}x + (2^{i+1} - 1)y = 2^{i+1}(x+y) - y$

Therefore, returned value = $t_{y-1} = 2^{y}(x+y) - y$

A. When $x=20, y=10 \implies t_{9} = 2^{10}(30) - 10 \ngtr 2^{20}$

B. When $x=20, y=20 \implies t_{19} = 2^{20}(40) - 20 > 2^{20}$

C. When $x=20, y=10 \implies t_{9} = 2^{10}(30) - 10 \nless 2^{10}$

D. When $x=10, y=20 \implies t_{19} = 2^{20}(30) - 20 > 2^{20}$

Answer - B, D
Answer:

Related questions