Thread: recursive math funcction

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    7

    Question recursive math funcction

    hi, i am writing a function to return the value of a function A(n), where:
    A(n) = 2A(n-1) - 3A(n-2) for n>1

    when n = 0, A = a and when n = 1, A = 2.

    I've came up with the function below, but I am getting different results to when I am work out the answers by hand.

    Code:
    int A(int n)
    {	
    
    	if(n==0||n==1)
    	{
    		n +=1;
    	}
    	
    	else
    	{
    		n = (2*A(n-1)) - (3*A(n-2));
    	}
    
    	return n;
    }
    I am not sure where I am going wrong and would appreciate a push in the right direction.
    The answers I am getting from the program are -4, -11, -10 and 13 for n = 3 to 6 and my answers from working them out are-4, -29, -192 and -419. I think it has something to do with multiplying the function, and its probably really simple, but I've spent a good six hours trying and testing code and I've ran out of ideas.
    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >when n = 0, A = a
    What do you mean A = a? what's "a" ?
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    Sorry, its meant to be n = 0, A = 1.
    Thanks for the help Salem, I shall try it. I did try it when I started, but the program kept crashing, but I will try again and let you know.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Here's how i would have made it:
    Code:
    int A( int n )
    {
    	if( n==0 || n==1 )
    		return n;
    	else
    		return( 2*A(n-1) - 3*A(n-2) );
    }
    Loading.....
    ( Trying to be a good C Programmer )

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by whatman
    The answers I am getting from the program are -4, -11, -10 and 13 for n = 3 to 6 and my answers from working them out are-4, -29, -192 and -419.
    Are you sure that the answers you are working out are correct?
    Code:
    A(0) = 1, A(1) = 2, A(n) = 2A(n-1) - 3A(n-2) for n > 1 :
    A(2) = 2 * A(1) - 3 * A(0) = 2 * (  2) - 3 * (  1) =   4 -   3 =   1
    A(3) = 2 * A(2) - 3 * A(1) = 2 * (  1) - 3 * (  2) =   2 -   6 =  -4
    A(4) = 2 * A(3) - 3 * A(2) = 2 * ( -4) - 3 * (  1) =  -8 -   3 = -11
    A(5) = 2 * A(4) - 3 * A(3) = 2 * (-11) - 3 * ( -4) = -22 - -12 = -10
    A(6) = 2 * A(5) - 3 * A(4) = 2 * (-10) - 3 * (-11) = -20 - -33 =  13
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    Thanks guys, it turns out it was my maths. I was substituting the equation as opposed to the value of the equation, which got complicated. My bad, but thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  2. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 07:22 PM
  3. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM