Feb 12, 2013 at 1:06am UTC
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 Feb 12, 2013 at 1:07am UTC
Feb 12, 2013 at 1:42am UTC
One minor question... isn't -1 ^ anything = 1 or -1?
Feb 12, 2013 at 1:44am UTC
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
Feb 12, 2013 at 1:45am UTC
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.
Feb 12, 2013 at 1:59am UTC
So I'm guessing there is no math.h?
Can't you just do sin(x)
Wooo 100th post
Last edited on Feb 12, 2013 at 2:01am UTC
Feb 12, 2013 at 2:06am UTC
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 Feb 12, 2013 at 2:14am UTC
Feb 12, 2013 at 2:31am UTC
Initialize your variables before use them.