Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-29-2007, 11:52 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Finding largest no
I have following code for finding largest no among 3 nos.
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-29-2007, 12:53 PM
Member
 
Join Date: Aug 2007
Posts: 22
revathi17 is on a distinguished road
You can have a separate int variable for position.
something like this...
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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-29-2007, 12:56 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-29-2007, 01:00 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga

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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-29-2007, 01:08 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-29-2007, 01:12 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-29-2007, 01:20 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-29-2007, 01:24 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-29-2007, 01:29 PM
Member
 
Join Date: Aug 2007
Posts: 22
revathi17 is on a distinguished road
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:

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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-29-2007, 01:35 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-29-2007, 01:46 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Its clear now. Thanks all of you.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 11-29-2007, 01:49 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Largest Prime Factor perito New To Java 2 03-26-2008 07:04 AM
Finding largest and smallest integer mlhazan New To Java 2 01-12-2008 11:30 PM
Largest string value (alphabetically) mew New To Java 3 12-14-2007 06:45 PM
ArrayList problem (finding largest no) bugger New To Java 3 12-12-2007 01:47 PM
Finding GCF in java lenny Advanced Java 1 07-31-2007 06:41 AM


All times are GMT +3. The time now is 09:44 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org