1. #1
    The Patient Buckeye's Avatar
    10+ Year Old Account
    Join Date
    Dec 2010
    Location
    Columbus, OH.
    Posts
    206

    Help with C++ Recursion

    Okay so for an assignment we were assigned to implement a string operation using recursion. The operation is Swap_Substring(t1(text), pos(int), len(int), t2(text)), and what it's meant to do is start at pos of t1 and take len amount of characters and swap them with t2. So it swaps strings.

    Code:
    if(t2.Length()>0){
       object Character ch, ch2;
       t2.Remove(0, ch);
       if(len>0){
          t1.Remove(pos, ch2);
       }
       Swap_Substring(t1, pos, len-1, t2); <-----This is the recursive call, the programs name is Swap_Substring
       if(len>0){
          t2.Add(t2.Length()-1, ch2);
       }
       t1.Add(pos, ch);
    }
    else{
       if(t1.Length>0){
          object Character ch3;
          if(len>0){
             t1.Remove(0, ch3);
             Swap_Substring(t1, pos, len-1, t2);
             t2.Add(0, ch3);
          }
       }
    }
    My problem comes when I try to do the following example:

    t1="This is a non-empty string"
    pos=0
    len=26
    t2="So is this one"

    It should output:

    t1="So is this one"
    pos=0
    len=26
    t2="This is a non-empty string"

    Everything is as it should be except, t2="empty strin-non a si sihTg" when I run it. I know the problem lies with my conditional statements, because the program will keep removing from t2 and when it empty, the call will go to the else statement, and I think thats why I get a jumbled output. If anyone can help it would be very appreciate.
    Last edited by chaud; 2012-02-24 at 02:43 AM.

  2. #2
    Is there any code behind Swap_Substring() ?
    And in what programm u doing this ? or is php ?
    sorry if the questions are silly my english is not too good in a ceratin way.
    Last edited by yannick0007; 2012-02-24 at 12:03 AM.

  3. #3
    OT: I thought I was pretty good with computers until I read that. Now I feel like a retard -_-

  4. #4
    Quote Originally Posted by yannick0007 View Post
    Is there any code behind Swap_Substring() ?
    yes, it's what he has there.
    gimme a minute i'll look at it.

    I haven't coded for awhile but this is my take away:
    when it swaps the chars from t1 to t2 it starts at the the length of t2-1 and copies itself backwards until it is at t1 length and then ends and displays the text. So check to see when you are calling the recursive method and what the length is.

    Also recursion looks much more magical than what you have lol
    Last edited by bals; 2012-02-24 at 12:24 AM.

  5. #5
    Don't u have to put t2 = t2.Remove(0, ch);
    seeing as it returns a string value
    (ofc the same of the rest).

  6. #6
    What an awkward way to introduce students to recursion.
    What dialect of C++ is this? I'm asking since the declaration of ch, ch2 and ch3 isn't quite obvious to me. Is "object" a macro?
    With "program name" I think you're referring to your function name, aren't you? Obviously you can only have one "program name" in C++. And that's "main".
    Can you give the whole function signature of Swap_Substring()?

    Of what type is t1 and t2? And how does the Remove and Add methods work? Does t2.Remove(0, ch); remove the first character of t2 and puts it into ch?

    Lots of questions, but right now your code throws up more questions than it answers.

  7. #7
    The Patient
    10+ Year Old Account
    Join Date
    Jul 2009
    Location
    Swansea, United Kingdom
    Posts
    258
    You really need to post all of the code, including the Add and Remove functions (which, assuming you're using std::string, you've written, as I'm fairly sure std::string has no such member functions). If you do so, I'll step through the code tomorrow properly.

    A general piece of recursion advice, however; be careful with tail recursion. Remember that while the stack grows recursively it will return after the recursive call to the same point and continue, think Russian dolls. I'd say that this has been the downfall of about 90% of undergraduates attempting to write recursive functions for the first time.

  8. #8
    Deleted
    Too late for this -.-

    Edit:
    shouldn't
    t2.Add(t2.Length()-1, ch2);
    be
    t2.Add(0, ch2);
    ?
    Last edited by mmoc68f51d2d55; 2012-02-24 at 01:30 AM.

  9. #9
    The Patient Buckeye's Avatar
    10+ Year Old Account
    Join Date
    Dec 2010
    Location
    Columbus, OH.
    Posts
    206
    Sorry this code is very awkward because it was designed by our university in the 80's. It's a modification of C++ and no I didn't write the add/remove functions, they are standard calls. This is the entire code for the program body of Swap_Substring.
    The add/remove work with (int, char), where the integer is position in the string of text, and char is the variable that you want to use to store the character at that position. Remove's integer can only be 0<=x<|string|, while Add's integer may be 0<=x<=|string| because it allows for a character to be add at the very end.

  10. #10
    The Patient
    10+ Year Old Account
    Join Date
    Jul 2009
    Location
    Swansea, United Kingdom
    Posts
    258
    Quote Originally Posted by Buckeye View Post
    Sorry this code is very awkward because it was designed by our university in the 80's. It's a modification of C++ and no I didn't write the add/remove functions, they are standard calls. This is the entire code for the program body of Swap_Substring.
    The add/remove work with (int, char), where the integer is position in the string of text, and char is the variable that you want to use to store the character at that position. Remove's integer can only be 0<=x<|string|, while Add's integer may be 0<=x<=|string| because it allows for a character to be add at the very end.
    My point is that neither Add nor Remove is within stl, thus it's a bit hard to work out exactly what they do. I discovered 3 instances of what would have been fairly horrific stack overflows, but are apparently handled within the mysterious Add and Remove functions. To cite an example, whenever t1 is smaller than t2, this code:

    if(len>0){
    t1.Remove(pos, ch2);

    will overflow, so who even knows what's going on there. Additionally,

    t1.Add(pos, ch);

    will overflow at some stage because the string isn't long enough. There's something that I can only describe as dubious happening in both of those functions. I reworked it to both take from and add to the front of the string, if it's part of the brief that you work from the back, I would strongly suggest using fixed size character arrays, as adding to what is supposedly the 14th character of an empty std::string makes no sense.

    I generated a vague facsimile of Add and Remove (with std::string) and took the two ints out of the Swap_Substring function. This is "proper" c++, all within the stl.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void Swap_Substring(string*, string*);
    char Remove(string*, unsigned);
    void Add(string, unsigned, char);
    
    int main(int argc, char** argv)
    {
    	string t1 = "So is this one";
    	string t2 = "This is a non-empty string";
    	Swap_Substring(&t1, &t2);
    	
    	cout << t1.c_str() << endl << t2.c_str() << endl;
    	getchar();
    }
    
    char Remove(string *str, unsigned pos)
    {
    	char ch;
    	string t = *str;
    	ch = t[pos];
    	str->erase(str->begin()+pos, str->begin()+pos+1);
    	return ch;
    }
    
    void Add(string *str, unsigned pos, char ch)
    {
    	str->insert(pos, 1, ch);
    }
    
    void Swap_Substring(string *t1, string *t2)
    {
    	if(t1->length() > 0 && t2->length() > 0)
    	{
    		char ch1, ch2;
    		ch1 = Remove(t1, 0);
    		ch2 = Remove(t2, 0);
    		Swap_Substring(t1, t2);
    		Add(t1, 0, ch2);
    		Add(t2, 0, ch1);
    	}
    	else if(t1->length() > 0)
    	{
    		char ch1 = Remove(t1, 0);
    		Swap_Substring(t1, t2);
    		Add(t2, 0, ch1);
    	}
    	else if(t2->length() > 0)
    	{
    		char ch2 = Remove(t2, 0);
    		Swap_Substring(t2, t1);
    		Add(t1, 0, ch2);
    	}
    }
    Far from perfect and not exactly "good" code, did it fairly quickly and had to do some slightly strange things to try to ensure that the Add and Remove prototypes matched as closely as possible. Should give you something to go on at least.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •