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.

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

I'm not sure I understand the problem. You're defining the cos() function as double cos(float). There aren't two functions, just the one. You may also have issues with the compiler if it loads the standard cos() function as an intrinsic, because then you'd be conflicting with the standard function by using the same name.

how can I make it to be only even and just like in the Taylor series?

The cos() function defines the Taylor series. It might help to use the more general form to write your loop: cos(x) = (-1)^n / (2n)! * x^(2n). This follows more directly into an algorithm than trying to decompose a partial expansion of the series into the general concept.

So you'd get something like this:

for (i = 0; i < LIMIT; ++i)
    result += pow(-1, n) / factorial(2 * n) * pow(x, 2 * n);

And should I put the Taylor series in driver.c?

You'd call the cos() function in driver.c, probably. Your requirements suggest a certain amount of inside knowledge about the project that you didn't explain, so I can only speculate.

Oh ok now that I re-read the problem, instead of the function cos(), now I have to write double cos(). To run the programm I would have to make a makefile but I got that. So are my exponent file and util file correct? If they are can I put something like:

for (i = 0; i < LIMIT; ++i)
    result += exponent(-1, n) / factorial(2 * n) * exponent(x, 2 * n);

in my trig.c file? But how can I check to know if the last function is given two doubles and returns true if they are within 0.00005 of each other?

Oh ok now that I re-read the problem, instead of the function cos(), now I have to write double cos().

They're both the same thing...

So are my exponent file and util file correct?

At a glance, yes. Your code is a little complicated for what it's doing, in my opinion, and close_enough() should check the absolute value of the subtraction to be strictly correct, but it's probably fine for this exercise.

But how can I check to know if the last function is given two doubles and returns true if they are within 0.00005 of each other?

O_o

You're asking me about a project that's been poorly described to an outsider. These are really questions you should ask your teacher. All I can do is give you a complete example of what you already know:

#include <float.h>
#include <math.h>
#include <stdio.h>

#define THRESHOLD 0.00005

double factorial(int x)
{
    double result = 1;

    while (x > 1)
        result *= x--;

    return result;
}

double cosine(double x)
{
    double result = 0;
    int i;

    for (i = 0; i < DBL_DIG; ++i)
        result += pow(-1.0, i) / factorial(2 * i) * pow(x, 2 * i);

    return result;
}

int close_enough(double a, double b)
{
    return fabs(a - b) < THRESHOLD;
}

int main()
{
    double a, b;

    printf("Enter two floating point numbers: ");
    fflush(stdout);

    if (scanf("%lf%lf", &a, &b) == 2) {
        double cosa = cosine(a);
        double cosb = cosine(b);

        printf("cos(a) = %f\ncos(b) = %f\n", cosa, cosb);

        if (close_enough(cosa, cosb))
            printf("The two cosines are within %f\n", THRESHOLD);
    }

    return 0;
}

This is what my teacher asks me to find.
Here you will be writing a function:

         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. You will need to provide your own .h files for each of these .c files. You should also write a simple test driver to exercise your function in the file driver1.c. You should update the makefile to compile and link driver1.c, trig.c, util.c and exponent.c when you type trig.

So I rewrote my program so now it works like this:
Enter a radian:
I put 1.
Then it returns -0.9999.
Then I quit by EOF. But I just don't know the purpose of close_enough()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.