### taylor series swaping signs problem

hey guys am trying to compute a sinx taylor series but am unable to swap the + - signs in the sequence....my code is only adding...here is the sequence sinx=x-x^3/3!+x^5/5!-x^7/7!...................pls help am stuck.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` `````` #include #include #include #include using namespace std; void getValuesFromFile(vector&X) { double x=0.0; ifstream infile("indata.txt"); while(infile>>x) { X.push_back(x); } } double factorial(unsigned long number) { if(number<=1) return 1; else return number*factorial(number - 1); } void calculate(vector X) { double total; total=0.0; for(int i=1;i<=8;i +=2 ) { total=total+pow(X[0],i)/factorial(i); } cout< X; getValuesFromFile(X); calculate(X); return 0; }``````
To alternate from - to + there are several ways the easiest way would be to do something like:

 ``12345678`` ``````int sign = 1; int result = 10; //add result += 2 * sign; //add 2 sign *= -1; result += 2 * sign; //subtract 2 ``````

so you would put that inside your loop

Or you could multiply by `pow( -1 , i );` but that may be a bit more expensive.
thanx I got it to work:).....cud u also help me make it work without using the power function but loops
 ``123456`` ``````int sign = -1; for(int i=1;i<=8;i +=2 ) { sign *= -1; total += pow(X[0],i)/factorial(i) *sign; }``````

Topic archived. No new replies allowed.