0 votes 0 votes Consider the following pseudo-code fragment, where $m$ is a non-negative integer that has been initialized: $p=0$ $k=0$ while $(k<m)$ $p=p+2^k$ $k=k+1$ end while Which of the following is a loop invariant for the while statement? (Note: a loop invariant for a while statement is an assertion that is true each time the guard is evaluated during the execution of the while statement). $p=2^k-1$ and $0 \leq k < m$ $p=2^{k+1}-1$ and $0 \leq k < m$ $p=2^k-1$ and $0 \leq k \leq m$ $p=2^{k+1}-1$ and $0 \leq k \leq m$ Unknown Category ugcnetcse-dec2018-paper2 + – Arjun asked Jan 2, 2019 • edited Jun 22, 2020 by soujanyareddy13 Arjun 6.5k views answer comment Share Follow See all 12 Comments See all 12 12 Comments reply Shaik Masthan commented Jan 2, 2019 reply Follow Share is it a or c ? as per me, it is C 0 votes 0 votes srestha commented Mar 12, 2019 reply Follow Share Ans A) try the code with some examples 0 votes 0 votes Shaik Masthan commented Mar 13, 2019 reply Follow Share mam, is it A or C? Check one more time ! 0 votes 0 votes Sidd_ commented Mar 13, 2019 reply Follow Share Option C should be the ans. 0 votes 0 votes abhishekmehta4u commented Mar 13, 2019 reply Follow Share I am also getting c 0 votes 0 votes srestha commented Mar 13, 2019 reply Follow Share how C)? k=0 $p=0+2^{0}=1$, that means $p=2^{k+1}-1=2^{1}-1=1$ k=1 $p=1+2^{1}=3$, that means $p=2^{k+1}-1=2^{2}-1=3$ So, A) will be ans is it not? Someone please elaborate , if anything wrong 0 votes 0 votes srestha commented Mar 13, 2019 reply Follow Share @Shaik Masthan k is incrementing after p evaluated right? So, p is depending upon previous k value is it not? 0 votes 0 votes Shaik Masthan commented Mar 13, 2019 reply Follow Share check while entering in the loop on every iteration 0 votes 0 votes srestha commented Mar 13, 2019 reply Follow Share yes Say m=3 p=0; k=0; entering inside while loop $p=0+2^{0}=1$ -------------------------------- Still k is not incremented now chk option $p=2^{k+1}-1=2^{0+1}-1=1$ which is option A) where is my mistake? 0 votes 0 votes Shaik Masthan commented Mar 13, 2019 reply Follow Share mam, why you checking after executing the line p = p + 2$^k$ ? check before that line tooo... then p=0,k=0 ==> option c is matching 2 votes 2 votes srestha commented Mar 13, 2019 reply Follow Share ok, thank u that means, loop invariant will be executed before any condition will be evaluated right? 0 votes 0 votes Shaik Masthan commented Mar 13, 2019 reply Follow Share before any statement of the loop evaluated 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes $k=0,p=0$ After $first$ iteration, $p=0+2^0=1,\,k=1$ After $n^{th}$ iteration, $p=2^0+2^1+...+2^{n-1}=2^{n}-1,k=n$ As, the loop will go for $m$ iterations, so, $0\leq k\leq m$ So, $(C)$ should be the answer Shobhit Joshi answered Jan 3, 2019 Shobhit Joshi comment Share Follow See all 0 reply Please log in or register to add a comment.