Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program using C language Here you will be writing a function: to compute

ID: 650613 • Letter: W

Question

Write a program using C language

Here you will be writing a function:

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

and create the executable called trig.

Explanation / Answer

//place this file in trig.c



double cos(float x)
      {
      int i,fact=1;
      double sum,term;
      sum=1.0;
      term=1.0;
      i=1;
      do
         {
                         term=term*x*x;
                        term=term*(-1);
                        fact=factorial(2*i);
                        sum=sum+(term/fact);   
                        i++;
                        }
       while(term>0.00005)
        return(sum);                     
      }


//place this in util.c

float close_enough( double x,double n )
{
    int i = 1;
    float ex = 1;

    while ( i < n )
        {
            ex += (double) pow( x, i ) / fact( i );
            ++i;
        }

    return ex;
}

int factorial( int num )
{
    int f = 0;

    if ( num == 1 )
        return 1;
    else
f = num * fact( num - 1 );

    return f;
}


//driver1.c


#include "util.h"
#include "trig.h"
#include <stdio.h>

int main()
{
char ch, file_name[25];
   FILE *fp;

   printf("Enter the name of file you wish to see ");
   gets(file_name);

   fp = fopen(file_name,"r"); // read mode

   if( fp == NULL )
   {
      perror("Error while opening the file. ");
      exit(EXIT_FAILURE);
   }

   printf("The contents of %s file are : ", file_name);

   while( ( ch = fgetc(fp) ) != EOF )
      printf("%c",ch);

float x = (float)ch;
    double result=cos(x)
printf("The value is", result);

   fclose(fp);
   return 0;



}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote