My teacher wants me to write a function called
double cos(float);
to compute the cos of an angle (given in radians) using the Taylor series shown. Your function should find the value of the cos for ANY angle (preferred) or at least angles up to 2 PI (Hint: if you can do the later, the former should be easy) to a precision of 4 decimal places (within 0.00005). You should put the function cos() in the file called trig.c. You can use the function pos_power() in your exponent.c from Lab 6 and Hw 4, and you should write functions for factorial() and close_enough() in the file called util.c. This last function is given two doubles and returns true if they are within 0.00005 of each other. You can put any other functions you may want in util.c as well.
So I wrote my driver1.c file:
#include<stdio.h>
#include"util.c"
#include "exponent.c"
main()
{
int fact;
int answer;
printf("Enter a degree:\n");
scanf("%d", &fact);
answer=factorial(fact);
printf(" cos of %d degree is:%d\n", fact, answer);
}
My util.c file:
#include<stdio.h>
int factorial(float fact);
int close_enough(float num1, float num2);
int factorial(float fact)
{
float total;
total=fact*(fact-1);
while(fact>2)
{
fact=fact-1;
total=total*(fact-1);
}
return(total);
}
int close_enough(float num1, float num2)
{
if(num1-num2 >=0.00005)
{return 0;}
else
{return 1;}
}
My trig.c file:
#include<stdio.h>
#define PI 3.14
int cos(float x);
{
float answer;
printf("Enter an angle to compute:");
while(scanf("%d, &x)==1)
{
answer=(x*PI)/180;
}
printf("The cosine of %d is %d.\n", x, answer);
}
And lastly my exponent.c file:
#include<stdio.h>
#include"exponent.h"
/* #define DEBUG */
/* Declaration */
float pos_power(float base, int exponent)
{
float num;
float e;
num=base;
e=exponent;
float value;
value=1;
int power;
power=1;
if(e<0) /* Math rules */
{return 0;}
if(e == 0)
{return 1;}
#ifdef DEBUF /* Function */
printf("debuf: Enter pos_power: base = %f exponent= %d\n", base, exponent);
#endif
while (power<=e) /* Condition */
{
power=power+1; /* Controls how many times the function loops */
value=value*base;
}
#ifdef DEBUG /* If the first function is executed */
printf("debug:Exit pos_power: result = %4.9f\n", value);
#endif
return value;
}
The thing is I know I need to put the function double cos(float) in the trig.c file, but I don't know what to do with it when the function cos() is in there too. Plus, the angle needs to be calculated by the Taylor series which is cos(x)= 1 - x^2/2! + x^4/4! - x^6/6! ... My factorial (util.c) works, so is my exponent.c but how can I make it to be only even and just like in the Taylor series? And should I put the Taylor series in driver.c?
Please help me.