no the problem is that you have if it is less than equal to 0 do something..it will repeat for ever. You should read the recursion section on this site http://www.cplusplus.com/doc/tutorial/functions2/
But anyways it would be something like this
1 2 3 4 5
double recursion( double n )
{
if( n > 0.00001 ) return( ( 1000 - n ) * .2 - recursion( n - 1 ); //.00001 because it is a double and 0 could really be .000000000000001 and not 0.
return( 1 ); //if n is 0 we return 1.
}
I think this is what you are trying to do but I could be wrong. It is hard to tell because I have no idea what a1 a2 and a3 are. I just assumed the were the previous values of 1000 - n * .2
Actually no my solution will not work can you please explain what a1, a2, and a3 are? Are they all the numbers greater than 0?
After investigating your "formula" I noticed all it is doing is starting with 1000 then each time you call the function it divides by 2.
n = 2 would mean 1000 / 2 / 2 = 1000/4 = 250.
n = 4 would mean 1000 / 2 / 2 / 2 / 2 = 1000 / 8 = 62.5
So lets simplify your formula from
f(n) = (1000 - f( 0 ) - f( 1 ) - f( n ) ) * .2
to something more like this
f(n) = 1000 / ( 2n )
We can then code it like this
1 2 3 4
double function( int n )
{
return( 1000 / ( 2*n ) ); //I see no point in using recursion.
}
Oh sorry I thought it said /2 miss read it.
That's an easy fix.
The new formula would be:
( 1000 * .2 ) * .8^( n - 1 )
or
200 * .8^(n-1)
so we could write that like this
1 2 3 4
double function( int n )
{
return( 200 * pow( .8 , n - 1 );
}
*edit I am too lazy to come up with a recursion function. but it'd probably just multiply a static number by .8 each time the recursion happens.
You're not returning the *.2 for the final result. By the way it is "recursive" not "recurrsive." Also I think using recursive for something like this is a waste. Might as well just put 200 * pow( .8 , n - 1 ) and get the answer that way. I think it's better to use logic and go the easy way not the hard way. Also I think if you go the easy way it will compile a lot faster than using recursion.