Search code examples
carrayssortingsearchbinary-search

Binary search not returning the position


this code is used to create an array filled with 10 random integers. It sorts the array and then inputs the array into a binary search function. I do not get the position of where my search key is positioned.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int binary_search(int array[], int search, int strt, int ending)
{
   int middle;

   middle = (strt + ending)/2;//splitting the array in half to compare the search key

   if (search > array[middle]){binary_search(array, search, middle + 1, ending);}
   else if(search == array[middle])
    {
     printf("Your search key is indicated in %d position the array Ferrari\n", middle + 1);
     return middle;
    }
   else{binary_search(array, search, strt, middle -1);}


 return -1;
}


int main()
{
  srand(time(NULL));//random number seed generator
  int Ferrari[10];
  int size = 10;
  int selection;
  int temporary = 0;//I'm using this variable to store 
                    //the value returned from linear_search()
  int start = 0;
  int end;
  int i;

  //this is to generate a random number between 0 and 101   
  for(int i=0; i<10; i++) {Ferrari[i] = rand() % 100 + 1;}

   //printing the initial array     
   printf("\nThe array Ferrari consists of -> ");

   for(int i=0; i<10; i++){printf("%d, ", Ferrari[i]);}


//--------------------------SORTING--------------------------------------------
 for(int f = 0; f < (size - 1); f++)
{
    for(int kk = 0; kk < (size - 1 - f); kk++)
      {
        if(Ferrari[kk] > Ferrari[kk +1])
          {
            int Te_mP;
            Te_mP = Ferrari[kk + 1];
            Ferrari[kk+1] = Ferrari[kk];
            Ferrari[kk] = Te_mP;
           }
      }
  }

  //----------------------------------------------------------------------------

   //printing the array after it has been sorted    
   printf("\n");
   printf("\nThe sorted array Ferrari consists of -> ");

   for(int i=0; i<10; i++){printf("%d, ", Ferrari[i]);}

    start = 0;
    end = i -1;

  //this will be used to implement the searching algorithm  
   printf("\n\n");
   printf("Please enter a number to test if it is included in the array or not\n");     

   scanf("%d", &selection);


   temporary = binary_search(Ferrari, selection, start, end);


 return 0;
 }

I keep getting the answer that the search key is positioned in ``0 of array Ferrari. How do I resolve this?

Please let me know what I'm doing wrong over here. Much appreciated.


Solution

  • The problem is occurring because you are using uninitialised variable i here:

    end = i -1;
    

    Note that the scope of variable i declared in loop init clause is different from the scope of variable i declared in function block.

    for(int i=0; i<10; i++){
        ^^^^^^^
       // The scope of i declared in loop init clause is limited to the loop. 
    

    To fix the problem, you can use the i declared at function block scope as the loop variable, like this

    for(i=0; i<10; i++){
    

    Now, after the loop finishes, the variable i will hold its last value until it's value explicitly modify. But using i to identify the size of array down the code may cause several problems as it is not tightly coupled with the size of array and i may be get modified by other part of code. So, it is not the right idea to use i to identify the size of array.

    Since, you are having a variable size which hold the size of array Ferrari, you can do:

    end = size - 1;
    

    No need to have another variable to keep the track of size of array. The problem with this is that you have to keep updating the size whenever you change the array size. An alternative of this would be to use a macro to define the array size.

    The most appropriate way to set the end of array would be:

    end = (sizeof(Ferrari) / sizeof(Ferrari[0])) - 1;