title: How to return positive occurrence
body: <p>I have a function that when given a zero-indexed array A of N integers, sorted in non decreasing order, and some integer X, looks for X in A. If X is present in A, then it returns a positive index of an occurrence of X in A. Otherwise, the functions returns -1.</p>
  
  <p>It should work like this:</p>
  
  <ul>
  <li>If I have A[0]=1, A[1]=1 and X=1 it should return 0 because A[0]=1.</li>
  </ul>
  
  <p>But it doesn't return what I want. Can someone help me?
  Here is my code: </p>
  
  <pre><code>int Number(int *A, int N, int X) {
      int r, m, l;
      if (N == 0) {
          return -1;
      }
      l = 0;
      r = N - 1;
      while (l &lt; r) {
          m = (l + r) / 2;
          if (A[m] &gt; X)  {
              r = m - 1;
          } else {
              l = m;
          }
      }
      if (A[l] == X) {
          return l;
      }
      return -1;
  }
  </code></pre>