## Featured Replies in this Discussion

- by LabdabetaBefore getting to the issue at hand, I need to mention that you are making your code a little harder to read because you have ... and ... as both global and local variables. Please move those into the main function, they have no place in the global namespace. In fact that could be part of your problem, your compiler (if it isn't standard) could be using those x/value rather than the ones in your…

Before getting to the issue at hand, I need to mention that you are making your code a little harder to read because you have `double x;`

and `double value;`

as both global and local variables. Please move those into the main function, they have no place in the global namespace. In fact that could be part of your problem, your compiler (if it isn't standard) could be using those x/value rather than the ones in your sine function. Also instead of using a while loop this is the perfect place for a for loop: `for (int count=3; count<=100; count+=2)`

will be a lot easier than the while loop. Also you really should put 100 into a constant global of some kind at the top of your program. I would suggest `#define PRECISION_ITERATIONS 100`

then use `for (int count=3; count<=PRECISION_ITERATIONS; count+=1)`

that way if you ever have to change the precision from 100 to something else you won't have to search through your code, instead you just go to the top and change its value. (You could also ask the user to enter the number of iterations)

Now onto the formula. It looks like you just plugged in the formula given without understanding it. What your teacher is trying to get you to do is approximate sin/cos using a Maclaurin series. If you do not know what that means then I don't blame you. It often is not taught until 1st year university (thats when I was taught it). However I taught myself them a little earlier. If you understand basic calculus (taking derivatives) then you will find that the wikipedia page for Taylor Series (https://en.wikipedia.org/wiki/Taylor_series) is remarkably complete!

So, what formula are you trying to get. The formula given is in a minimal form, but it is the same as the Maclaurin series (http://blogs.ubc.ca/infiniteseriesmodule/units/unit-3-power-series/taylor-series/maclaurin-expansion-of-sinx/) You just need to make some modifications to get there. To do this just notice a few things:

1) The sign of the Maclaurin approximation always switches between + and -. This is accomplished by multiplying by -term because multiplying by negatives switches signs.

2) The bottoms of the fraction are i! This is accomplished in a very clever way by using the previous two i's and multiplying them. If you expand term_i-2 you would see that you end up with the factorial of i on the bottom, even though you only multiply 3 things, because the previous i's are all stored in term_i-2.

3) The terms are the terms of the Maclaurin series, you need to add them all together to get your answer. So just adding an extra 'x' won't cut it.

As such I think if you have the full equation you should be able to get it working. Here is what the true equation is:

sin(x)=sum of all term_i

where `term_i=-(term_i-1)*((x*x)/(i*(i-1)))`

and i is only odd (or only even for cos(x)).

As such the error in your code is where you set sine=x+term. You don't want to add term to x. You want to add term to sine and keep adding as you go. You might think that this will make sine really big, but remember that half of the terms are negative! I guess what I am trying to say is that `sine=x+term`

should really be `sine=sine+term`

or `sine+=term`

and that `sine=0;`

should be replaced with `sine=x`

or `sine=term`

.