Ask your own question, for FREE!
Computer Science 12 Online
OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

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)

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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)

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

How do I fix that?

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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!

OpenStudy (anonymous):

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

OpenStudy (anonymous):

What will the new code look like? Thanks!

OpenStudy (anonymous):

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; }

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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)

OpenStudy (anonymous):

looks good

OpenStudy (anonymous):

Not at all :(

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!