Results 1 to 1 of 1
Thread: To figure out what method does
- 03-14-2012, 09:06 PM #1
Member
- Join Date
- Nov 2010
- Location
- Beirut, Lebanon
- Posts
- 36
- Rep Power
- 0
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
by the way this was the given: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]+ " "); } } }
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
-
can someone please help me to figure this out >.<
By lyzeofkiel in forum New To JavaReplies: 11Last Post: 07-24-2011, 11:34 AM -
Can't figure out
By beauti477 in forum New To JavaReplies: 3Last Post: 07-20-2011, 03:58 PM -
New to Applets..How do you draw the figure in the paint method multiple times?
By etranman1 in forum Java AppletsReplies: 7Last Post: 05-05-2011, 04:26 PM -
trying to figure out how to minus the place of an int parameter in a method.
By Dev23 in forum New To JavaReplies: 2Last Post: 04-17-2011, 10:17 AM -
Can't figure out how to use JFrame's add() method right
By Fortu in forum New To JavaReplies: 9Last Post: 01-17-2011, 12:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks