this is a code for the binary search program in c language.now the program is not showing any error but when i run this program then the output is like:
enter the total numbers:6
enter the array elements: 5 4 7 1 3 2
the sorted numbers are:1 2 3 4 5 7
enter the number to be searched:5
then it prints::::the number is not found
now why does it always print the number is not found no matter what number i enter.it always show "the number is not found"????why?????it is printing the sorted list alright.so why not the number then?plz help me out!!!!!!!!

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
int beg,end,mid,target;
clrscr();
printf(“enter the total numbers:);
scanf(“%d,&n);
printf(“enter the array elements: );
for(i=0;i<n;i++)
scanf(“%d,&a[i]);
for(i=0;i<n-1;i++)
    {  
     for(j=0;j<n-i-1;j++)
       {
          If(a[j+1]<a[j])
            {
               temp=a[j];
                a[j]=a[j+1];
                 a[j+1]=temp;
            }
      }
  }
printf(“the sorted numbers are:);
for(i=0;i<n;i++)
printf(“%4d,a[i]);
beg=a[0];
end=a[9];
mid=(beg+end)/2;
printf(“\nenter the number to be searched:);
scanf(“%d,&target);
while(beg<=end && a[mid]!=target)
{
if(target<a[mid])
 end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
}
If(a[mid]==target)
  {
printf(“\nthe number is found at position %2d,mid);
  }
else
    {
printf(“\nthe number is not found:);
     }
getch();
}

Recommended Answers

this is a code for the binary search program in c language.now the program is not showing any error but when i run this program then the output is like:
enter the total numbers:6
enter the array elements: 5 4 7 1 3 2
the sorted numbers are:1 2 …

Jump to Post

Here is your culpret:

beg=a[0];
end=a[9];

beg should be the bottom of the range to search, in this case 0.
end should be the top of the range to search, in this case n (not 9).

You are taking the CONTENTS of the item at that point, …

Jump to Post

All 6 Replies

this is a code for the binary search program in c language.now the program is not showing any error but when i run this program then the output is like:
enter the total numbers:6
enter the array elements: 5 4 7 1 3 2
the sorted numbers are:1 2 3 4 5 7
enter the number to be searched:5
then it prints::::the number is not found
now why does it always print the number is not found no matter what number i enter.it always show "the number is not found"????why?????it is printing the sorted list alright.so why not the number then?plz help me out!!!!!!!!

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
int beg,end,mid,target;
clrscr();
printf(“enter the total numbers:);
scanf(“%d,&n);
printf(“enter the array elements: );
for(i=0;i<n;i++)
scanf(“%d,&a[i]);
for(i=0;i<n-1;i++)
    {  
     for(j=0;j<n-i-1;j++)
       {
          If(a[j+1]<a[j])
            {
               temp=a[j];
                a[j]=a[j+1];
                 a[j+1]=temp;
            }
      }
  }
printf(“the sorted numbers are:);
for(i=0;i<n;i++)
printf(“%4d,a[i]);
beg=a[0];
end=a[9];
mid=(beg+end)/2;
printf(“\nenter the number to be searched:);
scanf(“%d,&target);
while(beg<=end && a[mid]!=target)
{
if(target<a[mid])
 end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
}
If(a[mid]==target)
  {
printf(“\nthe number is found at position %2d,mid);
  }
else
    {
printf(“\nthe number is not found:);
     }
getch();
}

I Think You have done a mistake while defining the value of 'end', I think it should be end=a[5], Try this! :surprised :surprised

plz help me out here????
im not able to understand...what do u want me to try?could u plz make some necessary changes in my code in order to make it properly run!!!!!!

Here is your culpret:

beg=a[0];
end=a[9];

beg should be the bottom of the range to search, in this case 0.
end should be the top of the range to search, in this case n (not 9).

You are taking the CONTENTS of the item at that point, not the INDEX of the item.

Also, I suggested using n, not n-1, and in that case you'd loop while beg < end, not beg <= end. At least thats how it seems to me, you should double check.

ok now when i run this it gives me the out put correctly ...thx for helping me out.....thx.alot.

/* program for binary search using non recursive method  */
/* contact pranay at - anupcus123 @ yahoo.com*/

#include <stdio.h>
#include <conio.h>

int main(void)
{
int from = 0;  int to,target ;
int mid,a[100],i,j,n,temp;
/*---------------------------------*/
printf("enter the total numbers:");
scanf("%d",&n);
printf("enter the array elements:" );
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("target:" );scanf("%d",&target);
/*---------------------------------*/
/*.....................sort.output..........................................*/
for(i=0  ;i< n-1   ;i++ )   {
for(j=0  ;j< n-i-1 ;j++ )   {
if (    a[j+1]  < a[j]  )   {   temp=a[j]; a[j]=a[j+1];  a[j+1]=temp;  }
                }
                }
printf("the sorted numbers are:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
/*...................................................*/
to=n-1;
while (from <= to)
{
 mid = (from + to) / 2;
if (a[mid] == target)
goto re;
else if (target < a[mid])
to = mid - 1;
else
from = mid + 1;
}
re:
   printf("\n beg = %d ,end = %d , mid = %d  target = %d \t", from , to , mid,target);
   printf("\n");
   getch();
   return 0;
}

Is there any reason you resurrected a seven month old thread to post ugly code? Be careful with your answer because it will dictate whether or not I close this thread (and my finger is on the big red button as it is).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.