Recursion Square - HELP!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JWest46088
    New Member
    • Sep 2006
    • 74

    Recursion Square - HELP!

    Hello everybody. I'm having a little trouble using recursion.

    I need to accept input from the user and display the input in a square. It has to be done recursively. I would be able to do it without using recursion (although it may not be the best way and sloppy), but I cannot for the life of me figure out how to do it using recursion.

    Here is how it's supposed to work:

    Enter characters: lucky //ask for user input

    Display user input in square:

    y y y y y y y y y
    y k k k k k k k y
    y k c c c c c k y
    y k c u u u c k y
    y k c u l u c k y
    y k c u u u c k y
    y k c c c c c k y
    y k k k k k k k y
    y y y y y y y y y


    Here's my code (I have a lot of cout statements for testing. Please disregard those.):

    Code:
    #include <iostream>
    using namespace std;
    
    void input(char charSq[]);
    void draw(char charSq[], int size);
    
    int main()
    {
            int i;
            char charSq[5];
            char charSqCopy[5];
    
    
            cout << "\nThis program accepts a sequence of characters (up to ";
            cout << "5) consisting of one word and then produces a square using ";
            cout << "those characters with the first character in \nthe middle ";
            cout << "of the square and so on until the last character surrounds ";
            cout << "the \noutside of the square.\n\n";
    
            input(charSq);
    
            int size = strlen(charSq);
    
    //      for(i = 0; i < size + (size - 1); i++)
    //      {
    //              if(
    //              char charSq[size];
    
    //      for(i = 0; i < size; i++)
    //              charSqCopy[i] = charSq[i];
    
            cout << "\ncharSq " << charSq << "." << endl;
            cout << "charSqCopy " << charSqCopy << "." << endl;
            
            for(i = 0; i < size; i++)
            {
                    cout << "charSq " << charSq << endl;
                    cout << "charSqCopy " << charSqCopy << endl;
            }
    
            cout << charSq[0] << charSq[2] << endl;
            cout << charSqCopy[size] << endl;
    
            cout << strlen(charSq) << endl;
    
            cout << "test" << size << endl;
    
            draw(charSq, size);
    
            for(i = 0; i < size; i++)
            {
                    charSqCopy[size-size] = charSq[size];
                    charSqCopy[size-(size-1)] = charSq[size-1];
                    charSqCopy[size-(size-2)] = charSq[size-1];
                    //not finished
            }
            return (0);
    }
    
    void input(char charSq[])
    {
            cout << "Enter a one word sequence of up to 5 characters: ";
            cin >> charSq;
    
            if(strlen(charSq) > 5)
            {
                    cout << "\nYour sequence exceeded the allowed number of characters. ";
                    cout << "Try again!\n\n";
                    input(charSq);   
            }
            else
                    return;
    }
                    
    void draw(char charSq[], int size)
    {
            int i, j;
            int count = 0;
    
    //      for(i = 0; i < size; i++)
    //              charSqCopy[i] = charSq[i];
            
    //      cout << "size = " << size << endl;
    //      cout << "count = " << count << endl;
             
            for(i = 0; i < size + (size - 1); i++)
                    cout << charSq[size-1] << " ";
                    
            if(count == 0)
                    cout << endl << charSq[size-1] << " ";
                    
            for(j = 0; j < size + (size - 1); j++)
            {       
                    if(size == (size - j))
                    {
    //                      count++;
            
    //                      cout << endl << charSq[size-count] << " ";
                            cout << charSq[size-j] << " ";
    
                            charSq[size-1] = charSq[size-2];
                            size = strlen(charSq) - 1;
    //                      count++;
             
                            draw(charSq, size);   
                    }
                    else
                            return;
            }
                    
            cout << endl;
                    
            cout << endl;
            cout << strlen(charSq) + (strlen(charSq) - 1) << endl;
            cout << size << endl;   
            cout << "Size:" << charSq[size-2] << endl;
    }

    Executed code:

    This program accepts a sequence of characters (up to 5) consisting of one word and then produces a square using those characters with the first character in
    the middle of the square and so on until the last character surrounds the
    outside of the square.

    Enter a one word sequence of up to 5 characters: hello

    charSq hello.
    charSqCopy Ðh8ê¿ello.
    charSq hello
    charSqCopy Ðh8ê¿ello
    charSq hello
    charSqCopy Ðh8ê¿ello
    charSq hello
    charSqCopy Ðh8ê¿ello
    charSq hello
    charSqCopy Ðh8ê¿ello
    charSq hello
    charSqCopy Ðh8ê¿ello
    hl
    ¦
    5
    test5
    o o o o o o o o o
    o l l l l l l l
    l l l l l l l l l
    l l l l l l l l l
    l l l l l l l l l
    l l l l l l l l l
    l l l l l l l l l
    l l l l l l l l l
    l l l l l l l l l


    The l just keeps looping infinitely...
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    What's with the forum cross post?

    Comment

    • JWest46088
      New Member
      • Sep 2006
      • 74

      #3
      Originally posted by oler1s
      What's with the forum cross post?
      To get other opinions on a different forum. Can I not do that?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        I would think about the base case, and how you will reduce the problem to the base case at any other case.

        For this example, the base case seems to be the middle row of the square, where you finally print the first letter of the word, in the middle of the square.

        The other base cases seem to be thus: You print the last N characters of the word once, then repeat the Nth-to-last letter until you then print the N+1th letter, then the N+2th letter, etc...

        So each row seems like it will require a loop to print the proper characters. Then you have the basic algorithm:

        Code:
        if (this isn't the base case) {
           print_row(theString, theLetterNum);
           draw(theString, theLetterNum-1);
           print_row(theString, theLetterNum);
        } else {
           print_row(theString, theLetterNum);
        }

        Comment

        Working...