posted 4 years ago

Edited to update work so far. Now I'm just getting two errors concerning line 38 where it has Arrays.sort(int roomList); and the errors state that ".class is expected" and so is a semicolon. What did I do wrong? Also, how might I tweak the code to display "Occupied" or "Unoccupied" depending on the room that was entered?

Also we're not allowed to make use of API method for binary search so that's out of the question.

Also we're not allowed to make use of API method for binary search so that's out of the question.

Java Newb!

Shey Gordon

Greenhorn

Posts: 7

posted 4 years ago

I think the error you are having is on line 46. return mid; will not work. If this is the value, you should print occupied, then simply put return; to end the loop. Also when sorting your array try to account for the amount of 0's you will have. Your array set is 25. If the user puts less than 25 numbers then there will be zeros indexed and when put in order will be something like 0, 0, 0, 0, 0, 0, 0, ,0 ,0 ,0, 0, 0, 0, 0, 0, 1, 5, 9, 12, 36, 42, 50, etc... good luck.

Shey Gordon

Greenhorn

Posts: 7

Russell Clapp

Greenhorn

Posts: 8

posted 4 years ago

It's saying that because midValue is an int, and roomList is a String.

I am not an expert so I can't tell you how to fix this. But i can say you can write this program using all ints.

I am not an expert so I can't tell you how to fix this. But i can say you can write this program using all ints.

Russell Clapp

Greenhorn

Posts: 8

posted 4 years ago

Also how you had the code at the bottom was right. it is not one or the other, occupied or unoccupied. if the numbers are not the same then the program has a little more figuring out to do. The way a binary search works is to divide in half, then either adjust the top or bottom half and try again until the number it is searching for is the mid or there are no more tries. so if looking for 7 out of 1 2 3 4 5 6 7 8 9 10, it will check the 5 first. This is because 0 (low) plus 10 (high) divided by 2 is 5 realizing that 7 is higher than 5, it will add 1 to 0, (1) and then add this to 10 (11). 11/2 is 5.5. 7 is still higher so low + 1 is 2. 2+10 = 12, 12/2 = 6. See? So just because the room is not occupied on the first try does not mean it is automatically unoccupied.

Shey Gordon

Greenhorn

Posts: 7

posted 4 years ago

I fixed it again. It compiles properly and it works up until the binary search part. Let's say I enter room 4 and then search for room 4... instead of showing "Occupied" it prints "Unoccupied" infinitely. In order to adjust it to where it searches correctly, exactly how would I write the code?

Java Newb!

posted 4 years ago

Please look into your binary search algorithm. What you have written now is

Now this will be AN INFINITE LOOP. You have asked the while loop to run if low<=high whereas you haven't modified any of their values inside the loop.So the condition will always remain true and the loop will keep on running.

Now this will be AN INFINITE LOOP. You have asked the while loop to run if low<=high whereas you haven't modified any of their values inside the loop.So the condition will always remain true and the loop will keep on running.

Let there be Code!!

posted 4 years ago

In Binary Search, you have a sorted list.{suppose Ascending order}

1.Check the middle element and compare with your value.

2. If your value==mid then it is found.

3. If your value > middle element, you have to search the upper part of the list i.e now your lower bound will be mid and the upper bound will be array size.

4. Else your value is in the lower part of the list i.e your upper bound will change to mid.

5. Continue this until the element is found.

Now implement this in your case and you are done!!

1.Check the middle element and compare with your value.

2. If your value==mid then it is found.

3. If your value > middle element, you have to search the upper part of the list i.e now your lower bound will be mid and the upper bound will be array size.

4. Else your value is in the lower part of the list i.e your upper bound will change to mid.

5. Continue this until the element is found.

Now implement this in your case and you are done!!

Let there be Code!!