8 Replies - 4490 Views - Last Post: 20 November 2011 - 05:31 PM Rate Topic: -----

#1 welshej  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • 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  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • 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  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • 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  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

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

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

Posted 20 November 2011 - 05:28 PM

View PostGregBrannon, 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • 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