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

Scan the angle in degrees x_deg. Express this angle in radians, and then calcula

ID: 3631522 • Letter: S

Question

Scan the angle in degrees x_deg. Express this angle in
radians, and then calculate Y=sin(x)+cos(x) by
using the math.h library of functions. Compare the so
calculated value of Y with the approximate value y
obtained by using n_term terms of the Taylor series for
each of

sin(x)=Sum[(-1)^n x^(2n+1)/(2n+1)!] (n goes from 0 to n_term-1)

and

cos(x)=Sum[(-1)^n x^(2n)/(2n)!] (n goes from 0 to n_term-1).

Scan an integer value of n_term. Evaluate (2n)! and (2n+1)! by embedded
for-loop statements. Use two do/while statements to continue
the calculations for different n_term and different x_deg. For
example, use flag=1 to continue calculations for different n_term
within the inner do/while loop, and flag=0 to exit that loop.
Use Flag=1 to continue calculations for different x_deg
within the outer do/while loop, and Flag=0 to exit that loop.

Use %g format for all double values. Use double declaration
when calculating k! (because k! for large k becomes quickly
too big for integer declaration).
.................................................................

Your output should look like this:

Calculation of true and approximate values of sin(x)+cos(x)

Enter x_deg: 30
True value of sin(30)+cos(30) = 1.36603

n_term approximation of sin(30)+cos(30)

Enter number of terms:
2

Explanation / Answer

#include <iostream.h>
#include <cmath.h>
using namespace std;

double fac(int n)
{
double f=n;
while (--n)f*=n;
return f;
}

int main()
{
double long x;
cout << "Enter Angle in Radians: " << endl;
cin >> x;
double cos(0.0);
double temp(0.0);

for (int i=0; i<170; i++){
if (i%2 == 1)
temp = (pow(x,i*2+2))/(fac(i*2+2));
else
temp = (-1)*(pow(x,i*2+2))/(fac(i*2+2));
if (temp <= 0.0001)
break;
else
cos += temp;
}

cout << "Cosine of Angle is: " << cos << endl;

system("PAUSE");
return 0;
}