## mugwump101 4 years ago The value of sin(x) can be approximated by using this infinite series formula: x^3 x^5 x^7 x^9 sin(x) = x - ___ + ___ - ___ + ___ ... 3! 5! 7! 9! Write a function approxSin() that accepts two inputs in this order, a value for x, and a positive interger for the number of terms to computer, then return a result. The result should be in 10 floating-point precision Note: You should use exact same function name. Create a program that prompts two inputs, a value for x and a value for the number of terms. The program should then display the resulting of the

1. mugwump101

question continued: The program should then display the resulting of the computing series. **Hints:** If you are having trouble with this problem try the following steps. 1. Try writing a help function calFac() that accepts a number and computes its factorial first so that: - 3! = 3\*2\*1 = 6 - 4! = 4\*3\*2\*1 = 24 - ... 2. Be careful with the terms count. The example above shows the first 5 terms. 3. Use `double` type variables for your computations. `float` will give you slightly different results. **Constraints** - The input **x** is allowed to be any double precision number (no checks are needed) - Make sure the program checks that **terms** is a positive integer, 1 or greater - If a non-negative **terms** is entered print out: **Input Error** - Make sure the output is on a single line, no spaces between digits, and no additional text. - You may use the `pow()` function in \<cmath\> only. - You can check wether your calculation is correct, check the output of the `sin()` function in \<cmath\> Example 1: Enter a value for x and terms seperated by spaces (eg: 2 10): 4.5 -3 Input Error Example 2: Enter a value for x and terms seperated by spaces (eg: 2 10): 4.5 10 approximated Sine: -0.9775310994 cmath Sine: -0.9775301177

2. Narsat

Things to note before moving on into the code: You're declaring V as a global variable, then returning it in the function approxSin() (While this is not -wrong- it's not considered good practice, you can either change the function to void and return nothing, or declare V as a local variable to the function and return it as you're doing now Should work well either way.) What seems to be the problem with this code?

3. mugwump101

The code failed on Test Failed on input "2.5 8" and invalid inputs (which means if there was a value inputted that was supposed be false, I didn't fix it for the user input to be true)

4. mugwump101

Sometimes I get the wrong sin value compared to the sine value I get from cmath

5. Narsat

V = ( pow(-1, n-1) //exp is another thing, -1^(n-1) should do the trick ( *pow(x, 2*n+1) / calFrac(2*n+1))); I believe the error was in the exp part, everything else seems fine. (Also, you can change variable t to int, since it only acts as a counter, it has no need to be double)

6. mugwump101

I think exp should be fine though. There has to be other errors though :/ It's not working.

7. Narsat

Also, you seem to be equating V to the last value calculating, instead of acumulating all the values evaluated.

8. mugwump101

How do I fix that?

9. Narsat

There it goes, the correct function is: V += (Math.pow(-1, n) * (Math.pow(x, 2 * n + 1) / calFrac(2*n + 1))); or V = V + (Math.pow(-1, n) * (Math.pow(x, 2 * n + 1) / calFrac(2*n + 1))); Doing: x += 10 is the same as doing x = x + 10 (At least in Java, should work in C# too). Another thing to note, in order to get the correct answer, I initialized V = x to account to the first value (x^1)/1 which you are not considering in your function

10. mugwump101

where did you initialize V=x? Just as a declaration of variables?

11. Narsat

Though if you initialize your for in n = 0 instead of n = 1, the V will take the initial value of x^1/1, then move to x^3/3!

12. mugwump101

which is what I want right? How do I make sure the signs are in the right place though?

13. mugwump101

What will the new code look like? Thanks!

14. Narsat

Your code should look like... double approxSin(double x, int t) { for (int n=0; n<=t; n++) { V += (pow(-1, n) * (pow(x, 2 * n + 1) / calFrac(2*n + 1))); } return V; }

15. mugwump101

Also I think there might be something wrong with my if else statement in the end

16. mugwump101

Also, this is C++ I should I specified sooner :P Thanks!

17. Narsat

C#, C++, same thing for me =p. I'm midly experienced at C, but variants escape me. if-else seems fine, note that I changed the approxSin to an int t, maybe that's causing some trouble, you could change all t's to ints, or change that one back to a double.

18. mugwump101

Idk, it still doesn't work completely. I'll take a look at it more later I guess.

19. mugwump101

Strangest thing is there's a zero after my cout statements which say, "Enter a number: " ... so it looks like Enter a value for x: 0 _(space for input)

20. agdgdgdgwngo

looks good

21. mugwump101

Not at all :(