| person29 (5) | |
|
Need help writing a program in c++ to find sine and cosine using a for loop and the formula sin(x) = -1^n/(2n+1)! * x^2n+1 cos(x) = -1^n/(2n)!* x^2n | |
|
Last edited on
|
|
| Pickle Gunner (101) | |
| One minor question... isn't -1 ^ anything = 1 or -1? | |
|
|
|
| person29 (5) | |
| the n is part of the series summation its another way to do x^2/2! +x^4/4! the 2,4 are n i think | |
|
|
|
| person29 (5) | |
|
So for sin, for example, you will have to compute (-1)^n/(2n)! *x^2n in each iteration of the for loop, and keep track of the sum. | |
|
|
|
| Pickle Gunner (101) | |
|
So I'm guessing there is no math.h? Can't you just do sin(x)Wooo 100th post | |
|
Last edited on
|
|
| Pickle Gunner (101) | ||
Otherwise, I have not got to those equations yet, try something like this:
From http://forums.devshed.com/c-programming-42/program-to-calc-sin-and-sqrt-without-math-h-855352.html | ||
|
|
||
| person29 (5) | |
|
congrats on ur 100th post but I cant use sin(x) because that is the requirement anyway my program is or should look like this but the outputs for sin and cos are wrong. Only cmath. long fact(int n) { long product = 1; while(n>1) product *= n--; return product; } int main () { double x; double w; int i; double z; double sumCos; double sumSine; cout << "Enter a x value and I will compute it in sine and cosine terms" cin >> x; for(i=0; i <= 4; i++) { w= pow(-1,i)/fact(2*i)*pow(x,2*i); sumCos += w; z=( pow(-1,i)/fact(2*i+1))*pow(x,2*i+1); sumSine += z; } cout << sumSine <<endl; cout << sumCos <<endl; | |
|
Last edited on
|
|
| ne555 (4383) | |
| Initialize your variables before use them. | |
|
|
|
| person29 (5) | |
| how | |
|
|
|