To figure out what method does
hello
i am currently trying to figure out what a certain method does so as to proceed to annotate it, providing a post condition and loop invariant.
what i figured out:
on a positive and negative array and all positive x becomes positive upon the first time the minimum is encountered but when the array is all negative x and y are the sum of the elements
anyone see another approach to analyzing to this method?
thank you for your time
Code:
public class funA{
public static void functionOfA( int a[]){
int MAXINT= Integer.MAX_VALUE;
int x= MAXINT;
int y= MAXINT;
int i= 0;
int n= a.length;
while( i!= n){
if( y== MAXINT){
//y= MAXINT
y= a[i];
//y= a[i]
x= min( x, y);
//x= min(x,y)
System.out.println("x: " +x + " y: "+ y);
i= i+1;
//y= a[i-1];
} else{
y= min( y+ a[i], a[i]);
// y= min( y+ a[i], a[i]);
x= min( x, y);
//x= min( x, y);
System.out.println("x: " +x + " y: "+ y);
i= i+1;
// y= min( y+ a[i-1], a[i-1]);
}
}
System.out.println( x+ " "+ y);
}
public static int min( int x, int y){
if( x< y){
return x;
}
else{
return y;
}
}
public static void main ( String args[]){
int a[]= new int[] {0,-9,3,2,-4,3,-12,3,1};
functionOfA( a);
for( int i= 0; i< a.length; i++){
System.out.print( a[i]+ " ");
}
System.out.println();
System.out.println();
int a2[]= new int[] {-9,-3,-10,-4,-2};
functionOfA( a2);
for( int i2= 0; i2< a2.length; i2++){
System.out.print( a2[i2]+ " ");
}
}
}
by the way this was the given:
1. int x = MAXINT; int y = MAXINT; int i = 0;
2. int a[n];
3. while i != n {
4. y = min(y + a[i], a[i]);
5. x = min(x, y);
6. i = i + 1;
7. }