hey i tried to make a program to find an item in an integer array using binary search technique n by iteration with the help of functions...but i am getting errors....so plz help me out..

## All 6 Replies

EDIT:
Looks like Tom Gunn is one step ahead of me, as always! :)

hey m a new user....n i tried to make a program to search a no. in an integer array using binary search tech,,,,n w/o recursion but the program i made is not doing well....
the position of the no. to b searched is cuming out to b wrong with a null pointer assignment....
here's the program i made...i tried to hard but still there's the same problem...so plz lemme knw where m i going wrong with this program....
here's my program....

``````/*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
}
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);
}
``````

thanx
vandna

Your program is pretty close, you just need a few changes, such as `int main()` , use pointer to array in scanf() and flush out the '\n' characters

``````int main()
{
int array[50],i,ele,n;
char a;
//clrscr();
printf("the list should be sorted in ascending order\n");
printf("enter the size of array\n");
scanf("%d%c",&n,&a);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d%c",&array[i],&a);
}
printf("enter the element to be searched\n");
scanf("%d%c",&ele,&a);
binary(array,n,ele);
}``````

hey thank you ancient dragon....it worked....but will u plz lemme knw...how this char a made the program to be correct....wats its effect on the program...????

<snipped> Nevermind that was a bad post, sorry.

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.