Results 1 to 1 of 1
  1. #1
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default 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
    Java 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. }
    Last edited by farahm; 03-14-2012 at 09:14 PM.

Similar Threads

  1. can someone please help me to figure this out >.<
    By lyzeofkiel in forum New To Java
    Replies: 11
    Last Post: 07-24-2011, 11:34 AM
  2. Can't figure out
    By beauti477 in forum New To Java
    Replies: 3
    Last Post: 07-20-2011, 03:58 PM
  3. Replies: 7
    Last Post: 05-05-2011, 04:26 PM
  4. Replies: 2
    Last Post: 04-17-2011, 10:17 AM
  5. Replies: 9
    Last Post: 01-17-2011, 12:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •