Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with recursion

Reply
Thread Tools

Problem with recursion

 
 
Gaijinco
Guest
Posts: n/a
 
      03-26-2006
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);
}
}

 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      03-26-2006

Gaijinco wrote:
> 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)


Better:

if(pos >= size)
return 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);


This probably should be:

inverse(s, pos + 1, size - 2);

> }
> }


 
Reply With Quote
 
 
 
 
davidrubin@warpmail.net
Guest
Posts: n/a
 
      03-27-2006

Gaijinco wrote:
> 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)


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?

 
Reply With Quote
 
Gaijinco
Guest
Posts: n/a
 
      03-27-2006
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!

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      03-27-2006

"Gaijinco" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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!
>


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


 
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
va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com C Programming 8 04-26-2012 08:37 PM
Simple recursion problem. Need help. Anakin Java 14 04-13-2005 03:10 AM
Re: Recursion problem - Graph theory - Algorithm needed Allan W C++ 4 01-22-2004 02:42 PM
Recursion problem - Graph theory - Algorithm needed JimC C++ 3 01-17-2004 12:43 PM
Recursion-related problem kelvSYC Java 2 11-18-2003 03:32 PM



Advertisments