|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

11-29-2007, 11:52 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 111
|
|
|
Finding largest no
I have following code for finding largest no among 3 nos.
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
|
|

11-29-2007, 12:53 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 22
|
|
You can have a separate int variable for position.
something like this...
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);
}
}
|
|

11-29-2007, 12:56 PM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 111
|
|
|
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.
|
|

11-29-2007, 01:00 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

11-29-2007, 01:08 PM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 111
|
|
|
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.
|
|

11-29-2007, 01:12 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

11-29-2007, 01:20 PM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 111
|
|
Originally Posted by Eranga
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.
|
|

11-29-2007, 01:24 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

11-29-2007, 01:29 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 22
|
|
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:
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
|
|

11-29-2007, 01:35 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

11-29-2007, 01:46 PM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 111
|
|
|
Its clear now. Thanks all of you.
|
|

11-29-2007, 01:49 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
You are welcome pal....
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|