Recursion problem...help

This is a discussion on Recursion problem...help within the C Programming forums, part of the General Programming Boards category; Hi I have this problem.. if n<=7, seq(n)=10 if n>7, seq(n)= 3*n+seq(n-1)-21 I must use recursion, iv'e made something like ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    53

    Recursion problem...help

    Hi

    I have this problem..

    if n<=7, seq(n)=10

    if n>7, seq(n)= 3*n+seq(n-1)-21

    I must use recursion, iv'e made something like that but i have always segmentation fault.


    Code:
    int seq(int n)
    {
        if(n<7) //under 7 i must use seq(10)
        {
            return(3*n+seq(n-1)-21);
        }
        else
        {
            return(3*n+seq(n-1)-21);
        }
        return 0;
    }


    What's wrong with this??

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Your if- and else-block are identical so it doesn't matter what value n has and your recursion is infinite.
    I guess you should break out of the recursion when n reaches 7, aren't you?

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Hi Andreas;

    No, when n > 7 i must use this formula (3*n+seq(n-1)-21);

    if n <=7 (seq(n)=10)

    I don't understand this like this.

    Code:
     
    Code:
    int calculaSequencia(int n)
    {
        if(n<7)
        {
            return 10;
        }
        else
        {
            return(3*n+seq(n-1)-21);
        }
        return 0;
    }
    The exercise say:

    Implement with recursion, the function CalculaSequencia to calculate the n's of the sequency.


    Last edited by alphasil; 07-11-2012 at 06:04 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    But was should the function return as a whole?
    For example what's the solution for n=7, n=8, n=9?

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Well, for 8, the program gives me 13, 9 gives me 19

    Iv'e made some changes, but my doubt is:

    if n<=7, seq(n)=10

    Code:
    int calculaSequencia(int n)
    {
        if(n<7)
        {
            return 10;
        }
        else
        {
            return((3*n-21)+calculaSequencia(n-1));
        }
        return 0;
    }
    It's hard to believe tha the solution is return 10, it's too easy.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    All problems are easy as soon as you understand them :-)

    Every recursion needs an end point where it returns a fixed value to its caller (one level above in the call stack).

    And you don't need the return 0 at the end of the function (the function will never reach it).

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    So unless the return 0, the function is correct?

    Thanks

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    If I didn't misinterpret your problem description, yes.
    Do you get the expected results?

    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Hi
    Andreas;;

    I dont' have any result given, just the definition to use in the function.

    I think it should be ok then.


    Thanks

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    What value do you get when you pass in n=7? Look closely at your comparison.

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    I forgot that is (if <=7), i just put (if n<7).

    My mistake, sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion problem
    By deltanosix in forum C Programming
    Replies: 15
    Last Post: 02-02-2012, 01:13 PM
  2. Problem in recursion
    By annie in forum C Programming
    Replies: 21
    Last Post: 12-16-2011, 08:37 AM
  3. Recursion problem
    By ramayana in forum C Programming
    Replies: 3
    Last Post: 11-13-2006, 11:54 PM
  4. A Problem with recursion
    By Adamkromm in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2005, 09:35 PM
  5. recursion problem!!
    By newbie_grg in forum C++ Programming
    Replies: 5
    Last Post: 07-30-2002, 02:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21