Okay so this is for homework and i am stuck, we have to write our own cos func. I understand how to set every thing up and i was given the formula that
cos(x)=(-1^n)*((x^2n)/(2n)!)
this is what i have set up so far. any help would be great.
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
float cosd(float);
void main() // shell to test the cosd() function
{ // This program is a poor example of programming.
// It is only for testing another function, in
// this case the cosd() function.
float angle, answer;
do
{
printf("Enter angle (in degrees): ");
scanf("%f",&angle);
answer=cosd(angle);
printf("cos(%2f)=%6.4f\n\n",angle,answer);
} while (angle != -99.0); // -99 is sentinel value
}
// cosd() function goes here
float cosd(float d)
{
float r,term,sum,result;
r=(d)*(3.14159265/180);
term=1;
while(term > 0.0005)
{
sum=((r*r)/((term*2)*(2*term-1)))*(term-1);
}
return sum;
}
I mean i got the function to work by doing
Code:
float cosd(float d)
{
float r,sum;
r=(d)*(3.14159265/180);
sum= 1-((pow(r,2)/2))+(pow(r,4)/(4*3*2))-(pow(r,6)/(6*5*4*3*2))+(pow(r,8)/(8*7*6*5*4*3*2))-(pow(r,10)/(10*9*8*7*6*5*4*3*2))+(pow(r,12)/(12*11*10*9*8*7*6*5*4*3*2))-(pow(r,14)/(14*13*12*11*10*9*8*7*6*5*4*3*2));
return sum;
}
but my professor wants an algorithm that generates a term based on the previous term and i just don't see how to do it, yet.