i have written my code!! but i need someone to remake it!!

so i wanted someone to remake the code that i`ve written to have :
a recursive function to evaluate the first n terms in series specified
y= 1 - x + x^2/2 - x^3/6 + x^4/24 +....+(-1)^n x^n/n!



and this is my code:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <iostream>

using namespace std;

double power(int n, double x)

{

	double d =1;
	for (int i = 1 ; i<=n ; i++)
		d *= x;

	return d;


}

double fact(int n)

{

	double d =1;
	for (int i = 1 ; i<=n ; i++)
		d *= n;

	return d;


}



double function (int n, double x)

{
	double  y = 0;

	for ( int i=0; i< n ; i++)
	{if (i%2 == 0)
	y = y + (power(i , x) / fact (i) );
	
	else 
		y = y - (power(i , x) / fact (i) );
	}
	return y;
}



int main ()
{ 
	int n ;
	double x;
	double result;

	cout<< "enter integers N & X: \n";
cin>> n >> x;

result =function(n ,x);

cout<< "the result is: " << result<< endl;



return 0;
}



can you remake it??
Last edited on
please asap
closed account (48T7M4Gy)
what does remake it mean?
i meant writing it again but to let it work with a recursive function instead of what it is !!

kemort
it means to make it better or to find a better solution
kind of!!...

sorry for my bad english
Last edited on
I remade your code. Since it is your homework, I have not going to display it here.

For myself, I wrote an iterative version and then two recursive versions. Unfortunately, I cannot see how to do a recursive version without using two functions: one for the sum and one for the term.

Based on doing this, I can give you some clues (or hints).
1) You do not need your power() or fact() functions.
2) What is the ratio be between term j and term j-1? Here is the recursive clue: term(j) is term(j-1)*-1*x/j.
3) What is term(0)? Hint: 1.0.
4) So what is term(1)?

I hope that helps some.
Here, I made a recursive function that does the EXACT same thing as yours.

1
2
3
4
5
6
7
8
9
double function (int n, double x, int i = 0)
{
	if (i >= n) return (double)0;

	if (i%2 == 0)
		return function(n, x, i+1) + (power(i , x) / fact (i) );
	else 
		return function(n, x, i+1) - (power(i , x) / fact (i) );
}


I didn't check if it works correctly, but with short functions like this it's not that easy to make a mistake.
Note: this could be optimized, but I made this function to look as similar to your function as it can be.
pheininger
I`m not good at this or in english .but either way> thanks ..


zoran404
i guess it wil have some errors.. cause i still have to do other functions i have to recursive!! but i`ll try... thank yew

Last edited on
so this is what i got with your help
zoran404


and i tried to turn the other functions into recursive function then called by
funtion(x,y,i=0)


but all i get is errors...

here is my code for turning my last function into recursive with output:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>

using namespace std;




double power(int n, double x)

{

	double d =1;
	for (int i = 1 ; i<=n ; i++)
		d *= x;

	return d;


}

double fact(int n)

{

	double d =1;
	for (int i = 1 ; i<=n ; i++)
		d *= n;

	return d;


}



double function (int n, double x, int i = 0)
{
	if (i >= n) return (double)0;

	if (i%2 == 0)
		return function(n, x, i+1) + (power(i , x) / fact (i) );
	else 
		return function(n, x, i+1) - (power(i , x) / fact (i) );
}




int main ()
{ 
	int n ;
	double x;
	double result;

	cout<< "enter integers N & X: \n";
cin>> n >> x;

result =function(n ,x );

cout<< "the result is: " << result<< endl;



return 0;
}


/*
enter integers N & X:
3 5
the result is: 2.25
Press any key to continue
*/
/*
enter integers N & X:
4 6
the result is: -4
Press any key to continue
*/
I don't really think putting power and factorial functions into your function would be necessary, as they are simple linear functions that might be found in math librarys. (you have pow in math.h for example)
You can easily copy paste them into your function if you want tho. Or turn them into recursive functions, I've already shown you how to do it.

Btw, the function I wrote earlier could be simplified by declaring it as double function (int n, double x) and using function(--n, x) as a recursive call.
no.. my instructor wants it like that ,, really thanks!!

zoran404
:3

i guess i`m sticking with what it came to be (^_^")
You can see that every term in the summation produce the next by: rn = rn-1(-x/n).
This method works with very large integers while above methods get overflowing errors for very small values of n.
Last edited on
Topic archived. No new replies allowed.