title: How to have a counter for a binary search in C++?
body: <p>I am not sure how to have a counter for a binary search to determine how many comparisons was made before finding the target number that is inputted. Do I add a counter near the end of the binary search?</p>
  
  <pre><code>void search (int arr[])
  {
  int target;
  
  int first = 0,
      last = MAX - 1,
      mid,
      position = -1;
  int row = 2;
      bool found = false;
  
  cout &lt;&lt; "Enter in target number to search for: ";
  cin &gt;&gt; target;
  
  while (!found &amp;&amp; first &lt;= last)
  {
      mid = (first + last) / 2;
      if (avg[row][mid] == target)
      {
          cout &lt;&lt; "found at index " &lt;&lt; mid &lt;&lt; endl;
          found = true;
      }
      else if (avg[row][mid] &gt; target)
          last = mid - 1;
      else
          first = mid + 1;
  }
  if (!found)
      cout &lt;&lt; "Not found\n";
  }
  </code></pre>