I need to write a program using a binary search to guess a number between 1 and 100. I need to do it recursively but am not quite sure how. The user doesn't need to input a number just yes or no. Here is an example of how it should be outputted.
Please choose a number between 1 and 100.
Is the number 50? (y/n) n
No? Is the number greater than 50? (y/n) y
Yes. Okay then.
Is the number 75? (y/n) n
No? Is the number greater than 75? (y/n) n
No. Okay then.
Is the number 62? (y/n) y
Any suggestions for writing this as a recursive function?
int askFor(int start, int end)
{
int number = (start+end)/2;
// now ask if number is which the user wants
...
// if yes
return number;
// if no
// ask if greater
...
// if yes
return askfor(number, end);
// if no
return askfor(start,number);
}
If you don't know how to write the whole function ask again but I first wanted to give you a hint. ;)
#include <iostream>
#include <math.h>
#include <stdlib.h>
usingnamespace std;
int isYourNumber (char response, int start, int end, char y, char n)
{
int number = (start + end) /2;
cout << "Is your number " << number << "?" << endl;
if (response== y)
{
return number;
}
else
cout << "Is your number greater than 50? " << endl;
cin >> response;
if (response == y)
{
return isYourNumber(number, end);
}
elseif (response == n)
{
return isYourNumber(start, number);
} }
int main()
{
cout << "Guess a number between 1 and 100 " << endl;
cout << "Answer my questions with a 'y' for yes and a 'n' for no" << endl;
return 0;
}
You don't think I've read that tutorial over and over? If I understood how they worked I wouldn't have needed help. This stuff does not come easily to me.
1.) You define your function as having 5 arguments, and then call it with only 2. You need to pass it all the arguments it wants (probably 3 in this case, because you don't need to pass y and n)
2.) You are passing char y and char n, for no reason, they aren't ever used except (incorrectly) in your if statements
Use 'y' and 'n' if you want to compare it to that character
e.g.: if(response == 'n')
3.) You don't ever call the function in main().
You should probably first experiment with making your own simple programs using the new things you have learned before you jump into your assignment/work.
#include <iostream>
#include <math.h>
#include <stdlib.h>
usingnamespace std;
int isYourNumber (int start, int end)
{
char response;
int number = (start + end) /2;
cout << "Is your number " << number << "?" << endl;
if (response== 'y')
{
return number;
}
else
cout << "Is your number greater than 50? " << endl;
cin >> response;
if (response == 'y')
{
return isYourNumber(number, end);
cout << "Your number is " << number << endl;
}
elseif (response == 'n')
{
return isYourNumber(start, number);
cout << "Is your number greater than 25? " << endl;
cin >> response;
}
}
int main()
{
int number;
char response;
cout << "Guess a number between 1 and 100 " << endl;
cout << "Answer my questions with a 'y' for yes and a 'n' for no" << endl;
cout << "Is your number" << number << endl;
return 0;
}