https://ide.codingblocks.com/s/480784 —> Please Check my code.
Doubt doubt doubt
if (first > last)
{
cout << "not found" << endl;
return; // why do i have to write a return statement here??
// Can you explain whats happens if i do not put a return statement here. its not working without return.
}
you need to return from here because if first > last it means we don’t have array now so we don’t want to execute the code below this
you can also use else condition to wrap other part Like this
void findNum(int *arr, int first, int last, int k)
{
if (first > last)
{
cout << "not found" << endl;
// no need of return statement now
}else{
int mid = (first + last) / 2;
if (arr[mid] > k)
{
findNum(arr, first, mid - 1, k);
}
else if (arr[mid] < k)
{
findNum(arr, mid + 1, last, k);
}
else
{
cout << mid << endl; // Also do i need a return statement here??
}
}
}
also
else
{
cout << mid << endl; // Also do i need a return statement here??
}
no need to return from here as code will automatically goes to the end of execution
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.