Results 1 to 12 of 12
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Finding largest no

    I have following code for finding largest no among 3 nos.
    Java Code:
    import java.util.*;
    public class LargestNo{
    public static void main (String [] arg) {
    
    Scanner scan = new Scanner (System.in);
    
    int [] numbers = new int [3]; 
    int x; 
    int largestNumber; 
    
    System.out.print("Put in (three) numbers");
    for (x=0; x<numbers.length; x++) {
    	numbers[x]=scan.nextInt ();
    }
    
    largestNumber = 0; 
    for (x=0; x<numbers.length; x++) { 
    if (x == 0) {
        largestNumber = numbers[0];
    }
    if (numbers[x] > largestNumber) {
        largestNumber = numbers[x];
    }
    }	
    System.out.println("The largest number is " + largestNumber);
    
    }
    }
    }
    I want to print the largest no and also its position in the array. how to do that? Please give me tips.

    Cheers

  2. #2
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    You can have a separate int variable for position.
    something like this...
    Java Code:
    import java.util.*;
    public class LargestNo{
    	public static void main (String [] arg) {
    	
    		Scanner scan = new Scanner (System.in);
    		
    		int [] numbers = new int [3]; 
    		int x; 
    		int largestNumber; 
    		int pos=0;//position in the array
    		
    		System.out.print("Put in (three) numbers");
    		for (x=0; x<numbers.length; x++) {
    			numbers[x]=scan.nextInt ();
    		}
    		
    		largestNumber = 0; 
    		
    		for (x=0; x<numbers.length; x++) { 
    		if (x == 0) {
    		    largestNumber = numbers[0];
    		    pos = 0;
    		}
    		if (numbers[x] > largestNumber) {
    		    largestNumber = numbers[x];
    		    pos = x;//x is the position of the number in the array.
    		}
    		}	
    		System.out.println("The largest number is " + largestNumber);
    		System.out.println("Position of the largest number:"+pos);
    	
    	}
    }

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Ok kool. Cant I fetch the index from array just specifying the value. For example, I am assuming that array will only contain unique values and having a value I now want to get its index from array.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default


    Just like same thing you have to done. Use another dummy variable. At each of the comparison you have done to check whether the number is large or small, update that dummy value.

    Say first number is large, your dummy should be 0. In the next iteration the second number is small, still your dummy should be 0, because it holds the position.

    I think my logic is clear.

    At the same time, I think you have use additional } at last. Compile and check it.

  5. #5
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks Eranga. But consider the following:
    For example, I am assuming that array will only contain unique values and having a value I now want to get its index from array.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm not get you. What you mean unique values. Between array index and array values there is no connection. Can you explain little more.

  7. #7
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Between array index and array values there is no connection.
    This clears the confusion. I was assuming that there is a connection and we can retrieve the values both way some how.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    No there is no connection. I think you know that, array is indexing by starting with 0. That mean maximum indexing is less than one by number of element.

    element 12 5 23
    index 0 1 2
    #of element 1 2 3

  9. #9
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    There is actually a way to get the index..Arrays has a method
    binarySearch(array to be searched,key) which returns the position of the specified key..
    Using it in your program:

    Java Code:
    import java.util.*;
    public class LargestNo{
    	public static void main (String [] arg) {
    	
    		Scanner scan = new Scanner (System.in);
    		
    		int [] numbers = new int [3]; 
    
    		int x; 
    		int largestNumber; 
    		//int pos=0;//position in the array
    		int index=0;
    		
    		System.out.print("Put in (three) numbers");
    		for (x=0; x<numbers.length; x++) {
    			numbers[x]=scan.nextInt ();
    		}
    		
    		largestNumber = 0; 
    		
    		for (x=0; x<numbers.length; x++) { 
    		if (x == 0) {
    		    largestNumber = numbers[0];
    		    //pos = 0;
    		}
    		if (numbers[x] > largestNumber) {
    		    largestNumber = numbers[x];
    		    //pos = x;//x is the position of the number in the array.
    		   index = Arrays.binarySearch(numbers, largestNumber);
    		}
    		}	
    		System.out.println("The largest number is " + largestNumber);
    		System.out.println("Position of the largest number:"+index);
    	
    	}
    }
    I am not sure though if this would be a correct way to get the index of an array element...


    Thanks,
    R

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep that's correct. One of the efficient way it is.

    The way what I've told is the basis way to do it, comparing how to find the largest number. Because the same way should follow.

  11. #11
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Its clear now. Thanks all of you.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 09:25 PM
  2. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 11:30 PM
  3. Largest string value (alphabetically)
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 06:45 PM
  4. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 01:47 PM
  5. Finding GCF in java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 06:41 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
  •