Strange output

Discussion in 'C++' started by junw2000@gmail.com, Aug 4, 2006.

  1. Guest

    I wrote a simple binary search function:

    int binary_serach_R(int a[], int k, int l, int r){
    int m = (l+r)/2;
    if(a[m] == k){
    std::cout<<"return a[m] "<<a[m]<<'\n'; //LINE1
    return a[m]; //LINE2
    }
    if(l == r) return -1;
    // std::cout<<a[m]<<" "<<l<<" "<< m<<" "<<r<<'\n';
    if(a[m] > k) binary_serach_R(a, k, l, m);
    else binary_serach_R(a, k, m, r);
    }

    LINE1 can always output a correct value. But LINE2 always returns a
    random value,
    like -1207956288. What is the problem?

    Thanks.

    Jack
    , Aug 4, 2006
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > I wrote a simple binary search function:
    >
    > int binary_serach_R(int a[], int k, int l, int r){
    > int m = (l+r)/2;
    > if(a[m] == k){
    > std::cout<<"return a[m] "<<a[m]<<'\n'; //LINE1
    > return a[m]; //LINE2
    > }
    > if(l == r) return -1;
    > // std::cout<<a[m]<<" "<<l<<" "<< m<<" "<<r<<'\n';
    > if(a[m] > k) binary_serach_R(a, k, l, m);
    > else binary_serach_R(a, k, m, r);
    > }
    >
    > LINE1 can always output a correct value. But LINE2 always returns a
    > random value,
    > like -1207956288. What is the problem?


    You forgot to return the value from your recursive calls.

    Cheers! --M
    mlimber, Aug 4, 2006
    #2
    1. Advertising

  3. Guest

    mlimber wrote:
    > wrote:
    > > I wrote a simple binary search function:
    > >
    > > int binary_serach_R(int a[], int k, int l, int r){
    > > int m = (l+r)/2;
    > > if(a[m] == k){
    > > std::cout<<"return a[m] "<<a[m]<<'\n'; //LINE1
    > > return a[m]; //LINE2
    > > }
    > > if(l == r) return -1;
    > > // std::cout<<a[m]<<" "<<l<<" "<< m<<" "<<r<<'\n';
    > > if(a[m] > k) binary_serach_R(a, k, l, m);
    > > else binary_serach_R(a, k, m, r);
    > > }
    > >
    > > LINE1 can always output a correct value. But LINE2 always returns a
    > > random value,
    > > like -1207956288. What is the problem?


    > You forgot to return the value from your recursive calls.
    >

    Thanks. I got it. Then where is the random value from?
    Jack
    , Aug 4, 2006
    #3
  4. mlimber Guest

    wrote:
    > mlimber wrote:
    > > wrote:
    > > > I wrote a simple binary search function:
    > > >
    > > > int binary_serach_R(int a[], int k, int l, int r){
    > > > int m = (l+r)/2;
    > > > if(a[m] == k){
    > > > std::cout<<"return a[m] "<<a[m]<<'\n'; //LINE1
    > > > return a[m]; //LINE2
    > > > }
    > > > if(l == r) return -1;
    > > > // std::cout<<a[m]<<" "<<l<<" "<< m<<" "<<r<<'\n';
    > > > if(a[m] > k) binary_serach_R(a, k, l, m);
    > > > else binary_serach_R(a, k, m, r);
    > > > }
    > > >
    > > > LINE1 can always output a correct value. But LINE2 always returns a
    > > > random value,
    > > > like -1207956288. What is the problem?

    >
    > > You forgot to return the value from your recursive calls.
    > >

    > Thanks. I got it. Then where is the random value from?
    > Jack


    It's whatever junk is in the location where your return value would get
    stored (probably a value on the stack). I'm surprised your compiler let
    you get away with that without warning you (or did you ignore the
    warning?); if it didn't bark, you need to turn up your warning level.

    Cheers! --M
    mlimber, Aug 4, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nikk & Jak Anderson

    GZIP output and input strange behaviour

    Nikk & Jak Anderson, Apr 26, 2004, in forum: Java
    Replies:
    2
    Views:
    1,126
    Roedy Green
    Apr 26, 2004
  2. Miroslaw Osys

    very strange gcc output

    Miroslaw Osys, Sep 7, 2003, in forum: C Programming
    Replies:
    4
    Views:
    452
    Emmanuel Delahaye
    Sep 7, 2003
  3. Harvey Twyman
    Replies:
    8
    Views:
    550
    August Derleth
    Oct 25, 2003
  4. Materialised

    Strange output problem

    Materialised, Apr 1, 2004, in forum: C Programming
    Replies:
    15
    Views:
    501
    Dan Pop
    Apr 6, 2004
  5. Paul
    Replies:
    1
    Views:
    424
Loading...

Share This Page