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.
#include <iostream>
#include <conio.h>
#include "ctime"
#include "windows.h"
usingnamespace 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. ";
}
elseif(a < n)
{
cout << "Too low. ";
}
elseif (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 ";
}
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: elseif (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
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.