title: Logic of binary search
body: <p>I read a Pseudo Code of binary search in a data structure book and then I started to writing code. the code that i written is :</p>
  
  <pre><code>#include &lt;iostream.h&gt;
  #include &lt;conio.h&gt;
  template &lt;class T&gt;
  
  int BSearch(T x[], const int n, T item)
      {
      int loc, first = 0, found = 0, last = n-1;
          while(first &lt;= last &amp;&amp; !found)
          {
              loc = (first + last)/2;
              if(item &lt; x[loc])
                  last = loc - 1;
              else if(item &gt; x[loc])
                  first = loc + 1;
              else
                  found = 1;
           }
        return found;
     }
  
  int main()
      {
      const int n =5;
        int x[n],item;
        cout &lt;&lt; "Pls enter " &lt;&lt;n&lt;&lt;" number(s): ";
  
        for(int i = 0; i &lt; n;i++)
          cin &gt;&gt; x[i];
        cout &lt;&lt; "Pls enter the item to Search: ";
          cin &gt;&gt; item;
        if(BSearch(x,n,item))
          cout &lt;&lt; "\n\t Item Exist";
        else
          cout &lt;&lt; "\n\t Item NOT Exist";
  
        getch();
        return 0;
     }
  </code></pre>
  
  <p>there is no any Error but a logic fault is there . it just return 0 value from BSearch function and i just get this message "Item NOT Exist". where is my bug ?! i didn't find it.
  Thanks</p>