Binary search program

please send me the of binary search without using function just normal way how can I do it and is it possible to do with for loop then please send It to me


binary search can be done using while loop

#include using namespace std; int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; } int key; cout<<“element want to search”; int s=0; int e=n-1; while(s<=e){ cin>>key; int m=(s+e)/2; if(a[m]==key){ cout<<m<<endl; } if(a[m]>key){ e=m-1; } else{ s=m+1; } } cout<<“not found”<<endl; return 0; }

please correct my program and send me please help me where I was wrong correct me

its correct only when I am giving key in output I have to give 3 times then index is back to me please correct that program

#include using namespace std; int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; } int key; cout<<“element want to search”; int s=0; int e=n-1; while(s<=e){ cin>>key; int m=(s+e)/2; if(a[m]==key){ cout<<m<<endl; } if(a[m]>key){ e=m-1; } else{ s=m+1; } } return 0; }

I have corrected ur code

why in function we don’t have to use anything like bool in normal I did why its doing please explain me that I dint understand that thing explain me in clear way why so we are doing it

what the diff its making I just want to understand that

explain me and clear my dougts what’s diff its making in function we do not have to do anything but in normal why so we are doing like that

because in function u can directly return… how can u do the same in normal… and pls go through videos carefully… i think u are not watching them properly that’s why u ask these kind of doubts every time

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.