posted
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
posted
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.
Posts: 2489 | Registered: Jan 2002
| IP: Logged |
posted
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.
Posts: 332 | Registered: Apr 2005
| IP: Logged |
posted
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.
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).
Posts: 10177 | Registered: Apr 2001
| IP: Logged |
posted
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.
Posts: 10177 | Registered: Apr 2001
| IP: Logged |
posted
Sure would be. The quoted portion of the assignment specifies 5 questions though. You have enough.
Posts: 5656 | Registered: Oct 1999
| IP: Logged |