edited by
37,223 views
64 votes
64 votes

Consider the following segment of C-code:

int j, n;
j = 1;
while (j <= n)
    j = j * 2;

The number of comparisons made in the execution of the loop for any $n > 0$ is:

  1. $\lceil \log_2n \rceil +1$

  2. $n$

  3. $\lceil \log_2n \rceil$

  4. $\lfloor \log_2n \rfloor +1$

edited by

15 Answers

0 votes
0 votes

due to the line "in the execution of the loop" the answer is D.

if total comparisons were asked then it will be floor (logn) +2

–1 votes
–1 votes
let n=8, then then value if j that would get compaired are 1,2,4,8...number of comparison=4

upper_bound( ln8 ) +1 = 4

let n=6, then number of comparison = 3

upper_bound( ln6 ) +1 = 3

which are satisfied by option A
reshown by
Answer:

Related questions

27 votes
27 votes
4 answers
13
Kathleen asked Sep 21, 2014
34,042 views
The message $11001001$ is to be transmitted using the CRC polynomial $x^3 +1$ to protect it from errors. The message that should be transmitted is:$11001001000$$110010010...
33 votes
33 votes
4 answers
15