Connecting Tech Pros Worldwide Help | Site Map

Problem with recursion

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 26th, 2006, 03:45 PM
Gaijinco
Guest
 
Posts: n/a
Default Problem with recursion

I'm trying to do a function that returns a string which is a string the
characters in inverse order of the one given as a parameter. It
compiles but it crashes! Can anyone help me?

string inverse(string s, int pos, int size)
{
if(pos==size-1)
return s;
else
{
int idx = s.size()-1;
char character = s.at(idx);
s.erase(idx);
s.insert(s.begin()+pos,character);
inverse(s,pos+1,size);
}
}


  #2  
Old March 26th, 2006, 04:05 PM
Maxim Yegorushkin
Guest
 
Posts: n/a
Default Re: Problem with recursion


Gaijinco wrote:[color=blue]
> I'm trying to do a function that returns a string which is a string the
> characters in inverse order of the one given as a parameter. It
> compiles but it crashes! Can anyone help me?
>
> string inverse(string s, int pos, int size)
> {
> if(pos==size-1)[/color]

Better:

if(pos >= size)
return s;
[color=blue]
> return s;
> else
> {
> int idx = s.size()-1;
> char character = s.at(idx);
> s.erase(idx);
> s.insert(s.begin()+pos,character);
> inverse(s,pos+1,size);[/color]

This probably should be:

inverse(s, pos + 1, size - 2);
[color=blue]
> }
> }[/color]

  #3  
Old March 27th, 2006, 04:45 AM
davidrubin@warpmail.net
Guest
 
Posts: n/a
Default Re: Problem with recursion


Gaijinco wrote:[color=blue]
> I'm trying to do a function that returns a string which is a string the
> characters in inverse order of the one given as a parameter. It
> compiles but it crashes! Can anyone help me?
>
> string inverse(string s, int pos, int size)[/color]

Your implementation make far too many copies, and the contract is
unclear. If I had to guess, I'd say it returns a string which is a copy
of the characters s[pos]..s[pos+size] reversed. Is this what you mean?

  #4  
Old March 27th, 2006, 08:05 PM
Gaijinco
Guest
 
Posts: n/a
Default Re: Problem with recursion

Yeah, if you input carlos it would output solrac but there is something
quite odd, this works fine:

void inverse(string s, int pos, int size)
{
if(pos==size-1)
{
cout << s;
return s;
}
else
{
int idx = s.size()-1;
char character = s.at(idx);
s.erase(idx);
s.insert(s.begin()+pos,character);
inverse(s,pos+1,size);
}
}

But if I try to return the string, then the program crashes. I'm not
sure why is that!

  #5  
Old March 27th, 2006, 11:05 PM
Howard
Guest
 
Posts: n/a
Default Re: Problem with recursion


"Gaijinco" <gaijinco@gmail.com> wrote in message
news:1143492490.049722.158870@z34g2000cwc.googlegr oups.com...[color=blue]
> Yeah, if you input carlos it would output solrac but there is something
> quite odd, this works fine:
>
> void inverse(string s, int pos, int size)
> {
> if(pos==size-1)
> {
> cout << s;
> return s;
> }
> else
> {
> int idx = s.size()-1;
> char character = s.at(idx);
> s.erase(idx);
> s.insert(s.begin()+pos,character);
> inverse(s,pos+1,size);
> }
> }
>
> But if I try to return the string, then the program crashes. I'm not
> sure why is that!
>[/color]

You defined the function as void, but then have a "return s" statement in
the first part.

If it is supposed to return a string, then define it to return a string.
And then you also need a return statement for the else condition. (Probably
"return inverse(s,pos+1,size):"? I haven't tested the thought.)

If it is not supposed to return a string, then it should be operating on s
directly, and never returning a value. In that case, make s a string&
instead, and change "return s;" to just "return;".

-Howard


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.