English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

//program to implement binary search
#include
#include
void main()
{
clrscr();
int a[10],min=0,max=9,mid=0,p=-1,num;
for(int i=0;i<=9;i++)
{ cin>>a[i]; }
cout<<"enter: ";
cin>>num;
while((max>=mid)&&(p==-1))
{ mid=(min+max)/2;
if(a[mid]==num)
{ p=mid; }
else if(a[mid]>num)
{ min=mid+1; }
else
{ max=mid-1; }
}
if(p>-1)
{ cout<<"at"< else
{ cout<<"sorry!"; }
getch();
}

2007-02-25 01:31:18 · 4 answers · asked by jayati j 2 in Computers & Internet Software

i didn't find any answers good...infact i found my mistake and that was with lines...min=mid+1;...and another...max=mid-1;

2007-02-25 19:09:54 · update #1

4 answers

Error in While Loop Condition i.e. while(max>=min && p==-1)
another errors in If statements.
correct Prog is :

//program to implement binary search
#include
#include
void main()
{
clrscr();
int a[10],min=0,max=9,mid=0,p=-1,num;
for(int i=0;i<=9;i++)
{ cin>>a[i]; }
cout<<"enter: ";
cin>>num;
while((max>=min)&&(p==-1))
{ mid=(min+max)/2;
if(a[mid]==num)
{ p=mid; }
else if(a[mid]>num)
{ max=mid-1; }
else
{ min=mid+1; }
}
if(p>-1)
{ cout<<"at"< else
{ cout<<"sorry!"; }
getch();
}

2007-02-26 14:35:30 · answer #1 · answered by Indu Goel 1 · 0 0

mid can not be int, because you are assigning a fraction value to mid variable(mid=(min+max)/2) and see C book written by K & R

2007-02-25 09:48:57 · answer #2 · answered by Anonymous · 0 0

int a[10],min=0,max=9,mid=0,p=-1,n...

2007-02-25 09:33:49 · answer #3 · answered by boom boom 2 · 0 0

i think mid should b declared as double as it does the operation
mid==(min+max)/2;

2007-02-25 09:47:59 · answer #4 · answered by ash 1 · 0 0

fedest.com, questions and answers