writing function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>


using namespace std;

double x;

double function (x + 2) 
{
	if (x ==0)
		return 1;
	if (x== 1)
		return (1/3);
	else 
		return ( (10/3)*function(x+1) - function(x));
}

int main()
{
double y;
cout<<"type in the number nigga faggot";
cin>>y;


}


what i want to code is x(n+2) = (10/3) * X(n+1) - Xn for x0 = 1 x1 = 1/3

however they don't let me do function (x + 2)
Okay, your function parameters are all wrong. You cannot do calculations tehre, you can only put parameters in seperated by commas ( int myFunc (int a, int b)).

Your vulgar language is another thing...

You never call your function from anywhere in the program, thereby making it useless.

http://cplusplus.com/doc/tutorial/functions/
OO sorry for the vulgar language. If u mean my grammar sucks, then ok... i'll try to improve that. If you are pointing out i did not show any gratitude then i am really sorry i will be more polite.


So back to the question, yes i know you cannot pass a +2 in the function at the beginning. So the actually question is how to approach to write a recursion of x(n+2) = (10/3) * X(n+1) - Xn for x0 = 1 x1 = 1/3?

I can simplify the function by solving it and it will end up Xn = (1/3)^n but i just want to know is there a way to code x(n+2) = (10/3) * X(n+1) - Xn for x0 = 1 x1 = 1/3 instead. S=.
You can code it as xn = (10/3) * xn-1 - xn-2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>


using namespace std;

double x;

double function (x ) 
{
	if (x ==-2)
		return 1;
	if (x== -1)
		return (1/3);
	else 
		return ( (10/3)*function(x-1) - function(x-2));
}

int main()
{
double y;
cout<<"type in the number";
cin>>y;


}


yea i tried the above and it give me some strange numbers that i wasn't expecting. e.g i put in (1) it give me 0 S=. So it seems that's how i should be doing it, then how to set my initial conditio where x0 = 1 x1 = 1/3?
Well, the initial conditions should be the same as the ones in your
first post. The only thing you should change is the recursive formula.

Another thing. This -> 1/3 is integer division. The result
is zero. What you want is 1.0/3.0. Same here -> 10/3.
Topic archived. No new replies allowed.