Thread: recursion question

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    This question concerns a recursive function i have to write. its basically has to take a number in that will represent the the number of times you flip a coin. I need a way to print out all possible solutions.

    So for example say the user puts in 1, the output would be ' HT '.
    input is 2 so it prints hh tt th ht etc.

    What it does is takes in a number from the user and outputs the possible

    so far ive come up with
    Code:
    int coin(int x)
    {
    
    if(x==0)
    return;
    
    else
    {
    return coin(x-1);
    
    }
    QUESTION NOW IS:

    How would i print out the possible solutions. I was thinking something like strcat adding the letters but im not really sure how to proceed from where i am?
    Last edited by Salem; 10-05-2008 at 11:01 PM. Reason: Added code tags - learn to use them yourself

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would suggest with, say, two coin flips and three coin flips. Look for patterns -- especially ways to use the two-coin-flip answers inside the three-coin-flip problem (since that's what recursion does, go down one level).

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Quote Originally Posted by tabstop View Post
    I would suggest with, say, two coin flips and three coin flips. Look for patterns -- especially ways to use the two-coin-flip answers inside the three-coin-flip problem (since that's what recursion does, go down one level).
    ok. well correct me if i am wrong as i am just a begining comp science. student. I was informed that i had to do a double recursive call for this to even work period. I dont know if it makes sense at all. On the other hand how would i even go about coding something like this.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sumdude View Post
    I was informed that i had to do a double recursive call for this to even work period.
    If you mean your function will call function(n-1) twice, then yes. Most coins do have two sides.
    Quote Originally Posted by sumdude View Post
    I dont know if it makes sense at all. On the other hand how would i even go about coding something like this.
    That's why you start with paper. Write out the possibilities for two coins and three coins and look at the patterns.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    ok will try that out. appreciate the help

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    This is what i came up with but it only prints the h's. any help?

    int flip(int x, char string[])
    {


    if(x==0)
    { printf("%s\n", string);
    return 0; }

    if(x>0)

    {


    return flip(x-1,strcat(string,"H"));
    return flip(x-1,strcat(string,"T"));


    }


    }
    Can someone tell me where im going wrong please/

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Once you hit return, that's it. There's no way to get to the line "return flip(x-1, strcat(string, "T"));" in this function.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Quote Originally Posted by tabstop View Post
    Once you hit return, that's it. There's no way to get to the line "return flip(x-1, strcat(string, "T"));" in this function.
    so how can i resolve this problem?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't return.

    Just call the function.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    ok here is what im getting

    Please enter number of flips
    2
    HTHH
    HTHHT
    HTHHTTH
    HTHHTTHT

    why is this happen. Also i did declare in main ..... char sting[50]="HT"

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sumdude View Post
    char sting[50]="HT"
    So don't do that. string must be empty when this function is first called.

    And as you can see, things are not getting removed when you go from the H call to the T call. I'm guessing you want to make two local copies of the string (one to be passed into the H call, one to be passed into the T call) so that they don't interfere with each other in this way.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    still cant get this ting to work. now its begining to print out some wierd characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iteration and recursion question
    By Stonehambey in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2008, 06:16 PM
  2. simple recursion question
    By salvadoravi in forum C Programming
    Replies: 4
    Last Post: 12-30-2007, 07:53 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Recursion vs multiple functions
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2002, 08:52 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM