title: Binary search function in C
body: <p>The program 'randomly' chooses a set of 8 numbers and then the user has to guess as many of those numbers as they can. But even if I type in the numbers in this array of random numbers, the binary_search function doesn't find that exact number in the array of 'random' numbers. I can't seem to find the mistake in my binary_search function (at least I suspect that that's where the problem is). I have tried the program with large SIZE of numbers and small too. Could anyone help me please?</p>
  
  <pre><code>#define SIZE 3
  #define min  1
  #define max  36
  
  int random();
  bool binary_search(int value, int values[], int n);
  
  int main(void)
  {
      int numbers[SIZE];
      for(int i = 0; i &lt; SIZE; i++)
          numbers[i] = random();
      int score = 0;
      int n[SIZE];
      int number_user;
  
  
      for(int i = 0; i &lt; SIZE; i++)
      {   
          //Type in a number
          do
          {
              printf("Search number no.%d:\n&gt; ", i + 1);
              n[i] = GetInt();
  
              //Wrong input number
              if(n[i] &lt; min || n[i] &gt; max)
              {
                  printf("\nNumber should be between %d - %d!!!\n\n", min , max);
              }
          }
          while(n[i] &lt; min || n[i] &gt; max);
  
          //THE PROBLEMATIC STUFF - START
          number_user = n[i];
          if (binary_search(number_user, numbers, SIZE))
          {
              printf("FOUND!!!\n");
              score += 1;
          }
          else 
          {
              printf("NOT IN THE LIST\n");
          }
          //PROBLEMATIC STUFF - FINISH
      }
  
      //scoring
      printf("\n\n*******SCORE*******\n");
          //print your numbers
          for(int i = 0; i &lt; SIZE; i++)
          {
              printf(" %d", n[i]);
              if(i != SIZE - 1)
                  printf(",");
              if(i == SIZE - 1)
                  printf(".\n");
          }
  
          //print right numbers
          printf("\nRight numbers are:");
          for(int i = 0; i &lt; SIZE; i++)
          {
              printf(" %d", numbers[i]);
              if(i != SIZE - 1)
                  printf(",");
              if(i == SIZE - 1)
                  printf(".\n");
          }
  
      int result = score / SIZE * 100;
      printf("Your score is: %d / %d , %d %%\n", score, SIZE, result);
  
  
      return 0;    
  }
   //PROBLEM START
  bool binary_search(int value, int values[], int n)
  {
      int beginning = 0;
      int ending = n - 1;
  
      int middle;
  
      while (ending &gt;= beginning) 
      {    
          middle = (beginning + ending) / 2; 
          for(int i = 0; i &lt; SIZE; i++)
          {
              //look at middle of list if (binary_search(n, numbers, SIZE))
              if(values[middle] == value)
                  //if number found, return true 
                  return true;
  
              //else if middle higher, search left 
              else if(values[middle] &gt; value)
                  ending = middle - 1;
  
              //else if middle lower, search right 
              else if(values[middle] &lt; value)
                  beginning = middle + 1;
          } 
      }
      return false; 
  }
  //PROBLEM FINISH
  
  int random()
  {
      int r = rand() % ((max - min + 1) + min);
  
      return r;
  }
  </code></pre>