title: Binary Search in 2D Array 3
body: <p>I need to find a number in the 2D array. 
  Columns are sorted (from smallest value to the biggest).</p>
  
  <p>Here is my code:</p>
  
  <pre><code>const int SIZE = 4;
  const int NOT_FOUND = 1;
  int  binarySearch(int mat[][SIZE], int &amp;line , int num);
  
  void main()
  {
      int num, index,  mat[SIZE][SIZE] = { 11,1,5,11,
                                           11,6,7,2,
                                           8,7,7,7,
                                           0,12,9,10 };
  
      int line = sizeof(mat) / sizeof(mat[0][0]);
  
      cout &lt;&lt; "please type a number to search: " &lt;&lt; endl;
      cin &gt;&gt; num;
  
      index = binarySearch(mat, line, num);
  
      if (index == NOT_FOUND) {
          cout &lt;&lt; "The value: " &lt;&lt; num &lt;&lt; "doesn't exist in the array\n";
      }
      else {
          cout &lt;&lt; "The value: " &lt;&lt; num &lt;&lt; " exists in line " &lt;&lt; line+1 
          &lt;&lt;" and column: " &lt;&lt; index+1 &lt;&lt; endl;
      }
      system("pause");
  }
  
  
  int binarySearch(int mat[][SIZE], int &amp;line, int num)
  {
      for (int j = 0; j &lt; SIZE; j++)
      {
          int low = 0;
          int high = SIZE - 1;
          int middle;
  
          while (low &lt;= high)
          {
              middle = (low + high) / 2;
  
              if (num == mat[middle][j])
              {
                  line = middle;
                  return j;
              }
              else if (num &lt; mat[middle][j]) {
                  high = middle - 1;
              {
              else {
                  low = middle + 1;
              }
          }
      }
      return NOT_FOUND;
  }
  </code></pre>
  
  <p>The program doesn't find all the numbers in the array.
  Something doesn't work.</p>
  
  <p>What is the problem?</p>