| Bardikus (3) | |
|
hello I want to implement binary search.The code seems correct to me,but I think a small mistake I cant see makes it not work,here is the code(and thank you in advance): #include<stdio.h> #include<conio.h> int low=0,high,v[10],i,x; int binarysearch(int v,int x,int low,int high); int main() { printf("val cautata x="); scanf("%d",&x); printf("\n"); printf("lungimea vectorului este="); scanf("%d",&high); for(i=0;i<=high-1;i++) { scanf("%d",v[i]); } printf("%d",binarysearch(v,x,low,high)); getch(); } int binarysearch(int v,int x,int low,int high) { int mid; if(high<low) return -1; mid=(low+high)/2; if (x>v[mid]) return binarysearch(v,x,mid+1,high); else if (x<v[mid]) return binarysearch(v,x,low,mid-1); else return mid; } | |
|
|
|
| hamsterman (4325) | |
scanf("%d",v[i]); that should be &v[i].
| |
|
|
|
| beginner2011 (21) | |
|
1) int binarysearch(int v,int x,int low,int high); should be written as int binarysearch(int v[],int x,int low,int high); 2) scanf("%d",v[i]); should be written as scanf("%d",&v[i]); | |
|
|
|