int linearsrch(int *a,int n,int key)
{
if(n==0)
{
return -1;
}
if(a[0]==key)
{
return (what should I write to return return index of the key element) ;
}
return linearsrch(a+1,n-1,key);
}
Change in approach
PLease help in the above doubt
@deepaknike032
Instead of changing your pointer to iterate over the array , you should rather use a seperate index variable to iterate. This way , you can return the value of index variable when you find the key data in your array.
int linearsrch(int *a,int i,int n,int key)
{
if(i == n)
{
return -1;
}
if(a[i]==key)
{
return i;
}
return linearsrch(a,i+1,n,key);
}
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.