Maximum of an Array


Document last modified: 

ESC

Example - returns the maximum value in an array.

class MaxPk {

//@ pre true;
//@ post (\result >= x && \result >= y );

    	int maxOf(int x, int y) {
        		if (x > y)
            		return x;
        		else
            		return y;
	}
          int n;							// n defined globally for use in pre/post conditions

//@ pre A.length > 0 && A != null;
//@ post (\forall int j; 0 <= j && j <= n; A[j] <= \result );

	int maximum(int A[]) {

		int i=0;
		int Pk = A[i];
        		int n = A.length-1;

		//@ loop_invariant (\forall int j; 0 <=j && j <= i; A[j] <= Pk && i <= n);

		while (i < n) {
	           	Pk = maxOf(Pk, A[i+1]);
            		i = i + 1;
        		}  
	        	return Pk;
	}
}

Question 2.9.1

  1. What is the effect of removing the precondition A != null?
     
  2. What is violated by changing the postcondition to:

    //@ post (\forall int j; 0 <= j && j <= n; A[j] == \result );

  3. What would be violated by changing the loop starting point to:

    i = 2;

  4. What is violated by changing the loop-invariant to:

    //@ loop_invariant (\forall int j; 0 <=j && j <= i; A[j] < Pk && i <= n);

ESCjava warning:

MaxPk.java:25: Warning: Loop invariant possibly does not hold (LoopInv)
       while (i < n) {
         ^
Associated declaration is "MaxPk.java", line 24, col 6:
      //@ loop_invariant (\forall int j; 0 <= j && j <= i; A ...
         ^

5. Why is this loop-invariant correct but not useful?

//@ loop_invariant (\forall int j; 0 <=j && j <= i; i <= n);

        while (i < n) {
           Pk = maxOf(Pk, A[i+1]);
            i = i + 1;
        } 
 

Example - returns the index of the maximum value in an array.

Pragmas define pre and postconditions and loop-invariants.
 

Initialization, Maintainence and Termination
 

(\forall int x ; range(x); p(x) ) Universal quantifier over range(x) for quantifier expression p(x)

 

class Maximum {

//@ pre A != null;
//@ post (\forall int k; 0 <= k && k < A.length; A[\result] >= A[k]);

       int maximum(int A[]) {

         int i=0;
         int maxIndex=0;

         /*@ loop_invariant
                     (\forall int j;
                                0 <= j && j < i && 0 <= maxIndex && maxIndex < i;
                                A[maxIndex] >= A[j]);
         *@/

         for( i=0; i<A.length; i++)
             if (A[i] > A[maxIndex])
                  maxIndex = i;
         return maxIndex;
     }
}

Question 2.9.2

  1. What is the effect of removing the precondition A != null?
     
  2. What is violated by changing the postcondition to:

    //@ post (\forall int k; 0 <= k && k < A.length; A[\result] > A[k]);

  3. What would be violated by changing the loop to:

    for( i=5; i<A.length; i++)

  4. What is violated by changing the loop-invariant to:

    /*@ loop_invariant
                   (\forall int j;
                              0<=j && j < i && 0 <= maxIndex && maxIndex < i;
                              A[maxIndex] > A[j]);
     *@/

Example

ESCjava warns that the postcondition below may not be true because k quantifies non-existent elements of A when -1.

//@ post (\forall int k; -1 <= k && k < A.length; A[\result] >= A[k]);
 

Maximum.java:16: Warning: Postcondition possibly not established (Post)

Associated declaration is "Maximum.java", line 4, col 4:
//@ post (\forall int k; -1 <= k && k < A.length; A[\result] >= A[k]); ...
^