so i am still relatively new to c++ and trying to follow the book, i get the concept of binary search but i can't seem to put it in affect. can anybody help me try solve this. Any help and all help greatly appreciated.
Basically i need to make a number guessing game where user thinks of a numbver from 1 - 100 and the machine will try to guess it in the least number of times. Once it guesses the number it will also say how many tries it took to guess.
#include<iostream>
usingnamespace std;
constint MAX = 100;
int main()
{
char ch;
cout << "Think of an integer number between 0 and " << MAX<<endl;
cout << "Write it down on a piece of paper then hit a key to continue"<<endl<<endl;
cin.get(ch);
cout << endl<<"I am going to guess the number you picked... You just need to give me some hints"<<endl<<endl;
cout <<"Hit any key when you're ready..."<<endl<<endl;
cin.get(ch);
int first = 0; // First element of list. The list is initially [0...MAX]
int last = MAX; // last element of the list
int middle; // variable containing the current middle value of the list
int choice;
while (first <= last)
{
middle = (first + last) / 2;
cout << "Is your number: " << middle << endl;
cout << "If it is the number press y , otherwise press n " << endl;
cin >> choice;
if ('y' == choice)
{
return middle;
}
elseif ('n' == choice)
{
cout << "Is it higher or lower? " << endl;
cout << "Press h for higher and l for lower " << endl;
cin >> choice;
if ('h' == choice)
{
first = middle + 1;
}
elseif ('l' == choice)
{
last = middle - 1;
}
}
}
cin.get();
cin.get();
return 0;
}
Separate/Abstract your number from your search. Binary search only works on a sorted list. It assumes depending on the algorithm that data is sorted from least to greatest from left to right.
#include <iostream>
usingnamespace std;
#ifndef MAX
#define MAX 10
#endif
int main(int argc, char** argv[])
{
char ch;
cout << "Think of an integer number between 0 and " << MAX<<endl;
cout << "Write it down on a piece of paper then hit a key to continue"<<endl<<endl;
cin.get(ch);
cout << endl<<"I am going to guess the number you picked... You just need to give me some hints"<<endl<<endl;
cout <<"Hit any key when you're ready..."<<endl<<endl;
cin.get(ch);
int first = 0; // First element of list. The list is initially [0...MAX]
int last = MAX; // last element of the list
int middle; // variable containing the current middle value of the list
//INDEX INTO SET, When found
char choice;//we are using character input here
int set[MAX] = {0,1,2,3,4,5,6,7,8,9};
int search =-1;//using -1 as not found value
while('y' != choice)
{
cout << "Is your number: " << set[middle] << endl;
cout << "If it is the number press y , otherwise press n " << endl;
cin >> choice;
//search until found or the entire x*log2(N) occurrences are iterated through
while (first <= last && choice == 'n')
{
middle = (first + last) / 2;
if (set[middle] < search)
{
first = middle + 1;
}
elseif (set[middle] > search )
{
last = middle - 1;
}
else
{ `
break; //found
}
}
search = set[middle];
}//if done
cin.get();
cin.get();
return search;
}
You shouldnt have to query the user if the number is higher or lower because binary search already assumes the set is in ORDER. If your set is not ordered then you must order it.
EDIT:
for a number guessing game you don't need any search algorithm. All you need to do is generate or store a random number and give the user a certain number of attempts to guess it right before they lose. All you need is an infinite loop and if statements and bool checking. No need for binary or linear search here.