Binary search error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bravephantom
    New Member
    • Dec 2006
    • 27

    Binary search error

    error: subscript requires array or pointer type

    thats my code

    void main(int argc, char* argv[])
    {
    int s[10];
    int n=10;
    int location;
    int x;

    cout<<"enter the value you want to search for: "<<endl;
    cin>>x;

    for(int i;i<n;i++)
    s[i]=rand()%10;

    binsearch(n,s,x ,location);

    //return 0;
    }

    void binsearch(int n, int s,int x,int& location)
    {
    int low,high,mid;

    low=1;
    high=n;
    location=0;
    while((low<=hig h)&&(location== 0))
    {
    mid=floor((low+ high)/2);
    if(x==s[mid])
    location=mid;
    else if(x<s[mid])
    high=mid-1;
    else
    low=mid+1;
    }

    }

    i donno whats wrong, its supposed to be workin.
    thnx for help
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Your s parameter needs to be a pointer or an array (it's the same thing, read this). Also, void main() is nonstandard, use int main() instead and uncomment the return 0 at the end of the function.

    Comment

    Working...