Linux.com

Home Linux Community Forums Programming and Development Software Development HELP... error in my simple binary search program

HELP... error in my simple binary search program

Link to this post 03 Apr 11

Could any help me with a runtime error in my program?

In the program of binary search, I am not getting output if I choose my searching element more than the middle element.
Could anyone please help me to debug it?


#include<iostream>
using namespace std;
int main()
{
int a[100],n,ele,mid,pos=0;
cout<<"\nEnter the number of elements you want to enter in an array: ";
cin>>n;
cout<<"\nEnter the array: ";
for(int i=0;i<n;++i)
cin>>a;
cout<<"\nEnter the element you want to search: ";
cin>>ele;
int low=0,high=n-1;
while(low<=high)
{
mid=low+high/2;
if(ele==a[mid])
{
pos=mid+1;
break;
}
else if(ele<a[mid])
high=mid-1;
else
low=mid+1;
}
if(pos!=0)
cout<<"\nThe element you searched is "<<a[mid]<<" and is at position "<<pos<<endl;
else
cout<<"\nThe element you were trying to search was not found !"<<endl;
return 0;
}

Link to this post 05 Apr 11

The problem is here:

mid=low+high/2;
Since division has a higher precedence than addition, your code is equivalent to the following:
mid = (low) + (high/2);
The program seems to work fine if you replace that line with this:
mid=(low+high)/2;

Edit:
By the way, why is this posted under the category Installation and not under Software Development? :)

Link to this post 05 Apr 11

jabirali wrote:
By the way, why is this posted under the category Installation and not under Software Development? :)[/quote]

I moved the discussion to the correct section.

Link to this post 05 Apr 11

Well, thank you!

Link to this post 07 Apr 11

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct tree
{
int data ;
struct tree *left,*right;
}*root, *T=NULL;
typedef struct tree *node;
node insert(int,node t);
node FindMin(node t);
node del(int,node t);
void display(node t);
int main()
{
int item,n,i;
char c;
printf("\nenter the number of elemnets in tree..:");
scanf("%d",&n);
printf("\nenter the elements:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&item);
T=insert(item,T);
}
printf("\nelements displayed in inorder:\n");
display(T);
printf("\nenter the elements to delete:\n");
scanf("%d",&item);
T=del(item,T);
printf("\ncontents of tree after deletion:\n");
display(T);
}
node insert(int x, node T)
{
struct tree *temp;
temp=malloc(sizeof(struct tree));
if(temp==NULL)
printf("\nout of space");
else
{
if(T==NULL)
{
temp->data=x;
temp->left=temp->right=NULL;
T=temp;
}
else
{
if(x<T->data)
T->left=insert(x,T->left);
else
T->right=insert(x,T->right);
}
}
return T;
}
node del(int x, node T)
{
node temp;
if(T==NULL)
printf("\nelement not found");
else
{
if(x<T->data)
T->left=del(x,T->left);
else
if(x>T->data)
T->right=del(x,T->right);
else
if(T->left && T->right)
{
temp=FindMin(T->right);
T->data=temp->data;
T->right=del(T->data,T->right);
}
else
{
temp=T;
if(T->left==NULL)
T=T->right;
else
if(T->right==NULL)
T=T->left;
free(temp);
}
}
return T;
}
node FindMin(node T)
{
if(T!=NULL)
{
if(T->left==NULL)
return T;
else
return FindMin(T->left);
}
}
void display(node T)
{
if(T!=NULL)
{
display(T->left);
printf("%d\n",T->data);
display(T->right);
}
}

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board