Results 1 to 6 of 6
  1. #1
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default Find the second largest number in the array

    I have written the following code for finding the 1st largest number in the given array and please do correct me in case there is some error in the program and can any one help me in finding the 2nd largest number in the array.
    The code which i have written is as follows:

    public class Assignment1 {

    public static void main(String args[]) {
    int i;
    int temp = 95;

    int arr[] = {95, 20, 34, 100, 9,150};
    for (i = 0; i < arr.length - 1; i++) {

    if (i == 0) {
    temp = arr[i];
    } else if (temp < arr[i]) {

    temp = arr[i];

    }
    }
    System.out.println("Largest is :" + temp);

    }
    }

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi - have you tested your code? If so, and if it works correctly, you can do a second pass through the array to find the second largest number, using some if statements like you have done.

    Also, are you not allowed to sort the array?

    Best,
    --user0--

  3. #3
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi - also, looks like you are missing the last element in your loop, due to the condition arr.length - 1. I would suggest changing the for loop to :
    Java Code:
    for(int i = 0; i < arr.length; i++)
    Secondly, why are you initializing temp to 95? I would initialize it to the lowest possible int value:
    Java Code:
    int temp = Integer.MIN_VALUE;
    and get rid of the first if statement you have as it would just be a waste checking if i==0 each time the loop runs.

    best,
    --user0--

  4. #4
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    My code works .I am not able to write the code to find the second highest number.Any help or suggestions

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,355
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by radhi16 View Post
    I have written the following code for finding the 1st largest number in the given array and please do correct me in case there is some error in the program and can any one help me in finding the 2nd largest number in the array.
    One loop and a couple of if-statements can do the job: think of it, suppose you have two numbers: first and second representing the largest and second largest numbers so far; also suppose a current number curr. If curr is larger or equal to first, what to do? Else, if curr is larger or equal to second, what to do?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  2. Replies: 12
    Last Post: 10-31-2010, 06:14 PM
  3. Find max number in 2D array?
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 04:27 PM
  4. find the greatest and lowest number in 2D array
    By le_albina@hotmail.com in forum New To Java
    Replies: 2
    Last Post: 03-30-2009, 11:09 PM
  5. Replies: 8
    Last Post: 04-22-2008, 03:21 AM

Posting Permissions

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