in Programming in C edited by
15,458 views
54 votes
54 votes

Consider the following pseudo code, where $x$ and $y$ are positive integers.

begin 
    q := 0 
    r := x 
   while r ≥ y do 
      begin 
      r := r - y 
      q := q + 1 
    end 
end

The post condition that needs to be satisfied after the program terminates is

  1. $\{ r = qx + y \wedge r < y\}$
  2. $\{ x = qy  + r \wedge r < y\}$
  3. $\{ y = qx + r \wedge 0 < r < y\}$
  4. $\{ q + 1 < r - y \wedge y > 0\}$
in Programming in C edited by
15.5k views

3 Comments

1
1
edited by

“ ^ “ is EXOR operator in C language.

take y =2 x= 11 and simply you get option B as answer  

0
0

Concept in Loop Invariant Problems:

Just take arbitrary values of variables, here x and y as q is already given (0) and perform operations in while loop.

Satisfy the options with the updated values of variables.

 

0
0

7 Answers

70 votes
70 votes
Best answer

Correct Option: B

The loop terminates when $r < y$. So, $r < y$ is one post condition. 

In each iteration $q$ is incremented by $1$ and $y$ is subtracted from $r$. Initial value of $r$ is $x$. So, loop iterates $x/y$ times and $q$ will be equal to $x/y$ and $r = x\%y \Rightarrow x = qy + r;$ 

edited by
by

4 Comments

No that has to be AND but anyhow I don't think there is as such hard and fast rule, as people commonly write according to the convenience.

Only in standard things you will find the right way.
1
1
^ is EXOR operator in C language. You have to see the context.
0
0

@Arjun sir      @jeetsir.....ty for ur valuable comment.

1
1
16 votes
16 votes

Just take an example and try to solve it by using options:

suppose, x=10 and y=5

begin 
    q := 0  // q = 0
    r := x  // r = x i.e. r = 5 as value of taken as 10
   while r ≥ y do  // 10 ≥ 5 ; TRUE     (first iteration)
      begin 
      r := r - y  // r = 10 - 5 = 5
      q := q + 1  // q= 0 + 1 = 1
    end 
   while r ≥ y do // 5 ≥ 5 ; TRUE        (second iteration)
      begin 
      r := r - y // r = 5 - 5 = 0 
      q := q + 1 // q= 1 + 1 = 2
    end 
  while r ≥ y do // 0 ≥ 5 ; FALSE         (third iteration)
end

at the end of while loop

we get value of r = 0 and q = 2

x= 10 and y = 5

Now one by one put these values in given option 

and thus, option B satisfied i.e. 

{x=qy+r∧r<y}   // x = 2*5 + 0 = 10 and 0 < 5

2 Comments

@rishu_darkshadow

Good explanation

In this type of question, i always solve to assume the value, because I'm not comfortable to solve the question in the variable. 

3
3

so,nice explanation 

 

post condition :-

the loop terminates when ( r<y ) so one condition { r<y }

( q=q+1 ) q is increasing every time by 1 so q is simply counting loop count.

so r after execution

r = r - q y

(r=x) so

r = x - q y

x = r + q y

so option B

x = r + qy ^ r < y

0
0
14 votes
14 votes
post condition :-

the loop terminates when ( r<y ) so one condition { r<y }

( q=q+1 ) q is increasing every time by 1 so q is simply counting loop count.

so r after execution

r = r - q y

(r=x) so

r = x - q y

x = r + q y

so option B

x = r + qy ^ r < y

4 Comments

@flash12

in the given code , each time the condition in while is satisfied , we increment q. we can say that for n iteration of while loop , q get incremented by n. So  we can also say that the while loop has iterated q times (since q is equal to n).

hence we can write

r=x-qy or x=r+qy
0
0
^ is a bitwise operator "xor" and how can 8 = 8 ^ True?
0
0

so,nice explanation 

 

post condition :-

the loop terminates when ( r<y ) so one condition { r<y }

( q=q+1 ) q is increasing every time by 1 so q is simply counting loop count.

so r after execution

r = r - q y

(r=x) so

r = x - q y

x = r + q y

so option B

x = r + qy ^ r < y

0
0
4 votes
4 votes

we can solve by taking some values and after check out with options.

Answer:

Related questions