Results 1 to 10 of 10
- 10-27-2010, 07:43 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
Finding maximum number at index position
Hi, I've created a program and it seems to find the maximum value when a number of command line arguments are entered but i cannot get it to also print the index of which value it is:
for (int i=0; i < args.length; i++)
{
double aValue = Double.parseDouble(args[i]);
if (aValue > max)
{
max = aValue;
largestIndex = i;
}
largestIndex = i;
}
//System.out.println(max + largestIndex);
- 10-27-2010, 07:45 PM #2
That's because you're adding them numerically.
System.out.println(2 + 5); // prints 7
System.out.println(2 + "" + 5); // prints 25
System.out.println(2 + " and " + 5); // prints 2 and 5
- 10-27-2010, 07:49 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
No i've just put it like that jsut to test, but the actual print output is:
System.out.println("The maximum value is " + max + " which is on index " + largestIndex);
I'm presuming it still wouldnt make a difference.
I keep getting the error that largestIndex might not be initialized.
- 10-27-2010, 07:49 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
... and that second 'largestIndex = i;' should go.
kind regards,
Jos
- 10-27-2010, 07:52 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
what about that. Where would it go? I do need that dont i, because then the value wont be assigned to largestIndex because i is in the loop.
- 10-27-2010, 07:57 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
initialise largestIndex to 0 before the loop
- 10-27-2010, 08:00 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
It works, but it outputs the wrong index value..
- 10-27-2010, 08:02 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
It should go to the bit bucket because you don't need it nor do you want it. If you want to find the largest number in an array keep track of the index of the largest number so far. You can set the index at, say, -1 if you haven't found any number yet. Otherwise compare a current number with array[index] where index is the index of the largest number in the array so far and update it if necessary. In code it translates to this:
kind regards,Java Code:int maxIndex(double[] array) { int index= -1; if (array != null) for (int i= 0; i < array.length; i++) if (index == -1 || array[i] > array[index]) index= i; return index; }
Jos
- 10-27-2010, 08:10 PM #9
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
I have yet learnt about array. I've only learnt about args so far.
- 10-27-2010, 08:14 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Finding nth prime number
By dextr in forum New To JavaReplies: 2Last Post: 04-12-2010, 11:42 PM -
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02-15-2010, 12:37 AM -
Finding the Xpath of xml document with index
By indus in forum XMLReplies: 1Last Post: 02-05-2010, 10:41 AM -
Find index position of every word in a String
By pentace in forum New To JavaReplies: 6Last Post: 06-28-2009, 08:26 PM -
finding length on a number
By thekrazykid in forum New To JavaReplies: 8Last Post: 12-12-2008, 08:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks