Results 1 to 6 of 6
Thread: Finding Second largest number
- 12-29-2011, 02:58 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
Finding Second largest number
The purpose of this code is to do 3 things.
1st - Find the max time (given in the array times)
2nd - Find the 2nd max time (given in the array times)
3rd - Find the person that correlates with the max and 2nd max time (the names are given in array names)
I managed to figure out how to find the max time.
Im having trouble finding the 2nd time (attempt shown)
Can anyone point me towards the right direction to do the 3rd part?
Java Code:class Marathon { public static void main(String[] arguments){ String names[] ={ "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"}; int times[] ={ 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265}; int max=0; int max2 = 0; int counter, counter2; //finding max time for (counter = 0; counter < times.length; counter++) { while(times[counter]>max){ max=times[counter]; } } System.out.println("maximum number is = " + max); //find 2nd max time for(int x: times){ while(x > max2 && max2 != max){ max2 = x; } } System.out.println("2nd maximum number is = " + max2); } }
- 12-29-2011, 06:24 AM #2
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Finding Second largest number
You don't need a nested loop to find the max number in an array.
Just run on the array and keep asking if max is smaller then the value of the array in the current index then max gets the array value.
Try to think what you do with the second one.
- 12-29-2011, 11:37 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: Finding Second largest number
Solution :
class Marathon {
public static void main(String[] arguments){
String names[] ={
"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
"Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",
"Aaron", "Kate"};
int times[] ={
341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,
343, 317, 265};
int max=0;
int max2 = 0;
int counter, counter2;
//finding max time
for (counter = 0; counter < times.length; counter++) {
if(times[counter]>max){
max2=max;
max=times[counter];
}
else if(times[counter]>max2)
{
max2=times[counter];
}
}
System.out.println("maximum number is = " + max);
System.out.println("2nd maximum number is = " + max2);
}
}
- 12-29-2011, 12:01 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Finding Second largest number
The initialization of max and max2 is incorrect; e.g. what happens if all the numbers are negative?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-29-2011, 01:04 PM #5
Re: Finding Second largest number
@hidanjutt Please start your own thread for your questions.
-
Re: Finding Second largest number
What if you created a separate method to run through an array and find the index position of the max number? Then once you've got that index, you could create a copy of the array with the highest number removed and find the highest number in the second array.
Java Code:int idxMaxNum; for (int count=array.length-1; count>0; count--) { idxMaxNum = (array[count] > array[count-1])? count : count-1; } return idxMaxNum;Last edited by ozzyman; 12-29-2011 at 03:54 PM.
Similar Threads
-
smallest number and largest number using while and if statements
By vicu1 in forum New To JavaReplies: 26Last Post: 11-14-2011, 02:22 PM -
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
Finding the largest value.
By Blacky777 in forum New To JavaReplies: 7Last Post: 02-02-2010, 08:52 PM -
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01-12-2008, 10:30 PM -
Finding largest no
By bugger in forum New To JavaReplies: 11Last Post: 11-29-2007, 12:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks