• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

binary search error

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I make it so I am able to enter any number rather then just the numbers in the arrays? I want it to be able to find the position of any number between 0-100
BTW I am Joe and this is my first post.


[Moderator edit: added code tags]
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,

Welcome to the Ranch!

If you're going to post code, please UseCodeTags (←click on the link to learn how to do that). Also, you should try to format your code properly to make it easier for other people to read it. I find the Google Java Style Guide a good reference. There's also the JavaRanch Style Guide.
 
joey shertz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, will use the code tag from now on.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really understand what you want. Can you give a concrete example to clarify?
 
joey shertz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only positions the code finds are the arrays listed, ie 10,20,30,40,50, etc. I want to find a way to be able to find the position of any number. ( like 44, 56 etc )
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

joey shertz wrote:The only positions the code finds are the arrays listed, ie 10,20,30,40,50, etc. I want to find a way to be able to find the position of any number. ( like 44, 56 etc )



How do you expect to find something that's not in your array?

Lines 5-15 of your code listing determine which values you will be able to find. How can you expect to find numbers like 44 and 56 if you don't put them there?
 
joey shertz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I worded it wrong. Is there a way to add all my 1-100 numbers w/o adding 100 arrays?
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

joey shertz wrote:Ok I worded it wrong. Is there a way to add all my 1-100 numbers w/o adding 100 arrays?


"You keep using that word. I don't think it means what you think it means" - Inigo Montoya in "The Princess Bride"

You only have one array. You mean to say elements of the array.

Since you have already learned about the for-loop, can you think of a way you can use one to assign increasing values to each successive element of your array? That is, you first declare an array of 100 integers (instead of the 11 that you're declaring now). Then you use a loop to set the value of each of those elements to a number that's bigger than the previous one, if there is any. The only case where there is no previous element is, of course, for the very first element (index = 0).

You need to assign increasing values to your array elements because that's the only way a binary search will work.
 
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Otherwise you would have to [copy your array with the clone method and] sort it before you can try binary search.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic