Problem1

Write a C program, called cos approx.c, that computes the approximate value of cos(x) according to its Taylor series expansion: See image.

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: Implementthefunctioncos N(double x, int N)thatacceptsas pa- rameters a real number x and an integer N (you can assume that N will be always positive). 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 See image.

Noticethat thefirst sum in(2) contains N terms, whilethesecond sumcontains 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 apositiveintegerm (you can assumethat m <= 64). Thefirstlineisfollowedby m otherlines; 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
1 -1.00 6
2 1 0.00001
1 1.5 2
2 2 0.09
2 2 1.1

The program should write its output to the file cos output.dat. The output file should consist of m lines, one per test case. For each test case, the program should print the test-case number followed by cos(x.xxx) = y.yyyyyyyyyyyy. In the above, the argument of cos(·) should beprinted with 3 digits of precision, while its (approximate) value should be printed with 12 digits of precision. For example, here is the file cos output.dat that results upon processing the file cos input.dat above:

Case 1: cos(-1.000) = 0.540302303792
Case 2: cos(1.000) = 0.540302303792
Case 3: cos(1.500) = -0.125000000000
Case 4: cos(2.000) = -0.422222222222
Case 5: cos(2.000) = 1.000000000000

Notes:

  • As part of the solution, you are required to declare, define, and call the following two functions. The function factorial(int n) that accepts as a parameter an integer n >= 0 and then re- turns n! as an int. Recall that n! = 1·2·3···(n−1)·n for n >= 1, while 0! = 1. The function power(double x, int n) that accepts as parameter a real number x and an integer n >= 0 and returns x^n as a double. Recall that x^0 = 1 for any real number x.
  • You do not need to worry about numerical overflows. You can assume that the input is such that they do not occur. For example, in the function factorial(int n), you can assume that the parameter n is small enough so that n! fits in a variable of type int. For most machines, this means that n <= 12 Optional, not for credit: How would you modify this function so that it can compute n! for much larger values of n?
  • You are not allowed to use any functions declared in math.h in the file cos approx.c that you submit. However, it might be advisable to use standard-library functions such as pow(x,y) and cos(x) during the development and debugging of the program, in order to compare their output with the output of the functions that you implement. If you choose to do so, do not forget to delete the relevant parts of the code in the final version of cos approx.c that you submit.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.