/* PROOF OF CORRECTNESS: NEGATE(N): compute negative of a non-negative integer, e.g. NEGATE(10) = -10 */ #include int negate(int x) { int i,f; // PRE: x>=0 i = 0; f = x; // INVARIANT: while ( i < 2*x ) { f = f - 1; i = i + 1; } // INVARIANT: and EXIT: // POST: f=x-2*x return f; } int main() { printf("negate(+10)=%d\n",negate(10)); }