Welcome to the Linux Foundation Forum!

HELP... error in my simple binary search program

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

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: ";<br />
cin>>n;

cout<<"\nEnter the array: ";<br />
for(int i=0;i
cin>>a[i];

cout<<"\nEnter the element you want to search: ";<br />
cin>>ele;

int low=0,high=n-1;

while(low<=high)<br />
{

mid=low+high/2;

if(ele==a[mid])

{

pos=mid+1;

break;

}

else if(ele
high=mid-1;

else

low=mid+1;

}

if(pos!=0)

cout<<"\nThe element you searched is "<<a[mid]<<" and is at position "<<pos<<endl;<br />
else

cout<<"\nThe element you were trying to search was not found !"<<endl;<br />
return 0;

}

Comments

  • jabirali
    jabirali Posts: 157
    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? :)
  • mfillpot
    mfillpot Posts: 2,177
    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.
  • Well, thank you!
  • #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);
    }
    }

Categories

Upcoming Training