Results 1 to 16 of 16
  1. #1
    napi1234 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Unhappy find maximum value from for loop

    hello there,

    I am new to java language..and i have some trouble in finding maximum value for the output of the calculation from for loop.:confused:

    this is my code:

    Java Code:
    import static java.lang.Math.*;
    import java.util.*;
    
    public class sort2
    {
       public static void main(String arg[])
       {
        double [] x = new double[10];
        double [] y = new double[10];
        double [] v = new double[10];
        double dx, dy, dv;
        double xy, a, vf;
        int j;
    
    
    	x[1]=  0; y[1]=  0; v[1]= 15;
    	x[2]=  0; y[2]= -1; v[2]= 7;
    	x[3]=  1; y[3]= -1; v[3]= 6;
    	x[4]= -1; y[4]=  0; v[4]= 8;
    	x[5]= -1; y[5]= -1; v[5]= 5;
    	x[6]=  1; y[6]=  0; v[6]= 11;
    	x[7]= -1; y[7]=  1; v[7]= 8;
    	x[8]=  0; y[8]=  1; v[8]= 9;
    	x[9]=  1; y[9]=  1; v[9]= 10;
    
    
    	for(j=2; j<=9; j++){
    
    	dx=  x[1] - x[j];
    	dy=  y[1] - y[j];
    	dv=  v[1] - v[j];
    
    	xy= pow((dx*dx+dy*dy),0.5); // (vx^2 + vy^2) raised to the power of 1/2
    	
    	vf = dv/xy;	    }
         //here where i am stuck!! how to pick the max value of vf after for loop
        //need vf-max for further calculation here!
         }
    }
    actually i am searching for gradient of v which is vf=dv/xy around point (x[1], y[1]).
    i used 'for loop' to calculate each gradient vf, and then to find the maximum value of vf without writing it because i am going to used the max vf for further calculation.

    Thanks in advance!

    napi,
    Last edited by Eranga; 05-31-2010 at 06:54 PM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    To find the max value of something, save the first value then compare future values against it. If they are larger, replace with new larger value.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Just think that how you find the maximum number from a set of numbers. Same thing you've to do is, which is Norm explain above.

  4. #4
    napi1234 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    ok, i have the idea but can some one shows jow to do it the right way?
    i have try many things yet because i am new to java and new to programming language so this part i really have some trouble.!!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Something like this.
    Java Code:
    maxVal = first value;
    loop
      nxtVal = getNextValue() // or however you get the next value
      if(nxtVal > maxVal)  // test if new value is greater than current max
        maxVal = nxtVal    // if it is, replace current max
    
    end loop

  6. #6
    napi1234 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    first of all thanks,

    ok actually i have try this but, i got stuck in how to actually declare the first value in my case vf, and also the next value in my case they were both vf?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    If the values of vf can have the smallest possible double number then you might need to use a boolean flag to test for first case.
    If the values of vf can NEVER be the smallest possible number, the set maxVf to the smallest number and test future values of vf against that. They should ALL be larger than the initial value.
    Java Code:
    maxVf = smallest possible number;
    loop
      vf = next value
      if( vf > maxVf)  // is this one larger?
        maxVf = vf;    //yes, save new max value
    ...
    end loop

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by napi1234 View Post
    first of all thanks,

    ok actually i have try this but, i got stuck in how to actually declare the first value in my case vf, and also the next value in my case they were both vf?
    Pick the first value and keep it, by assign to a variable. Then compare with the next value and if the compared one is the larger then assign that value to your variable. Else keep the same value as it is.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Why doesn't any of you take the object oriented approach? Have a look at this class:

    Java Code:
    class Max {
    	
    	private Comparable max;
    	
    	public void feed(Comparable entry) {
    		if (max == null || entry.compareTo(max) > 0) max= entry;
    	}
    
    	public Comparable getMax() { return max; }
    }
    It can be used like this:

    Java Code:
    public class T {
    	public static void main(String[] args) {
    		int[] a= { 4, 3, 5, 2, 1 };
    		Max imax= new Max();
    		for (int i : a)
    			imax.feed(i);
    		System.out.println("max: "+imax.getMax());
    		
    		String[] s = { "Fred", "Barney", "Wilma", "Betty" };
    		Max smax= new Max();
    		for (String is : s)
    			smax.feed(is);
    		System.out.println("max: "+smax.getMax());
    	}
    }
    kind regards,

    Jos

    ps. I could've used generics to make the compilere forbid me to feed a number to a Max<String> class and vice versa, but you get the idea.
    Last edited by JosAH; 06-02-2010 at 02:53 PM.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Eranga View Post
    I think now our OP need a better explanation on this. ;)
    For seven lines of simple code and just a three line method? You must be kidding me ...

    kind regards,

    Jos

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    That solution would require that all variables be classes vs primitives.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    That solution would require that all variables be classes vs primitives.
    See my example main( ... ) method; it uses (primitive) ints as well.

    kind regards,

    Jos

  14. #14
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    You would need to be using jdk1.5 or higher as to take advantage of the autoboxing and unboxing.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Thanks StormyWaters.
    I'd forgotten about boxing. I really don't like it when the compiler does things for you like that. Sometimes it turns into the compiler doing things to you that you don't expect. And since it doesn't tell you it is doing it, it can take a while to find the problem.
    There should be a compiler option to turn it off.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    Thanks StormyWaters.
    I'd forgotten about boxing. I really don't like it when the compiler does things for you like that. Sometimes it turns into the compiler doing things to you that you don't expect. And since it doesn't tell you it is doing it, it can take a while to find the problem.
    There should be a compiler option to turn it off.
    Autoboxing is a mixed blessing; I take it that you don't like enhanced for loops either? (it uses Iterables and generates Iterators).

    kind regards,

    Jos

Similar Threads

  1. Calculating Maximum and Minimum
    By Ejava in forum New To Java
    Replies: 6
    Last Post: 03-01-2010, 05:36 PM
  2. using loop to find duplicate
    By gwithey in forum New To Java
    Replies: 7
    Last Post: 03-06-2009, 02:46 PM
  3. maximum view depth?
    By Bit2_Gosu in forum New To Java
    Replies: 0
    Last Post: 02-16-2009, 03:32 PM
  4. Formula on Maximum loan..
    By esh21 in forum NetBeans
    Replies: 4
    Last Post: 08-14-2008, 03:27 PM
  5. To find the Maximum and Minimum in an Array of Strings
    By luscious in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 07-31-2008, 02:51 PM

Posting Permissions

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