Strange output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junw2000@gmail.com

    #1

    Strange output

    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<<"ret urn 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

  • mlimber

    #2
    Re: Strange output


    junw2000@gmail. com 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<<"ret urn 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

    Comment

    • junw2000@gmail.com

      #3
      Re: Strange output


      mlimber wrote:
      junw2000@gmail. com 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<<"ret urn 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

      Comment

      • mlimber

        #4
        Re: Strange output


        junw2000@gmail. com wrote:
        mlimber wrote:
        junw2000@gmail. com 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<<"ret urn 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

        Comment

        Working...