Strange output

J

junw2000

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
 
M

mlimber

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
 
J

junw2000

mlimber said:
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
 
M

mlimber

mlimber said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top