hey m a new user so didnt knw it...
nywyas here's the program i made..

/*A PROGRAM TO SEARCH AN ELEMENT IN 1D ARRAY USING BINARY SEARCH USING ITERATION*/
#include<stdio.h>
#include<conio.h>
void binary(int array[],int n,int ele)
{
    int beg=0;
    int end=n-1;
    int mid;
    mid=(beg+end)/2;
    while((beg<=end)&&(array[mid]!=ele))
    {
        if(ele>array[mid])
        beg=mid+1;
        else
        end=mid-1;
        mid=(beg+end)/2;
    }
    if(array[mid]==ele)
    printf("element found at %d",mid);
    else
    printf("element not found\n");
}
void main()
{
    int array[50],i,ele,n;
    clrscr();
    printf("the list should be sorted in ascending order\n");
    printf("enter the size of array\n");
    scanf("%d",&n);
    printf("enter the elements\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",array[i]);
    }
    printf("enter the element to be searched\n");
    scanf("%d",&ele);
    binary(array,n,ele);
}

and the output is not right....the position of the item to be searched is cuming out to b wrong..
n showing null pointr assignment..

Recommended Answers

All 4 Replies

Nice program....Why did you post it?

hey the output is showing errors...the position of element to b searchd is not cumin out to b ryt

make the following correction in main()

for(i=0;i<n;i++)
{
      scanf("%d",&array[i]); /*& added*/
}

Next time use code tags when posting code. That [code] link isn't just to make DaniWeb pretty.

Be a part of the DaniWeb community

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