Purpose: practice using invariant-assertion style to reason about program fragments
This is a team homework assignment. All work must be done by the team members together. All team members should understand all work submitted. If you have concerns about your own understanding or that of your teammates, please let me know. I want everyone to learn all they can in this course!
For each of the following program fragments, calculate the weakest pre-condition of the statement, given the post-condition.
Please note: You must have at least one assert clause between every line of code. That level of rigor will be necessary for the more complicated problems to come.
//@ assert ____________________________; x = 2*y + 3; //@ assert x == 13;
//@ assert ____________________________; x = x + y; //@ assert x < 2*y;
//@ assert ____________________________; j = j + 1; //@ assert j > 0;
//@ assert ____________________________; i = i + 1; //@ assert i <= n;
//@ assert ____________________________; a = a - b; //@ assert a > b;
//@ assert ____________________________; x = z - 3; y = x * z; //@ assert y > 0;
//@ assert ____________________________;
if (a > b) {
a = a - b;
} else {
b = b - a;
}
//@ assert a > 0 && b > 0;
//@ assert ____________________________;
if (i > 0) {
a = i;
}
//@ assert a == Math.abs(i);