| thropedo7 (2) | |
|
Write a program that calculates the value of sin(x) from the power series x - (x^3/3!) + (x^5/5!) - (x^7/7!) + ..... The user should be prompted for both the value of x and the number of terms to include in the calculation. My code seems to work for most numbers but if I input n (number of terms) greater than 7, then it just goes to a really high number. Also, how would I change my code so that the numbers are exact, not integers. Any help would be greatly appreciated! #include <iostream> #include <cmath> using namespace std; int PowerSeries(int x,int n) { int y=0; int i=1; int fSum=0; int fFactorial=-1; while (i<=n*2) { for (y=1;y<=i;y=y+1) { fFactorial=-fFactorial*y; } fSum=fSum+pow(x,i)/fFactorial; i=i+2; } return fSum; } int main(void) { int n=0; int x=0; cout << " Please enter the value of x. " << endl; cin >> x; cout << " Please enter the number of terms. " << endl; cin >> n; cout << PowerSeries(x,n) << endl; return 1; } | |
|
|
|