A community for students.
Here's the question you clicked on:
 0 viewing
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 floatingpoint 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
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 floatingpoint 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

This Question is Closed

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0question 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 nonnegative **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

Narsat
 4 years ago
Best ResponseYou've already chosen the best response.1Things 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?

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0The 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)

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0Sometimes I get the wrong sin value compared to the sine value I get from cmath

Narsat
 4 years ago
Best ResponseYou've already chosen the best response.1V = ( pow(1, n1) //exp is another thing, 1^(n1) 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)

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0I think exp should be fine though. There has to be other errors though :/ It's not working.

Narsat
 4 years ago
Best ResponseYou've already chosen the best response.1Also, you seem to be equating V to the last value calculating, instead of acumulating all the values evaluated.

Narsat
 4 years ago
Best ResponseYou've already chosen the best response.1There 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

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0where did you initialize V=x? Just as a declaration of variables?

Narsat
 4 years ago
Best ResponseYou've already chosen the best response.1Though 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!

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0which is what I want right? How do I make sure the signs are in the right place though?

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0What will the new code look like? Thanks!

Narsat
 4 years ago
Best ResponseYou've already chosen the best response.1Your 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; }

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0Also I think there might be something wrong with my if else statement in the end

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0Also, this is C++ I should I specified sooner :P Thanks!

Narsat
 4 years ago
Best ResponseYou've already chosen the best response.1C#, C++, same thing for me =p. I'm midly experienced at C, but variants escape me. ifelse 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.

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0Idk, it still doesn't work completely. I'll take a look at it more later I guess.

mugwump101
 4 years ago
Best ResponseYou've already chosen the best response.0Strangest 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)
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.