Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My output doesn't match with the solution. Can you please check the code. I also put my output in here just in case if you

My output doesn't match with the solution. Can you please check the code. I also put my output in here just in case if you want to see what's not the same!

image text in transcribed

image text in transcribed

// C program to find approximate value of cos(x) using Taylor series

#include

#include

// function declaration

int factorial(int);

double power(double ,int);

double cos_N(double ,int);

double cos_delta(double,double);

int main(void) {

int m,option,iter;

double x,delta;

int i=1;

// file pointers for input and output file

FILE *fileptr= fopen("cos_input.dat","r");

FILE *outFile = fopen("cos_output.dat","w");

// if input file doesn't exist

if(fileptr == NULL)

printf("Unable to open file");

else{

fscanf(fileptr,"%d",&m); // read m

// read till the end of file

while(!feof(fileptr))

{

fscanf(fileptr,"%d",&option); // read the option

fscanf(fileptr,"%lf",&x); // read the value of x

// call cos_N

if(option==1)

{

fscanf(fileptr,"%d",&iter);

fprintf(outFile,"Case %d: cos(%lf) = %lf ",i,x,cos_N(x,iter));

fflush(stdout);

}else{ // call cos_delta

fscanf(fileptr,"%lf",&delta);

fprintf(outFile,"Case %d: cos(%lf) = %lf ",i,x,cos_delta(x,delta));

fflush(stdout);

}

i++;

}

}

// closing the files

fclose(fileptr);

fclose(outFile);

return EXIT_SUCCESS;

}

// function to calculate and return the factorial of a number

int factorial(int n)

{

int fact =1;

int i=1;

for(;i

}

fact=fact*i;

return fact;

}

// function to calculate and return the number raised to power n

double power(double x,int n)

{

double p = 1;

int i=0;

for(;i

}

p=p*x;

return p;

}

// function to calculate the value of cos(x) given x and number of iterations

double cos_N(double x,int n)

{

double cx=1;

int i=1;

for(;i

}

cx=cx+(((double)power(-1,i)*power(x,2*i))/factorial(2*i));

return cx;

}

// function to calculate the value of cos(x) given x and fixed precision

double cos_delta(double x,double delta)

{

double cx,px,eps;

int num=2;

cx=1;

px=0;

int sf=-1;

eps = cx - px;

if(eps

eps = -eps;

while(eps > delta)

{

px=cx;

cx = cx+((sf*power(x,num))/factorial(num));

eps = cx-px;

if(eps

eps =-eps;

sf=-sf;

num=num+2;

}

return cx;

}

// end of program

image text in transcribed

Problem 1. Write a C program, called cos.approx.c, that computes the approximate value of cos(x) according to its Taylor series expansion: (-1)nx2n (2n)! 8 10 cos(r This series produces the exact value of cos(x) for any real number x, but contains an infinite number of terms. Obviously, a computer program can compute only a finite number of terms. Thus, you will have to truncate the infinite series in (1). Your program should be able to do so in two different ways Fixed number of terms: Implement the function cos N (double x, int N) that accepts as pa- rameters a real number x and an integer N (you can assume that N will be always posive). The function cos_N should return the sum of the first N terms in (1), as a double Fixed precision: Implement the function cos_delta (double x, double delta) that ac cepts as parameters a real number x and another real number (you can assume that will be always positive). The function cos-delta should return, as a double, the sum of the first N terms in (1), where N is the smallest positive integer such that N- N-2 12)"x2" (2n)! (2n) Notice that the first sum in (2) contains N terms, while the second sum contains N- 1 terms. It is possible for the second sum in (2) to be empty-this happens when N = 1 . You should assume that an empty sum evaluates to zero Your program should read its input from a file called cos.input.dat. The first line in this file is a positive integer m (you can assume that m 64). The first line is followed by 111 other lines; each such line constitutes a test case. Every test-case line contains three numbers separated by whitespace. The first number is either 1 or 2, indicating whether you should use cos N or cos_delta. The second number is the value of x for which you should compute cos(x). The third number y is either the re- quired precision (if the first number is 2) or the required number of terms N (if the first number is 1) In the former case, y will be a floating-point number while in the latter case, it will be an integer. In both cases, you can assume that y is positive. Here is a sample cos.input.dat file 5 2 1 0.00001 1 1.5 2 2 0.09 2 2 1.1 2<>

<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions