Recursion

This is a discussion on Recursion within the C++ Programming forums, part of the General Programming Boards category; I am trying to figure out this problem consider this recurrence relation f(1)=2 f(2)=2 f(n)=2*f(n-1)+f(-2) for n>2 write a recursive ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    80

    Recursion

    I am trying to figure out this problem

    consider this recurrence relation

    f(1)=2
    f(2)=2

    f(n)=2*f(n-1)+f(-2) for n>2

    write a recursive function to compute f.

    I have some code that I have been working on. My question is.

    How do u get the function to return a value that it computed back to itself to be used again .

    When i run this for f(2) and f(1)it comes out right, but for anything else its wrong ( or at least i think.)

    can anyone tell mat f(3) and f(4) are so i know what to look for? Also, give me a hint as to what I am doing wrong in the code?

    Code:
    #include<iostream>
    using namespace std;
    
    
    
    int computef(int n)
    
    {
        int answer;
           
        if ((n=2)||(n=1))
        
        return 2;
       
         else
            
        answer= 2*computef(n-1)+computef(n-2);
        
        cout << answer;
        
         
         
    
    }
    
        int main()
        {
         int x;
         
         cout << "Please input the number for calculation  " ;
         cin >> x;
            
           cout << computef(x);
            
                   
            
            system("pause");
            return 0;
            
            }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    How do u get the function to return a value that it computed back to itself to be used again .
    Just return the value.

    can anyone tell mat f(3) and f(4) are so i know what to look for?
    I presume that you have a typographical error and thus f(-2) is actually f(n-2).
    f(3) = 2 * f(3-1) + f(3-2) = 2 * f(2) + f(1) = 2 * 2 + 2 = 6

    From the above calculation, do you understand how the recurrence relation works now?

    EDIT:
    Oh wait, the real problem (other than failing to return a value) is that you wrote ((n=2)||(n=1)) instead of ((n==2)||(n==1)).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    Holy smokes Batman!!!

    That worked. So when do u use something like n=2 vs n==2
    also, when u use "return" it return the value to the calling function to use it again if the conditions are not met?

    revised code.

    Code:
    #include<iostream>
    using namespace std;
    
    
    
    int computef(int n)
    
    {
        int answer;
           
        if ((n==2)||(n==1))
        
        return 2;
       
         else
            
        answer= 2*computef(n-1)+computef(n-2);
        
        return answer;
        
        cout << answer;
         
         
    
    }
    
        int main()
        {
         int x;
         
         cout << "Please input the number for calculation  " ;
         cin >> x;
            
           cout << computef(x);
            
                   
            
            system("pause");
            return 0;
            
            }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    So when do u use something like n=2 vs n==2
    The former is used in assignment (or as part of the syntax for initialisation), the latter is used for comparing for equality.

    when u use "return" it return the value to the calling function to use it again if the conditions are not met?
    Yes. Of course, whether some conditions are met or otherwise does not matter: from the point of view of the function, you are just returning a value.

    revised code.
    I suggest that you indent your code more consistently. Also remove the cout << answer; since it is not needed and unreachable anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    Thank you so much foe help. U made my night, i was working on this one problem for like 3 and a half hours

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, 08:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 09:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 08:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 01:29 PM

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