Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strange output

Reply
Thread Tools

Strange output

 
 
junw2000@gmail.com
Guest
Posts: n/a
 
      08-04-2006
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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      08-04-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
junw2000@gmail.com
Guest
Posts: n/a
 
      08-04-2006

mlimber wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      08-04-2006

(E-Mail Removed) wrote:
> mlimber wrote:
> > (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange problem - page output contains output from another request Paul ASP .Net 1 04-10-2007 03:41 PM
AVG Email Scanner activating at strange times with strange IP addresses dennispublic@hotmail.com Computer Support 1 08-26-2006 04:27 AM
GZIP output and input strange behaviour Nikk & Jak Anderson Java 2 04-26-2004 08:11 PM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM
very strange gcc output Miroslaw Osys C Programming 4 09-07-2003 08:03 PM



Advertisments