Results 1 to 12 of 12
Thread: Finding largest no
 11292007, 10:52 AM #1Senior Member
 Join Date
 Nov 2007
 Posts
 111
 Rep Power
 0
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); } } }
Cheers
 11292007, 11:53 AM #2Member
 Join Date
 Aug 2007
 Posts
 26
 Rep Power
 0
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); } }
 11292007, 11:56 AM #3Senior Member
 Join Date
 Nov 2007
 Posts
 111
 Rep Power
 0
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.
 11292007, 12:00 PM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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.
 11292007, 12:08 PM #5Senior Member
 Join Date
 Nov 2007
 Posts
 111
 Rep Power
 0
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.
 11292007, 12:12 PM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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.
 11292007, 12:20 PM #7Senior Member
 Join Date
 Nov 2007
 Posts
 111
 Rep Power
 0
 11292007, 12:24 PM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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
 11292007, 12:29 PM #9Member
 Join Date
 Aug 2007
 Posts
 26
 Rep Power
 0
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); } }
Thanks,
R
 11292007, 12:35 PM #10
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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.
 11292007, 12:46 PM #11Senior Member
 Join Date
 Nov 2007
 Posts
 111
 Rep Power
 0
Its clear now. Thanks all of you.
 11292007, 12:49 PM #12
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Similar Threads

Finding Largest Prime Factor
By perito in forum New To JavaReplies: 7Last Post: 11082010, 08:25 PM 
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01122008, 10:30 PM 
Largest string value (alphabetically)
By mew in forum New To JavaReplies: 3Last Post: 12142007, 05:45 PM 
ArrayList problem (finding largest no)
By bugger in forum New To JavaReplies: 3Last Post: 12122007, 12:47 PM 
Finding GCF in java
By lenny in forum Advanced JavaReplies: 1Last Post: 07312007, 05:41 AM
Bookmarks