Results 1 to 6 of 6
Thread: Finding Second largest number
 12292011, 03:58 AM #1Member
 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); } }
 12292011, 07:24 AM #2Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 6
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.
 12292011, 12:37 PM #3Member
 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);
}
}
 12292011, 01:01 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,372
 Blog Entries
 7
 Rep Power
 25
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 12292011, 02: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.length1; count>0; count) { idxMaxNum = (array[count] > array[count1])? count : count1; } return idxMaxNum;
Last edited by ozzyman; 12292011 at 04:54 PM.
Similar Threads

smallest number and largest number using while and if statements
By vicu1 in forum New To JavaReplies: 26Last Post: 11142011, 03:22 PM 
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11032010, 07:49 AM 
Finding the largest value.
By Blacky777 in forum New To JavaReplies: 7Last Post: 02022010, 09:52 PM 
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01122008, 11:30 PM 
Finding largest no
By bugger in forum New To JavaReplies: 11Last Post: 11292007, 01:49 PM
Bookmarks