# Getting the Index of the Largest Number in an Array

Page 1 of 1

## 8 Replies - 5983 Views - Last Post: 20 November 2011 - 05:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=256513&amp;s=f9870d72287b0e487b25542db6a6174e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 welshej

• D.I.C Head

Reputation: -2
• Posts: 82
• Joined: 18-November 11

# Getting the Index of the Largest Number in an Array

Posted 20 November 2011 - 04:24 PM

I'm having a hard time getting the index of the largest number in an array. I think I've got all the other methods down(the one I'm taking issue with is indexOfMaxInRange(int[] a)), but I thought I'd include them in order for you guys to get a better understanding of my knowledge of Java (I'm fairly new).

For whatever reason, the when I run this it always displays a number between 0-3, seemingly at random... Any help you could give me would be much appreciated.

```import java.util.*;

public class Ex93 {
private static final int q = 0;
public static void main(String[] args) {

int rndArray[] = randomArray(10);
printArray(rndArray);
int indexMax = indexOfMaxInRange(rndArray);
System.out.println("The largest number is in position 3" + indexMax);
}
public static int[] randomArray (int n) {
int[] a = new int[n];
for (int i = 0; i<a.length; i++) {
a[i] = randomInt (0, 100);
}
return a;
}
private static int randomInt(int low, int high) {
int randReturn = 0;

randReturn = (int) (high * Math.random()) + 1;

return randReturn;
}
public static void printArray (int[] a) {
for (int i = 0; i<a.length; i++) {
System.out.println (a[i]);
}
}
public static int indexOfMaxInRange(int[] a) {
double[] b = new double [a.length];
int i = 0;
while (i < 4) {
b[i] = a[i];
i++;
}
Arrays.sort(B)/>;
int max = (int) b[b.length-1];

int z=0;
while (a[z] !=max) {
z++;
}

return z;

}
}

```

Is This A Good Question/Topic? 0

## Replies To: Getting the Index of the Largest Number in an Array

### #2 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Getting the Index of the Largest Number in an Array

Posted 20 November 2011 - 04:34 PM

Rethink your algorithm. All you have to do is:

```assign a temporary variable, temp, to a minimum value
for each element of the array:
if an element of the array is greater than temp
temp = that value
return temp
```

Was This Post Helpful? 1

### #3 welshej

• D.I.C Head

Reputation: -2
• Posts: 82
• Joined: 18-November 11

## Re: Getting the Index of the Largest Number in an Array

Posted 20 November 2011 - 04:47 PM

However, that would only work if I knew how many elements would be in the array beforehand.
Was This Post Helpful? 0

### #4 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Getting the Index of the Largest Number in an Array

Posted 20 November 2011 - 04:55 PM

Can't you use Array.length?
Was This Post Helpful? 0

### #5 welshej

• D.I.C Head

Reputation: -2
• Posts: 82
• Joined: 18-November 11

## Re: Getting the Index of the Largest Number in an Array

Posted 20 November 2011 - 05:04 PM

assign a temporary variable, temp, to a minimum value

You'd need to do:
int first;
int second;
...
int last;

Which won't work unless I know how big the array is.

Besides, I don't see why my way won't work. I create a temporary array, which I sort, then I take the largest number from that and set it equal to a variable max. After this I go back to working with my original array, and I use a loop to say, increment up the array until the maximum value equals the value of the array at that specific index. Once it does, return the index number.

I am fully aware that there may be many other, probably better ways to accomplish this. However, if at all possible, I'd like to stick to the general algorithim I have created thus far.

Thanks,
Ethan
Was This Post Helpful? 0

### #6 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Getting the Index of the Largest Number in an Array

Posted 20 November 2011 - 05:23 PM

Let's look at your algorithm:
```	public static int indexOfMaxInRange(int[] a) {
double[] b = new double [a.length];
int i = 0;
while (i < 4) {
b[i] = a[i];
i++;
}
Arrays.sort(B)/>;
int max = (int) b[b.length-1];

int z=0;
while (a[z] !=max) {
z++;
}

return z;

}
```

Here's what it does:
Creates a new array, b, of doubles equal in length to the argument array, a. (Why doubles when you know the argument array is ints?)

Assigns the first 3 values of the argument array, a, to the array b. Why 3 values? What will be in the remaining values of b?

Sorts array b.

Assigns the value the last index of b to an int value, max.

Assigns zero to a new int, z.

Compares the value of array a at index z to the variable max, the value of the last index of array b. If the value of the a array at that index doesn't equal max, the while loop exits. Otherwise, the value of z is incremented.

Return the value of z.

It's clear to me that the value of z is not related to the maximum value in either array a or b. Is it now clear to you?

This post has been edited by GregBrannon: 20 November 2011 - 05:24 PM

Was This Post Helpful? 1

### #7 welshej

• D.I.C Head

Reputation: -2
• Posts: 82
• Joined: 18-November 11

## Re: Getting the Index of the Largest Number in an Array

Posted 20 November 2011 - 05:23 PM

Looking at your code made me double check my algorithm. Turns out I put a '4' where I meant to put a.length. Thanks everyone!
Was This Post Helpful? 0

### #8 welshej

• D.I.C Head

Reputation: -2
• Posts: 82
• Joined: 18-November 11

## Re: Getting the Index of the Largest Number in an Array

Posted 20 November 2011 - 05:28 PM

GregBrannon, on 20 November 2011 - 05:23 PM, said:

Let's look at your algorithm:
```	public static int indexOfMaxInRange(int[] a) {
double[] b = new double [a.length];
int i = 0;
while (i < 4) {
b[i] = a[i];
i++;
}
Arrays.sort(B)/>;
int max = (int) b[b.length-1];

int z=0;
while (a[z] !=max) {
z++;
}

return z;

}
```

Here's what it does:
Creates a new array, b, of doubles equal in length to the argument array, a. (Why doubles when you know the argument array is ints?)

Assigns the first 3 values of the argument array, a, to the array b. Why 3 values? What will be in the remaining values of b?

Sorts array b.

Assigns the value the last index of b to an int value, max.

Assigns zero to a new int, z.

Compares the value of array a at index z to the variable max, the value of the last index of array b. If the value of the a array at that index doesn't equal max, the while loop exits. Otherwise, the value of z is incremented.

Return the value of z.

It's clear to me that the value of z is not related to the maximum value in either array a or b. Is it now clear to you?

I am looking for the index of the largest element. After sorting the array, I grab the final (and largest) element in the array. With a loop, I then continue to traverse my array until the index of the array is equivalent to that of the pre-determined largest value in the array. Once it does, the loop stops and returns the value of the incrementer value.
Was This Post Helpful? 0

### #9 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Getting the Index of the Largest Number in an Array

Posted 20 November 2011 - 05:31 PM

Sounds like you figured it out in spite of me. Yes, I was focused on the largest element in the original array, but that's half the answer.

Good job!
Was This Post Helpful? 1

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }