HHHHHHHHEEELLLLPPPPPPPP

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
One minor question... isn't -1 ^ anything = 1 or -1?
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
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.
So I'm guessing there is no math.h?
Can't you just do sin(x)


Wooo 100th post
Last edited on
Otherwise, I have not got to those equations yet, try something like this:


void find_sin( double user )
{
double numer = user;
double denom = 1.0;
double sign = 1.0;
double sin = 0.0;

int i;

for( i = 1 ; i <= 20 ; i = i + 2 )
{
sin += numer / denom * sign;
numer *= user * user;
denom *= i*2.0 * (i*2.0+1.0);
sign *= -1.0;
}
}

From http://forums.devshed.com/c-programming-42/program-to-calc-sin-and-sqrt-without-math-h-855352.html
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
Initialize your variables before use them.
how
Topic archived. No new replies allowed.