C in Xcode Given the numbers 1 to 1000 what is the minimum n

C++ in Xcode

Given the numbers 1 to 1000, what is the minimum number of guesses needed to find a specific number if you are given the hint ‘higher’ or ‘lower’ for each guess you make?

Read the question carefully and you’ll note that the question asks for the ‘minimum’ number of guesses. Think about it – someone can guess the right question on their first try right?

So, the answer here would be ‘1’, since it would take only one correct guess to find a specific number.

Finding the maximum number of guesses

But, what if we wanted to find the maximum number of guesses?

Solution

#include<iostream>
using namespace std;
int count=1;//set count to 1 if number found in first attempt

int MaximumGuess(int low,int high,int number)
{
   if(low<high)
   {
       int mid=(low+high)/2;

       if(mid<number)
       {
           count++;
           return MaximumGuess(mid+1,high,number);//if number is greater than mid
          
       }
       else if(mid>number)
       {
           count++;
           return MaximumGuess(low,mid-1,number);//if number is smaller than mid
          
       }
       else
       {count++;
           return mid;//found number
       }

   }


}

int main(int argc, char const *argv[])
{
   MaximumGuess(1,1000,25);
   cout<<\"Max Guess Count is \"<<count<<endl;
  
   return 0;
}

=========================================================================

Ouput:

akshay@akshay-Inspiron-3537:~/Chegg$ g++ minimguess.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Max Guess Count is 10

C++ in Xcode Given the numbers 1 to 1000, what is the minimum number of guesses needed to find a specific number if you are given the hint ‘higher’ or ‘lower’ f
C++ in Xcode Given the numbers 1 to 1000, what is the minimum number of guesses needed to find a specific number if you are given the hint ‘higher’ or ‘lower’ f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site