Recursion programme

This is a discussion on Recursion programme within the C++ Programming forums, part of the General Programming Boards category; Is anyone able to code this? ive done a sample already look at it: 1 1 2 0 3 -1 ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    Recursion programme

    Is anyone able to code this?
    ive done a sample already look at it:

    1 1 2 0 3 -1 4 -2 5....

    such that the 3 terms are 1, 1 and 2 and each term thereafter is defined recusively as the sum of the first 2 items minus the third, that is:

    seq(n) = seq(n-3)+seq(n-2)-seq(n-1)

    the programme accepts the number into the vairable n
    implement the recursive function that computes the above series, call the function recursiveSeq().

    The main() program should look like this:
    cout<< "Enter n = ";
    cin>>n;
    cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;

    Code:
    #include <iostream>
    : using namespace std;
    :
    :
    :
    : int recursiveSeq(const int n)
    : {
    : if (1==n || 2==n)
    : {
    : return 1;
    : }
    : else if (3==n)
    : {
    : return 2;
    : }
    : else
    : {
    : return recursiveSeq(n-3) + recursiveSeq(n-2) - recursiveSeq(n-1);
    : }
    : }
    :
    : using std::cout;
    : using std::cin;
    : int main()
    : {
    : int n;
    :
    : cout<< "Enter n = ";
    : cin>>n;
     cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;
     }
    But it only works for up to some certain of numbers??i dont know why??and how do i do it if i want to input negative numbers??

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by kenneth_888 View Post
    But it only works for up to some certain of numbers??
    Could you define "doesn't work"? If it appears to be hanging, it could be because you specified a large sequence for it to calculation and it's just taking that long.

    Quote Originally Posted by kenneth_888 View Post
    and how do i do it if i want to input negative numbers??
    The important thing is to get your base cases right. You have the program stop calculating a particular entry if n is 1, 2, or 3.

    If someone enters -1, then what? When do you want them to stop? You have to figure out when the limit is there to stop them.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    well i would to be able to input to 10000

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    That's not realistic. Recursive function calls take time, and what's more is that when you call the same function more than once, you're doing it exponentially.

    One call with n set to 10,000 = 1 original call + 3 more calls + 9 more calls + 27 more calls + 81 more calls.... etc. etc.. You'll be calling this function many, many times.

    The program will run for awhile. If you really need to calculate it faster, then you need to find a faster method of calculating the sequence.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    21

    Smile

    ohh i c thanxs, what about with negative numbers??its not possible right
    i guess theres nothing wrong with my programme unless i input a big integer.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What about negative numbers? What would the formula be for them?

    If I enter -1, what do you want the answer to be?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  3. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 09:05 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM

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