Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
mugwump101
Group Title
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
 2 years ago
 2 years ago
mugwump101 Group Title
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
 2 years ago
 2 years ago

This Question is Closed

mugwump101 Group TitleBest ResponseYou've already chosen the best response.0
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 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
 2 years ago

Narsat Group TitleBest ResponseYou've already chosen the best response.1
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?
 2 years ago

mugwump101 Group TitleBest ResponseYou've already chosen the best response.0
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)
 2 years ago

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

Narsat Group TitleBest ResponseYou've already chosen the best response.1
V = ( 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)
 2 years ago

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

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

mugwump101 Group TitleBest ResponseYou've already chosen the best response.0
How do I fix that?
 2 years ago

Narsat Group TitleBest ResponseYou've already chosen the best response.1
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
 2 years ago

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

Narsat Group TitleBest ResponseYou've already chosen the best response.1
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!
 2 years ago

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

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

Narsat Group TitleBest ResponseYou've already chosen the best response.1
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; }
 2 years ago

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

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

Narsat Group TitleBest ResponseYou've already chosen the best response.1
C#, 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.
 2 years ago

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

mugwump101 Group TitleBest ResponseYou've already chosen the best response.0
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)
 2 years ago

agdgdgdgwngo Group TitleBest ResponseYou've already chosen the best response.0
looks good
 2 years ago

mugwump101 Group TitleBest ResponseYou've already chosen the best response.0
Not at all :(
 2 years ago
See more questions >>>
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.