Results 1 to 6 of 6
  1. #1
    Gsangha is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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.

  3. #3
    nithinv is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default 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);
    }
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,024
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default Re: Finding Second largest number

    @hidanjutt Please start your own thread for your questions.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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

  1. Replies: 26
    Last Post: 11-14-2011, 02:22 PM
  2. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  3. Finding the largest value.
    By Blacky777 in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 08:52 PM
  4. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM
  5. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •