Results 1 to 16 of 16
Thread: find maximum value from for loop
 05312010, 10:51 AM #1Member
 Join Date
 May 2010
 Posts
 7
 Rep Power
 0
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 vfmax for further calculation here! } }
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; 05312010 at 05:54 PM. Reason: code tags added
 05312010, 03:18 PM #2
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.
 05312010, 05:56 PM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
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.
 06012010, 03:01 AM #4Member
 Join Date
 May 2010
 Posts
 7
 Rep Power
 0
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.!!
 06012010, 03:25 AM #5
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
 06012010, 03:42 AM #6Member
 Join Date
 May 2010
 Posts
 7
 Rep Power
 0
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?
 06012010, 03:09 PM #7
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
 06022010, 12:51 PM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
 06022010, 01:50 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,951
 Blog Entries
 7
 Rep Power
 22
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; } }
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()); } }
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; 06022010 at 01:53 PM.
 06022010, 01:58 PM #10
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
I think now our OP need a better explanation on this. ;)
 06022010, 02:32 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,951
 Blog Entries
 7
 Rep Power
 22
 06022010, 03:08 PM #12
That solution would require that all variables be classes vs primitives.
 06022010, 03:11 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,951
 Blog Entries
 7
 Rep Power
 22
 06022010, 03:13 PM #14Senior Member
 Join Date
 Feb 2009
 Posts
 309
 Rep Power
 7
You would need to be using jdk1.5 or higher as to take advantage of the autoboxing and unboxing.
 06022010, 03:34 PM #15
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.
 06022010, 03:39 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,951
 Blog Entries
 7
 Rep Power
 22
Similar Threads

Calculating Maximum and Minimum
By Ejava in forum New To JavaReplies: 6Last Post: 03012010, 05:36 PM 
using loop to find duplicate
By gwithey in forum New To JavaReplies: 7Last Post: 03062009, 02:46 PM 
maximum view depth?
By Bit2_Gosu in forum New To JavaReplies: 0Last Post: 02162009, 03:32 PM 
Formula on Maximum loan..
By esh21 in forum NetBeansReplies: 4Last Post: 08142008, 02:27 PM 
To find the Maximum and Minimum in an Array of Strings
By luscious in forum JavaServer Pages (JSP) and JSTLReplies: 9Last Post: 07312008, 01:51 PM
Bookmarks