# Thread: find maximum value from for loop

1. Member
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 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,
Last edited by Eranga; 05-31-2010 at 05:54 PM. Reason: code tags added

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.

3. 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. Member
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.!!

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```

6. Member
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?

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```

8. 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.

9. 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 01:53 PM.

10. 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

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

12. 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

13. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
8
You would need to be using jdk1.5 or higher as to take advantage of the autoboxing and unboxing.

14. 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.

15. 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

#### Posting Permissions

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