#include <iostream>
usingnamespace 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;
}
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)?
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) );
elsereturn 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.
#include <iostream>
usingnamespace 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) );
elsereturn 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.
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.