guys, I'm working on an problem in which the computer is guessing the number the user is thinking. but the guesses are not quite working. And I think is because the implementation of the guessHigh and guessLow methods. Any help appreciated. Here's the problem.
--------------------------------------------------------------------------------
Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.
If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.
The program must let the user continue to guess until the user correctly guesses the number.
Modify the program so that instead of the user guessing a number the computer came up with, the computer guesses the number that the user has secretly decided. The user must tell the computer whether it guessed too high or too low.
I guess the assigment of low and high should be made before calling the function b/c the low and high change in dependence of which function is called.... I could be wrong
Actually, guessLow will work as intended. guessLow is wrong though. What I don't really get is why you make a difference between high and low, you could do it with binary search using only one variable.
could you link me somewhere that is explained.... I'm doing searches by "binary search" but none of the tutorials I've seen are applicable to this scenario
Well, actually yes. You are looking for an unknown value, and you always know whether the value you search for is higher or lower than the current value, and the values are ordered (yay for Maths: You have the set of natural numbers lower than 101 excluding 0. That set is, by definition, ordered). Means you can use binary search. I know, most tutorials use this to find indices of arrays or something like that, but you can use it to guess a number just as easily.
Copied straight from wikipedia:
This rather simple game begins something like "I'm thinking of an integer between forty and sixty inclusive, and to your guesses I'll respond 'High', 'Low', or 'Yes!' as might be the case." Supposing that N is the number of possible values (here, twenty-one as "inclusive" was stated), then at most \lceil\log_2 N\rceil questions are required to determine the number, since each question halves the search space. Note that one less question (iteration) is required than for the general algorithm, since the number is already constrained to be within a particular range.
Even if the number to guess can be arbitrarily large, in which case there is no upper bound N, The number can be found in at most 2\lceil \log_2 k \rceil steps (where k is the (unknown) selected number) by first finding an upper bound by repeated doubling.[citation needed] For example, if the number were 11, the following sequence of guesses could be used to find it: 1, 2, 4, 8, 16, 12, 10, 11
One could also extend the method to include negative numbers; for example the following guesses could be used to find −13: 0, −1, −2, −4, −8, −16, −12, −14, −13.
You call guessHigh and guessLow with the values high and low (in that order). In each function high is set to the value of the first parameter (which is high) and low is set to the value of the second parameter (which is low). So in effect,
1 2 3 4 5
high = l; // is the same as
high = high;
low = h; // is the same as
low = low;