Bracketing Search

I need a little help with bracketing search problem.
i solved the first part which involves guessing the computer's number.
Second part works too, but it takes too many tries for the computer to guess your number, I think I messed up around the 2 "fors" for generating random numbers, but I can't figure out how to make the computer guess faster.


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <conio.h>
#include "ctime"
#include "windows.h"
using namespace std;



void HumanVsComputer();
void ComputerVsHuman();

int main()
{
	
	int tries=0;

	cout<<"----------Welcome to the guessing number game!--------------"<<endl<<endl;
	cout<<"\t 1 - Human vs Computer"<<endl;
	cout<<"\t 2 - Computer vs Human"<<endl;
	cout<<"\t 3 - Quit Program" <<endl;

	int choice;
	




	while(cin){
		cin>> choice;  // Make Menu Secection
	{
		switch(choice)
		{
	case 1:
		HumanVsComputer();
		break;

	case 2:
		ComputerVsHuman();
		break;
		
	case 3:
		return 0;
     
	default:
         break;
         return 0;
		}
	}
	}
	



	_getch();
}
//The function in which the human is guessing the number
void HumanVsComputer()
{
	int n, a, i, tries=0;
	srand(time(0));

	for(i=1; i<100; i++)
	{

		n = rand()%100+1;
	}


	do
	{
		cout <<"\nInsert a number between 1-100 : "; 
		cin>>a;
		tries++;

		if(a > n)
		{
			cout << "Too high. ";


		}
		else if(a < n)
		{
			cout << "Too low. ";

		}

		else if (a = n)
		{
			cout <<"Corect.";
			break;

		}


	}
	while(a !=n);

	cout << endl;
	cout <<"\nThe number was " << n  <<" You guessed it in "<<tries <<" tries."<<endl;


}

//The function in which the Computer is guessing the number
void ComputerVsHuman()
{
	int n, a, i, tries=0;

	cout <<"\nInsert a number between 1-100 : "; 
	cin>>a;

	do{

	srand(time(0));
	for(i=1; i<100; i++)
	{
		n = rand()%100+1;
	}
	
	Sleep(600);
	if(a<n)
	{
		cout <<"\nToo high.";
		srand(time(0));

		for(i=1; i<100; i++)
		{

			n = rand()%i+1;
		}
		cout << "(Too High)Computer will try again: \n"<<n;
		tries++;
	}

	if(a>n)
	{
		cout <<"\nToo low.";	
		srand(time(0));
		for(i=n; i<100; i++)
		{
			n = rand()%i;
		}
		cout << "(Too Low)Computer will try again: "<<n;
		tries++;
	}
	

	if(a==n)
	{
		cout <<"\nThe Computer guessed the number";
		break;
	}
	
	}
	while(n != a);


    cout <<"\nYour number was " << a <<".The computer guessed it in " << tries << " tries ";
}
Victor89 wrote:
i solved the first part which involves guessing the computer's number.

You didn't solve the first part, you instead assigned the value being guessed to the value the computer chose so the program terminated after that:
else if (a = n) // should be else if (a == n)

Also why are you doing this?
1
2
3
4
for(i=1; i<100; i++)
{
	n = rand()%100+1;
}


Finally the trick to solving this is using the good ol' divide and conquer method which when implemented correctly should be able to guess the number in less than log2MaxValue tries. Ex. if the maximum number is 1000, this method allows one to guess the correct value in at most 10 tries. The trick is to start with the middle of the range and with each guess you narrow down the range of numbers until the correct one is the only one left

This page has an example implementation of your program but does it by making use of the divide and conquer method:
http://everythingcomputerscience.com/algorithms/CSDivide_and_Conquer_Algorithm.html
Last edited on
I know it should be (a==n), i corrected the code before, looks like I copied and pasted the old one.

Anyway, I should try and not generate random numbers for the computer to guess, I agree on that, hope I can implement the divide and conquer algorithm.
Last edited on
Thank you it works very well with this algorithm.
Last edited on
Topic archived. No new replies allowed.