Win a copy of Head First Android this week in the Android forum!
  • 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 16668
278
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: 16668
278
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: 16668
278
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: 16668
278
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: 74374
334
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic