This is topic Computer Programming(Java Intro help) Logic Question. in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=051893

Posted by Lanfear (Member # 7776) on :
 
So I'm not asking for anyone to do my homework, because that wouldn't be the way I learn how to program, but I'm having trouble with the logic behind this. If anyone could give some insight I would love it..

"a Java program which prompts the user to select (but not disclose) a number between 0 and 31, and then poses exactly five questions to the user to determine the number the user has selected. Each of the five questions is posed by presenting four lines containing a program-generated group of sixteen numbers, asking if the user's selected number is in the group. The program then determines and announces the user's number"

So, I need to have a set of 16 numbers between 0 and 31. They click yes or no. Then it displays 16 more numbers. Yes or No. You get five questions and then I have to announce the number they picked.

So i don't need help with the actual coding of this thing. I need to figure out the logic behind the this number "game"

Anything you could do to steer me in the right direction would be great

http://img.skitch.com/20080211-f98n2ww27mkk66hreshbchitid.jpg
 
Posted by MEC (Member # 2968) on :
 
Think of it as in binary - 11111 = 31, make it so that each question eliminates half of the numbers and you will have the result.
 
Posted by Lanfear (Member # 7776) on :
 
Ha. one of the hints is "he key to this assignment is the secret of binary. The sets of numbers are generated by fixing the appropriate binary digit at 1."

So I managed to not think of it in binary , but still get it done in five questions.

But I want to be able to think of it in binary. Anyone who can explain further, it would be great.
 
Posted by Xavier (Member # 405) on :
 
It sort of looks like a game of "higher/lower" with some added junk in the question.

A higher/lower game that picks the middle number I think can guess the number in 4 questions.

Some examples:

quote:

For instance, if the number picked was 1:

Is the number higher/lower than 16? Lower
Is the number higher/lower than 8? Lower
Is the number higher/lower than 4? Lower
Is the number higher/lower than 2? Lower

Your number is 1.

quote:

For instance, if the number picked was 31:

Is the number higher/lower than 16? Higher
Is the number higher/lower than 24? Higher
Is the number higher/lower than 28? Higher
Is the number higher/lower than 30? Higher

Your number is 31.

quote:

For instance, if the number picked was 7:

Is the number higher/lower than 16? Lower
Is the number higher/lower than 8? Lower
Is the number higher/lower than 4? Higher
Is the number higher/lower than 6? Higher

Your number is 7.

Seems like you could do this program here pretty easily, and display some crap numbers in to pad the question to 16 numbers each display.

I may be misinterpreting the assignment, however.

Sort of looks like they are trying to teach you binary search algorithms in a strange way.

Edit: This little program above was actually the first program I've ever written. I was bored in math class one day so I wrote it on my graphing calculator.

[ February 11, 2008, 01:34 PM: Message edited by: Xavier ]
 
Posted by MrSquicky (Member # 1802) on :
 
MEC told you what you need to know.

There are four binary digits in all possible numbers between 0 and 31 (or 0000 to 1111). If you can determine what these digits are, you'll know what the number is.

Also important to remember, fixing one binary digit gives you exactly one half the numbers in the original sample set. Say for 0 to 7 (000 to 111), you've got 8 possibilities. If you hold the center (2^1) digit to 1, you now have 4 possibilities (2 - 010, 3 - 011, 6 - 110, and 7 -111).
 
Posted by Lanfear (Member # 7776) on :
 
This is my first computer science class and im struggling. But I think its starting to click.
because 0000 and 1111 is 0 to 31.

So that means I could ask the same set of questions no matter what they answer to them?

because the first question would show all the numbers that have a 0... and determine whether the the first column is 0 or 1

then it will go to .0..

then ..0.
then...0

and then once you know all four places you know the number.

I could be missing something entirely. I'm sorry if i'm being oblivious
 
Posted by MrSquicky (Member # 1802) on :
 
No, you got it completely. That's really great.

edit: Incidentally, I'd be surprised if the asking the same questions wasn't supposed to be part of the assignment. Otherwise, the insistence of having 16 numbers each time doesn't make that much sense. It would just be stupid padding, like Xavier said.
 
Posted by Lanfear (Member # 7776) on :
 
i feel like im bugging now.
but im trying to just make my own binary table with their corresponding regular numbers

isn't 1111 15?
so 31 would be 11111?
 
Posted by Xavier (Member # 405) on :
 
Sure would be. The quoted portion of the assignment specifies 5 questions though. You have enough.
 
Posted by Lanfear (Member # 7776) on :
 
Thanks guys. It's been very very helpful.
 
Posted by MrSquicky (Member # 1802) on :
 
Oh yeah, sorry. Don't know what I was thinking.
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2