return value
-
This recursive function doesnt have a end point,becuase 12 item is not exist in the array.How we can fix this recursive function to solve this problem?
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int sum;sum=BinSearch(arr,12,0,9); cout< A\[mid\]) return Binary\_Search(A,item,mid+1,height); else return Binary\_Search(A,item,low,mid-1);
}
-
This recursive function doesnt have a end point,becuase 12 item is not exist in the array.How we can fix this recursive function to solve this problem?
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int sum;sum=BinSearch(arr,12,0,9); cout< A\[mid\]) return Binary\_Search(A,item,mid+1,height); else return Binary\_Search(A,item,low,mid-1);
}
-
This recursive function doesnt have a end point,becuase 12 item is not exist in the array.How we can fix this recursive function to solve this problem?
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int sum;sum=BinSearch(arr,12,0,9); cout< A\[mid\]) return Binary\_Search(A,item,mid+1,height); else return Binary\_Search(A,item,low,mid-1);
}
-
This recursive function doesnt have a end point,becuase 12 item is not exist in the array.How we can fix this recursive function to solve this problem?
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int sum;sum=BinSearch(arr,12,0,9); cout< A\[mid\]) return Binary\_Search(A,item,mid+1,height); else return Binary\_Search(A,item,low,mid-1);
}
The array passed into the function could potentially have any value in it you can't select a simple return value to represent your error condition. I'd consider throwing an exception rather than using a return value:
if( A[low] > item || A[high] < item] ) throw binary_search_out_of_bounds();
Another option is to have the function return a
std::pair<bool, int>
where the first item in the pair tells the caller if they have an error condition or not and the second is the value they're after. Personally I find that a bit ugly and it complicates your code more as you have to do more in the calling function so I'd go with an exception. Cheers, Ash -
The array passed into the function could potentially have any value in it you can't select a simple return value to represent your error condition. I'd consider throwing an exception rather than using a return value:
if( A[low] > item || A[high] < item] ) throw binary_search_out_of_bounds();
Another option is to have the function return a
std::pair<bool, int>
where the first item in the pair tells the caller if they have an error condition or not and the second is the value they're after. Personally I find that a bit ugly and it complicates your code more as you have to do more in the calling function so I'd go with an exception. Cheers, Ash