Number Array

• January 15th, 2011, 02:18 PM
TheJavaGuy
Number Array
So I have an array of values:
dht = new int[] { 41, 68, 77, 74, 74, 68, 88 };

And I want to return the index/of the position of the highest number. In this case 88.
I understand it is in the 6th position.

So,
Code :

```public static int hottestDay( int[] temp) { int hottest = 88; for(int i; i < dht.length ; i++){ if( dht[i] == hottest) return i; }```

I am more interested in how you solve, than the actual answer itself. If someone could get on AIM and walk me through this and other stuff, I would be so happy.
• January 15th, 2011, 02:59 PM
newbie
Re: Number Array
Code java:

``` int[] test = {1, 2, 3, 4}; int highestVal = 0; int position = -1; for (int i = 0; i < test.length; i++) { if (test[i] > highestVal) { highestVal = test[i]; position = i; //Kinda tells position of highest val }   } if (highestVal == 0 || position == -1) { System.out.println("Test Failed?"); } else { System.out.println("Highest Array Element is " + highestVal); }```

That might be what you're kind of after? It stores highest value so far and its position.
• January 15th, 2011, 03:18 PM
javapenguin
Re: Number Array
Kinda of.
Code java:

```int[] test = {1, 2, 3, 4}; int highestVal = 0;     int position = -1; for (int i = 0; i < test.length; i++) { if (test[i] > highestVal) { highestVal = test[i]; position = i; //Kinda tells position of highest val }   } if (highestVal == 0 || position == -1) { System.out.println("Test Failed?"); } else { System.out.println("Highest Array Element is " + highestVal); }   return position; }```

That would work, but what if
Code java:

`int[] test = {-5,-4,-3,-2,-1};`

It'll return position -1.
However, the highest value would be -1, which is at position 4.
• January 15th, 2011, 03:22 PM
javapenguin
Re: Number Array
Change newbie's code to set it to set highest value to the first index of the array. Then compare it like before. This way, it can handle negatives.
• January 17th, 2011, 03:59 AM
helloworld922
Re: Number Array
indices != values

newbie's code almost works. The only case it won't work is if the highest value happens to be 0. There's no need to store the highest value and the position. Furthermore, except for an array of size 0 there is always guaranteed to be at least one value which is the largest (or in this case, most positive). How you handle multiple highest values in the list is up to you, though usually you would just return the first one you find.

Code Java:

```public static int max(int[] numbers) { if(numbers.length == 0) { return -1; // no numbers to find the max of } int highestIndex = 0; for(int i = 1; i < numbers.length; ++i) { if(numbers[i] > numbers[highestIndex) { highestIndex = i; } } return highestIndex; }```

Test cases:
Code Java:

```System.out.println(max(new int[]{1, 2, 3, 4})); // prints 3 System.out.println(max(new int[]{})); // prints -1 System.out.println(max(new int[]{-1, 0})); // prints 1 System.out.println(max(new int[]{-1, -3, -6 ,-1, 3, 100, 50})); // prints 5```