Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c > Newsgroup comp.lang.c.moderated

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-13-2009, 07:35 AM
hhc
Guest
 
Posts: n/a
Default recursive function

I want to create a recursive function that uses the binary search
algorithm. But every time I write the function into itself it doesn't
work. The system compiles the program but it freezes before anything
happens.If I take out the last line:<binsearch(val,array,high,low);>
it doesn't crash.
val is the value that we are searching for
array is the array that we are searching in
int binsearch(int val,int array[],int high,int low)
{
int mid;

mid=(low+high)/2;
if(val<array[mid])
{high=mid-1;}
else if(val>array[mid])
{low=mid-1;}
else
{return(val);}
binsearch(val,array,high,low);
}
I'm not sure if this is clear enough but just tell me any additional
info you need.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-15-2009, 04:19 AM
shahav
Guest
 
Posts: n/a
Default Re: recursive function

This is not seems to be c++ related..
any way - u r missing a 'return' statmement at the very end.

something like
return binsearch(val,array,high,low);

And if you would compile with any new c++ compiler he would warn about
it.
R.


On Jul 13, 10:35*am, hhc <cao...@yahoo.com> wrote:
> I want to create a recursive function that uses the binary search
> algorithm. But every time I write the function into itself it doesn't
> work. The system compiles the program but it freezes before anything
> happens.If I take out the last line:<binsearch(val,array,high,low);>
> it doesn't crash.
> val is the value that we are searching for
> array is the array that we are searching in
> int binsearch(int val,int array[],int high,int low)
> {
> * * int mid;
>
> * * mid=(low+high)/2;
> * * if(val<array[mid])
> * * {high=mid-1;}
> * * else if(val>array[mid])
> * * {low=mid-1;}
> * * else
> * * {return(val);}
> * * binsearch(val,array,high,low);}
>
> I'm not sure if this is clear enough but just tell me any additional
> info you need.
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line. *Sorry.

--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
  #3 (permalink)  
Old 07-15-2009, 04:19 AM
Dariusz Linda
Guest
 
Posts: n/a
Default Re: recursive function

First off, a good compiler should issue a warning that control
reaches end of non-void function. That means you should put
return binsearch(...) at the last line. Otherwise, if nothing
found, the function will return garbage.
Second, your calculation of mid always goes towards 0, which
effectively cuts off the high half of the array (its' never searched).
Use mid = low + (high - mid) / 2.
Third, what if the value you're looking for is not in the array?
You should return a special value (perhaps -1) to indicate this.
So the code would be:

int binsearch(int val,int array[],int high,int low)
{
int mid;

if (low > high) return -1;

mid = low + (low + high)/2;
if (val < array[mid])
{high = mid - 1;}
else if (val > array[mid])
{low = mid + 1;}
else
{return(val);}
return binsearch(val,array,high,low);
}
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
  #4 (permalink)  
Old 07-15-2009, 04:19 AM
Awhan Patnaik
Guest
 
Posts: n/a
Default Re: recursive function

On Jul 13, 12:35*pm, hhc <cao...@yahoo.com> wrote:
> I want to create a recursive function that uses the binary search
> algorithm. But every time I write the function into itself it doesn't
> work. The system compiles the program but it freezes before anything
> happens.If I take out the last line:<binsearch(val,array,high,low);>
> it doesn't crash.
> val is the value that we are searching for
> array is the array that we are searching in
> int binsearch(int val,int array[],int high,int low)
> {
> * * int mid;
>
> * * mid=(low+high)/2;
> * * if(val<array[mid])
> * * {high=mid-1;}
> * * else if(val>array[mid])
> * * {low=mid-1;}
> * * else
> * * {return(val);}
> * * binsearch(val,array,high,low);}
>
> I'm not sure if this is clear enough but just tell me any additional
> info you need.
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line. *Sorry.


i think you might be getting stuck in an infinite loop as the self
call to binsearch is not part of a conditional statement.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
  #5 (permalink)  
Old 07-15-2009, 04:19 AM
Kevin Ashley
Guest
 
Posts: n/a
Default Re: recursive function

hhc wrote:
> I want to create a recursive function that uses the binary search
> algorithm. But every time I write the function into itself it doesn't
> work. The system compiles the program but it freezes before anything
> happens.If I take out the last line:<binsearch(val,array,high,low);>
> it doesn't crash.
> val is the value that we are searching for
> array is the array that we are searching in
> int binsearch(int val,int array[],int high,int low)
> {
> int mid;
>
> mid=(low+high)/2;
> if(val<array[mid])
> {high=mid-1;}
> else if(val>array[mid])
> {low=mid-1;}
> else
> {return(val);}
> binsearch(val,array,high,low);
> }
> I'm not sure if this is clear enough but just tell me any additional
> info you need.


This looks and sounds like homework, so my answer is intended to help
you find the answer for yourself.

If a recursive function 'freezes', it's probably failing to end the
recursion. Wait long enough and the freeze will probably stop, along with
a lot of (probably unhelpful) error messages.

Why not add something to help you understand what's going on ? If you
like using an interactive debugger, put a breakpoint on the entry
to that routine and see what's happening to its arguments.

Or if you don't like/have a debugger, put a call to printf at the beginning
of binsearch to tell you its arguments, or at least what 'high' and 'low' are.

Or walk through the code with these values:

Imagine that high=3 and low=1 on the first call to binsearch,
and that the value you seek is at array[3]. What value does mid
take ? What values are high and low set to before the first recursive
call to binsearch, if any ? And the next ? And the next ?

That way you may find enlightenment.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
  #6 (permalink)  
Old 07-15-2009, 04:19 AM
Wolfnoliir
Guest
 
Posts: n/a
Default Re: recursive function

hhc wrote:
> I want to create a recursive function that uses the binary search
> algorithm. But every time I write the function into itself it doesn't
> work. The system compiles the program but it freezes before anything
> happens.If I take out the last line:<binsearch(val,array,high,low);>
> it doesn't crash.
> val is the value that we are searching for
> array is the array that we are searching in
> int binsearch(int val,int array[],int high,int low)
> {
> int mid;
>
> mid=(low+high)/2;
> if(val<array[mid])
> {high=mid-1;}
> else if(val>array[mid])
> {low=mid-1;}
> else
> {return(val);}
> binsearch(val,array,high,low);
> }
> I'm not sure if this is clear enough but just tell me any additional
> info you need.


I suppose it goes into an infinite loop and when the stack overflows, it
crashes. What error do you get when it crashes (segfault, ..?)
You should try using a debugger like gdb to see exactly when it crashes.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
  #7 (permalink)  
Old 07-15-2009, 04:20 AM
Barry Schwarz
Guest
 
Posts: n/a
Default Re: recursive function

On Mon, 13 Jul 2009 02:35:19 -0500 (CDT), hhc <caohhc@yahoo.com>
wrote:

>I want to create a recursive function that uses the binary search
>algorithm. But every time I write the function into itself it doesn't
>work. The system compiles the program but it freezes before anything
>happens.If I take out the last line:<binsearch(val,array,high,low);>
>it doesn't crash.
>val is the value that we are searching for
>array is the array that we are searching in
>int binsearch(int val,int array[],int high,int low)
>{
> int mid;
>
> mid=(low+high)/2;
> if(val<array[mid])
> {high=mid-1;}


A consistent and visually obvious indenting style can save you a lot
of heartache when you write larger functions.

> else if(val>array[mid])
> {low=mid-1;}
> else
> {return(val);}


return is a statement, not a function. The parentheses are
unnecessary.

> binsearch(val,array,high,low);
>}
>I'm not sure if this is clear enough but just tell me any additional
>info you need.


Take a piece of paper and play computer. Let array = {1,2,3} and call
the function once with val = 2 and once with val = 1. Note what
happens when you call binsearch recursively in the second case. Where
does the second call return to?

I don't think you want to return val (since you know what it is in the
calling function). You probably want to return mid.

--
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
  #8 (permalink)  
Old 07-16-2009, 01:06 AM
Barry Schwarz
Guest
 
Posts: n/a
Default Re: recursive function

On Tue, 14 Jul 2009 23:19:29 -0500 (CDT), shahav <shahav@gmail.com>
wrote:

>This is not seems to be c++ related..
>any way - u r missing a 'return' statmement at the very end.
>
>something like
> return binsearch(val,array,high,low);
>
>And if you would compile with any new c++ compiler he would warn about
>it.


Since this is a C group, your first comment is irrelevant and your
last inappropriate. But the middle one does address the problem.

--
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
  #9 (permalink)  
Old 07-16-2009, 01:07 AM
James Kuyper
Guest
 
Posts: n/a
Default Re: recursive function

Hans-Bernhard Bröker wrote:
> hhc wrote:
>
>> The system compiles

>
> If so, either your compiler is totally broken, or the code you showed
> wasn't being compiled. I know that because there's a critical syntax
> error in it that no compiler is allowed let pass.


Which error is that? I couldn't find any such such error. I cut and
pasted the code into a file and compiled it with the command

gcc -std=c99 -pedantic -Wall -Wpointer-arith -Wcast-align \
-Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -c binsearch.c

and gcc (4.1.2) agreed with me - it didn't generate any error messages,
just a few warning messages. The most serious was

warning: control reaches end of non-void function

That's not a syntax error, that's undefined behavior, but only if the
calling function tries to use the non-existent return value (6.9.1p12).
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
  #10 (permalink)  
Old 07-16-2009, 01:07 AM
REH
Guest
 
Posts: n/a
Default Re: recursive function

On Jul 15, 12:19*am, shahav <sha...@gmail.com> wrote:
> This is not seems to be c++ related..
> any way *- u r missing a 'return' statmement at the very end.
>


How is this not relevant to C++?

REH
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
  #11 (permalink)  
Old 07-21-2009, 04:56 PM
Dag-Erling Smørgrav
Guest
 
Posts: n/a
Default Re: recursive function

Awhan Patnaik <awimagic@gmail.com> writes:
> i think you might be getting stuck in an infinite loop as the self
> call to binsearch is not part of a conditional statement.


Actually, it is. That part is correct. The parts that aren't have
already been explained by others.

DES
--
Dag-Erling Smørgrav - des@des.no
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Reply With Quote
 
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 09:19 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.