- Is the following program segment correct with respect to the pre-condition
y >= 5 and the post-condition z >= 65? Please give the reason why or why not.
int x = 13;
z = x * y;
if (y > 0)
z++;
else
z--;
- Given the pre-condition -2 <= y < 4, what is the post-condition for z
in the code segment shown below?
int z = 0;
if (y < 0)
z = y * -y;
else if (y == 0)
z = 0;
else
z = y * y;
- Given the post-condition -4 <= z <= 4, what is the pre-condition for x
in the code segment shown?
z = x * x + 2 * x - 4;
- What is the pre-condition for x, if the post-condition is z >= 25
for the code segment shown?
y = 15;
z = 100 - x - (2 * y);
- What are the loop invariants for all variables in the code segment shown?
a = 5;
b = 2;
c = -3;
while (c < 0) {
b = b + 2 * a;
a = b / 3;
c++;
}
- Given the pre-conditions a = 5 and b = -3, what are the loop invariants
for all variables except the loop counter in the code segment shown?
c = 2;
for (int i = -2; i < 3; i++) {
c += a-- + (b * i);
}
The answers will be discussed at the end of class.