find maximum value from for loop

• 05-31-2010, 10:51 AM
napi1234
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:

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.

napi,
• 05-31-2010, 03:18 PM
Norm
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.
• 05-31-2010, 05:56 PM
Eranga
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.
• 06-01-2010, 03:01 AM
napi1234
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.!!
• 06-01-2010, 03:25 AM
Norm
Something like this.
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```
• 06-01-2010, 03:42 AM
napi1234
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?
• 06-01-2010, 03:09 PM
Norm
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.
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```
• 06-02-2010, 12:51 PM
Eranga
Quote:

Originally Posted by napi1234
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.
• 06-02-2010, 01:50 PM
JosAH
Why doesn't any of you take the object oriented approach? Have a look at this class:

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:

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.
• 06-02-2010, 01:58 PM
Eranga
I think now our OP need a better explanation on this. ;)
• 06-02-2010, 02:32 PM
JosAH
Quote:

Originally Posted by Eranga
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
• 06-02-2010, 03:08 PM
Norm
That solution would require that all variables be classes vs primitives.
• 06-02-2010, 03:11 PM
JosAH
Quote:

Originally Posted by Norm
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
• 06-02-2010, 03:13 PM
StormyWaters
You would need to be using jdk1.5 or higher as to take advantage of the autoboxing and unboxing.
• 06-02-2010, 03:34 PM
Norm
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.
• 06-02-2010, 03:39 PM
JosAH
Quote:

Originally Posted by Norm
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