Question

Problem 1: The series expansion for cos, and sin, functions and trigonometric identities are as follows (r in radians) 6 20 Points cos x = 1-2, + sin(2x) = 2 sinx cosx cos(2x) cos2 -sin2

a.) Write a C++ program that calculates the value of the series sin x and cos x, sin 2x or cos 2x where the user enters the value of x (in degrees) and n the number of terms in the series. For example, if n= 5, the program should calculate the sum of 5 terms in the series for a given values of x. The program should use switch statements to determine the choice sin x AND cos x, sin 2x OR cos 2x. (Note: you may have to convert degrees to radians for sin and cos functions.)

b.) Draw a detailed flow chart on how this program is designed, indicating the necessary inputs, outputs and thealgorithm you have used.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;

long long int factorial(int n)
{
int f=1,i;
for(i=2;i<=n;i++)
f*=i;
return f;
}

double sine(int n, double x)
{
int i;
double ans = x,tmp=1,exp=x;

for(i=1;i<n;i++)
{
tmp+=2;
exp*=x*x;
if(i%2)
ans-=exp/factorial(tmp);
else
ans+=exp/factorial(tmp);
}
return ans;
}

double cosine(int n, double x)
{
int i;
double ans = 1,tmp=0,exp=1;

for(i=1;i<n;i++)
{
tmp+=2;
exp*=x*x;
if(i%2)
ans-=exp/factorial(tmp);
else
ans+=exp/factorial(tmp);
}
return ans;
}

int main()
{
int n,choice;
double x,ans,tmp;

cout<<"Enter the angle and number of terms to be added\n";
cin>>x>>n;

x= x * 22 / (7 * 180); //Degree to radian

cout<<"1.sinx & cosx\n2.sin2x\n3.cos2x\n";
cin>>choice;

switch(choice)
{
case 1:
cout<<"sinx = "<<sine(n,x)<<endl;
cout<<"cosx = "<<cosine(n,x)<<endl;
break;

case 2:
cout<<"sin2x = "<<2*sine(n,x)*cosine(n,x)<<endl;
break;

case 3:
cout<<"cos2x = "<<cosine(n,x)*cosine(n,x)-sine(n,x)*sine(n,x)<<endl;
break;
}
}

START INPUT x,n & choicc Convert x to radians CHoose function sin2x cos2x sinx & cosx OUTPUT THE ANSWER STOP

Add a comment
Know the answer?
Add Answer to:
a.) Write a C++ program that calculates the value of the series sin x and cos...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • verify the following trigonometric identities. cos y 1-sın y 5, sec y + tany= cos x-sin...

    verify the following trigonometric identities. cos y 1-sın y 5, sec y + tany= cos x-sin x -cosx 1-tanx sinx cosx-l 7. sin20+cos 2 θ+ cot 2a 1+tan 2 θ 8.

  • Use a switch statement to build a menu-driven calculator

    ENGR 40 PROGRAMMING ASSIGNMENT MENU-DRIVEN CALCULATOR PROGRAM Use a switch statement to build a menu-driven calculator. Your program should ask the user to choose from a list of ten possible functions (square root, square, natural log, common log, e'x yx, 1/x, sin(x), cos(x), and tan(x). Each choice will correspond to an integer -example: (1) square root, (2) square, etec. Once the person has input a function choice then a switch statement will control the flow of the program- ie. the program...

  • **This is for the C Language. Trigonometric Calculator Specification You are required to write a program...

    **This is for the C Language. Trigonometric Calculator Specification You are required to write a program that calculates and displays values of the trigonometric functions sine, cosine, and tangent for user-supplied angular values. The basic program must comply with the following specification. 1. When the program is run, it is to display a short welcome message. TRIG: the trigonometric calculator 2. The program shall display a message prompting the user to enter input from the keyboard. Please input request (h-help,...

  • Problem1. Write a C program, called cos.approx.c, that computes the approximate value of cos(x) according to...

    Problem1. Write a C program, called cos.approx.c, that computes the approximate value of cos(x) according to its Tavlor series expansion: (-1)"r2n (2n)! x2 x4x6x8x10 cos(x)-Σ 1 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...

  • Problem 5 xx The Taylor series expansion for sin(x) is sin(x) = x -H + E-E+...

    Problem 5 xx The Taylor series expansion for sin(x) is sin(x) = x -H + E-E+ = o E- (-1) . 2n +1 no (2n+1)! !57 where x is in radians. Write a MATLAB program that determines sin(x) using the Taylor series expansion. The program asks the user to type a value for an angle in degrees. Then the program uses a while loop for adding the terms of the Taylor series. If an n is the nth term in...

  • Write in Python 3 Write a program that takes the value of an angle x in...

    Write in Python 3 Write a program that takes the value of an angle x in degrees, such that 0 s x <90, and calculates approximate value of sine of the given angel using the following series sin(x)-x-for all x The series given above has infinite number of terms. However, you can only include a finite number of terms in the series. The absolute value of each subsequent term is smaller than that of the term calculated before it. The...

  • Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos...

    Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos_series that takes that takes as its inputs, x and N and has output given by the sum in the N-term Maclaurin Series approximation for Cos(x). Hint: try a “for loop” and set “format long” in your code. You may use the MATLAB built-in function factorial() B. Check your code by finding the 2-terms, 3-terms, 4-terms, 5-terms and 6-terms Maclaurin Series approximations every 30 degrees...

  • Verify the following identity. sin? x + cos2x = cos? To transform the left side into...

    Verify the following identity. sin? x + cos2x = cos? To transform the left side into the right side, should be changed to and the left side simplified. Enter your answer in the answer box. Use the power-reducing formulas to rewrite the expression as an equivalent expression that does not contain powers of trigonometric functions greater than 1 40 sin?x cos? 40 sin’x cos2x = 0 Enter your answer in the answer box. Express the given product as a sum...

  • Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_Student...

    Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_StudentlD where StudentID is your actual student ID number) that has one integer input (N) and one double input (x) and returns a double output S, where N S = n 0 and X2 is given by 0 xeVn n 0,1 Хл —{2. nx 2 n 2 2 m2 x2 3 (Note: in the actual quiz, do not expect a always, practice...

  • Problem - 1: For the following declarations below, indicate which of the following statements are...

    Problem - 1: For the following declarations below, indicate which of the following statements are valid or not 5 Points int At yAddst; long *dtAddst, ptAddst; double *ptZ int a long b; double c 1. yAddst =&a; 3. yAddst Atl 4, pt2=&a; 5, ptAddst &b: 2. yAddst =&c; Problem - 2: Consider Problem - 1 in HW-3 that calculates the value of the series e, sinx, cos r using functions. Now modify this C++ program to calculate the value of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT