Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trying to find 3 largest numbers in an array using a single For loop

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to find 3 largest numbers in an array using a single For loop

    Hello Java Gurus:

    I am trying to find 3 largest numbers in an array using a single For loop but my following code is still showing randomly sorted numbers. Any help/suggestion will be much appreciated.

    public class largest3 {

    static int m, n, o;
    static int array[] = new int[100];

    public static void main(String[] args) {

    for (int i = 0; i < 10; i++) {
    array[i] = (int) (Math.random() * 100);
    }
    m = array[0];
    n = array[1];
    o = array[2];
    for (int i = 0; i < 10; i++) {
    if (array[i] > m){
    o = n;
    n = m;
    m = array[i];
    }
    else if(array[i] > n) {
    o = n;
    n = array[i];
    }
    else if(array[i] > o) {
    o = array[i];
    }

    }
    for (int i = 0; i < 10; i++) {
    System.out.println(array[i]);
    }
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to find 3 largest numbers in an array using a single For loop

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Fix your post, and give your variables better names. What does 'm', 'n', and 'o' mean?

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Trying to find 3 largest numbers in an array using a single For loop

    Hi, Your logic is not proper. Please modify your code and post it properly as mentioned by GregBrannon above.
    Thanks and regards,
    Sambit Swain

Similar Threads

  1. how to find 2nd largest array if array values like{10,20,92,81,92,34}
    By anjijava16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2013, 04:59 PM
  2. Logic to find adjacency of numbers 0 & 1 in an array
    By kasun.tawlatta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2013, 09:38 PM
  3. find largest value in BST
    By Java Bean in forum What's Wrong With My Code?
    Replies: 41
    Last Post: December 10th, 2012, 03:42 PM
  4. 3x3 array find largest number
    By joecan2010 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 27th, 2012, 11:56 PM
  5. Find the two largest number
    By vendettabf in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 29th, 2011, 01:23 PM