I have binary search - this one , but i need to change it like this , that it will search : for example we have array {1,10,11,12,15,16,22,22,22,22,30,40} I want to find 22 but not randomly - or it will be first 22(that goes after 16) or last 22 (that before 30)

int binary_search(int search_key, int list_size, const int array_to_search[], char success[]) {
  int first, last, mid, index;
  strcpy(success, "FALSE");
  index = -1;
  first=0;
  last=list_size-1;
  while (first <= last && strcmp(success, "FALSE") == 0)
  {
	  mid = (first +last) / 2;
      if (array_to_search[mid] == search_key)
      {
           index=mid;
           strcpy(success, "TRUE");
      }
	  else if (array_to_search[mid] > search_key ) {last = mid-1;}
      else first = mid +1; 
  }  
  return index;
}

Thank u!!!!

then why don't you find 22 then after that -1 if the number before it is 22 then decrement it again untill the number is not 22 then you have there is your first 22

commented: Good solution. +26

<deleted>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.