Nov 17, 2014 at 2:40pm UTC
i did a binarysearch usig c++ but the output is empty
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include<iostream>
using namespace std;
int binarysearch(int a[],int maxsize,int target)
{
int low=0;
int high=maxsize-1;
int mid=(low+high)/2;
while (a[mid]!=target && low<=high)
{
if (a[mid]>target)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
if (a[mid]==target)
{
return mid;
}
else
{
return -1;
}
}
int main()
{
int j[8]={1,2,3,4,5,6,7,8};
binarysearch(j,8,5);
}
Last edited on Nov 17, 2014 at 2:40pm UTC
Nov 17, 2014 at 3:16pm UTC
You should probably be changing mid in your while() loop instead of high and low. And you should probably use the return value instead of discarding it in main().
Last edited on Nov 17, 2014 at 3:17pm UTC
Nov 17, 2014 at 5:24pm UTC
can you write the code pleasse
Nov 17, 2014 at 5:33pm UTC
No, thank you anyway. You give it a try.
Last edited on Nov 17, 2014 at 5:33pm UTC