# Finding Second largest number

Printable View

• 12-29-2011, 03:58 AM
Gsangha
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?

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, 07:24 AM
tnrh1
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, 12:37 PM
nithinv
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, 01:01 PM
JosAH
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,

Jos
• 12-29-2011, 02:04 PM
Norm
Re: Finding Second largest number
@hidanjutt Please start your own thread for your questions.
• 12-29-2011, 04:49 PM
ozzyman
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.

Code:

```int idxMaxNum; for (int count=array.length-1; count>0; count--) {     idxMaxNum = (array[count] > array[count-1])?         count : count-1; } return idxMaxNum;```