Binary problem

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?
Write something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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. ;)
Thank you so much! I will try that and see how I do!
ok so here is my code, I am now stuck and I'm getting an error saying "too few arguments to function isYourNumber"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace 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);
       }
       else if (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;
}
I think you are a bit confused on how functions are supposed to work (specifically, arguments).

http://www.cplusplus.com/doc/tutorial/functions/
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.
Ok so I have this now, now how would I call it in main?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace 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;
       }
    else if (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;
}
Topic archived. No new replies allowed.