1

When I run this it always shows Number Not Found when the number is other than one i.e. it runs correctly for number 1.

I want to know what is the problem with this as according to me it runs all my test cases correctly.

int search(int *a,int start,int end,int num)
{
  int mid;
  mid=(start+end)/2;
  if(start==end)
  {
    if(num==a[start])
      printf("Number Found");
    else
      printf("Number Not Found");
  }
  else
  {
    if(num>a[mid])
      search(&a[mid+1],mid+1,end,num);
    else
      search(&a[start],start,mid,num);
  }
}

int main()
{
  int arr[10]={1,2,3,4,5,6,7,8,9,10};
  search(arr,0,9,10);
}
3
  • HINT: this two conditions are wrong search(&a[mid+1],mid+1,end,num); else search(&a[start],start,mid,num); Commented Feb 3, 2013 at 17:52
  • 1
    do try with search(a,mid+1,end,num); else search(a,start,mid-1,num); Commented Feb 3, 2013 at 17:59
  • 2
    Don't ever use this line of thinking again: "as according to me it runs all my test cases correctly." Commented Feb 3, 2013 at 18:00

1 Answer 1

2

You should not pass a different pointer to the array; instead you are already changing the end indices of the intervals you are interested in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.