I've been assigned to find the loop invariant for the following code:
i=0;
j=n-1;
while(i < n/2){
l = A[i];
r = A[j];
A[j] = l;
A[i] = r;
j = j-1;
i = i+1;
}
I understand the code function (reverses array), the guard (i < n/2), and I can find different invariants (e.g. i+j = n-1), but I'm stuck finding anything useful that relates the precondition:
A[0], A[1] ... A[n-2], A[n-1]
to the postcondition:
A[n-1], A[n-2] ... A[1], A[0]
Can anyone help with this problem, or even generally how to find useful invariants for loops that operate on arrays?