i want to implement binary_search with pointers
#include <cstdlib>
#include <iostream>
using namespace std;
int binary_p(int x[],int size,int target){
int *p=&x[0];
int *q=&x[size];
while(p<q){
int mid=(p+q)>>2;
if (target==x[*mid]) return 1;
else if(target<x[*mid]) q=mid-1;
else p=mid+1;
}
return -1;
}
int main(int argc, char *argv[])
{
int x[]={2,4,6,7,9,10,12};
int size=sizeof(x)/sizeof(int);
int target=9;
cout<<binary_p(x,size,target)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
but here is error list
prog.cpp: In function ‘int binary_p(int*, int, int)’:
prog.cpp:10: error: invalid operands of types ‘int*’ and ‘int*’ to binary ‘operator+’
prog.cpp:11: error: invalid type argument of ‘unary *’
prog.cpp:12: error: invalid operands of types ‘int*’ and ‘int’ to binary ‘operator*’
prog.cpp:12: error: invalid conversion from ‘int’ to ‘int*’
prog.cpp:13: error: invalid conversion from ‘int’ to ‘int*’
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:30: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result
please could anybody give me advise
Here is the working code.