Jumat, 08 Juli 2011

Help With Binary Search Program

HI, everyone. I am having trouble getting the index of the location of the search number to print out. This is my program. Any help would be greatly appreciated!

C++ Syntax (Toggle Plain Text)
#include<iostream.h>
#include<cmath>
void sortit(int x[], int n)
{
int temp,i,j;
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
void bsearch(int x[], int max, int searchnum)
{
int mid;
int high=0;
int size;
int low = size-1;
int location = -1;
int found;
while(high <=low && found==false)
{
mid=(high + low)/2;
if(x[mid]==searchnum)
location=mid;
found=true;
else if(searchnum>>x[mid])
high=(mid + 1);
else
low= (mid - 1);
}
}
main()
{
int num[11];
int max=10;
int i,mid,x,location;
int searchnum;
int found;
for(i=0;i<11;i++)
{
cin>>num[i]; //
}
sortit(num,max);
cout<<"The list sorted is: "<<endl;
for(int i=0;i<max;i++)
cout<<num[i]<<"\n";
while(cin>>searchnum)
{
bsearch(num,max,searchnum);
}
cout<<"The number is " <<searchnum<<endl;
if(found=true)
cout<<"The number was found at location" <<location<<endl;
else cout<<"Number not found" <<"\n";
}

Tidak ada komentar:

Posting Komentar