title: returning boolean from a recursive binary search in C
body: <p>I am trying to implement a recursive binary search in C. I am using the CS50 library to define bool as a type. My code will find the inputted value in a test array. However when i check the returned value r with an if statement it is usually returning false, even though the number is found.My code is as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;cs50.h&gt;
  
  bool binarysearch(int value, int values [], int n, int lo, int hi);
  int main(void)
  {
      // test array of 6 values sorted.
      int values[] = {1 , 2, 3, 4 , 5, 6};
      int n = 6;
      int hi = values[n-1];
      int lo = values[0];
      // input from user
      printf("What number\n");
      int value = GetInt();
      //search for value in test arary
      bool r = binarysearch(value,values,n,lo,hi);
      if (!r)
      {
          printf("not right\n");
          return 1;
      }
      return 0;
  }
  
  bool binarysearch(int value, int values [], int n, int lo, int hi)
  {
      int mid;
      mid = (lo + hi)/2;
      // condition to avoid indexing error
      if (((mid == 0) || (mid == n-1)) &amp;&amp; (values[mid] != value) )
      {
          return false;
      }
      //check if value is at mid index in test array
      if (values[mid] == value)
      {
          printf("Key Found\n");
          return true;
      }
      // check right half of array
      else if(value &gt; values[mid])
      {
          binarysearch(value, values,n, mid+1, hi);
      }
      //  check left half of array
      else if(value &lt;values[mid])
      {
          binarysearch(value, values,n,lo, mid-1);
      }
      return false;
  }
  </code></pre>