Exercise #3, Functions Given a positive integer n and a floating point number x between 0.0 and 360.0. The objective of this exercise is to write a C++ program to compute the below math equations: 9(y, k) = P(y,k) f(k) ys y³ S(y,n) y- + 3! 5! We decompose the eqations into 5 simplified expressions as follows: 1. y = r(x) = x x π/180 2. p(y, k) = yk 3. f(k) = k! y=xxπ/180 y7 yº 7! 9! + where k is an integer smaller than or equal to n 4. 5. S(y,n)=y-q(y, 3) + q(y,5) -q(y, 7) + q(y, 9) -q(y, 11)+...+q(y,n) Write C++ functions to calculate each of the above expressions. In your main, you prompt the user to enter x and n, control the user input, then call function S(x, n) to compute expression 5. Function S(x, n) will call function r(x) to get y, then it calls other functions as needed to complete the computations and display your result at the end. e.g. Value of x: 30 Number of interations n: 12 S(30,12): 0.5 y11 yn + ± 11! n! Two other programmers calculated the above expression using different methods, in Method 1 (see below), the programmer used two loops, in Method 2 the programmer used one single loop. Method 1 int fact, sign=-1; float y = x*M_PI/180, p, sum = 0; for(int i=1;i

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Exercise #3, Functions
Given a positive integer n and a floating point number x between 0.0 and 360.0. The objective of
this exercise is to write a C++ program to compute the below math equations:
y3
y = xx π/180
y5 y7 y⁹
+
3! 5! 7! 9!
We decompose the eqations into 5 simplified expressions as follows:
1. y=r(x) = x x π/180
2. p(y, k) = yk
3. f(k)= k!
4. q(y, k) =
p(y,k)
f(k)
5. S(y, n)=y-q(y, 3) + q(y, 5)-q(y, 7)+q(y,9)-q(y, 11)+q(y,n)
S(y, n) = y- +
Method 1
int fact, sign=-1;
float y = x*M_PI/180, p, sum = 0;
for(int i=1;i<=n; i+=2)
{
where k is an integer smaller than or equal to n
p= 1;
fact= 1;
for(int j=1; j<=i; j++)
{
}
y11
p= p*y;
fact=fact*j;
11!
Write C++ functions to calculate each of the above expressions. In your main, you prompt the user
to enter x and n, control the user input, then call function S(x, n) to compute expression 5.
Function S(x, n) will call function r(x) to get y, then it calls other functions as needed to complete
the computations and display your result at the end. e.g.
Value of x: 30
Number of interations n: 12
S(30,12): 0.5
Two other programmers calculated the above expression using different methods, in Method 1 (see
below), the programmer used two loops, in Method 2 the programmer used one single loop.
Method 2
int fact, sign=-1;
float y = x*M_PI/180, p, sum = 0;
for(int i=1;i<=n; i+=2)
{
sign=-1*sign;
sum += sign*p/fact;
+ ...
±
if(i==1){
p= y;
fact= 1;
}
else {
yn
n!
p= p*y*y;
fact fact* (i-1)*(i);
}
sign=-1*sign;
sum += sign*p/fact;
Try to understand the above methods, test them, then compare the three implementations (yours, method1, and
method2) to find out: which one is the best, what are the advantages and disadvantages of each method.
By the way, when you test the three methods: They all should GIVE THE SAME RESULT. This is one way to verify that
your implementation is correct.
e.g. S(30,12)= 0.5
S(45,12)= 0.707107
S(60,12)= 0.866025
Transcribed Image Text:Exercise #3, Functions Given a positive integer n and a floating point number x between 0.0 and 360.0. The objective of this exercise is to write a C++ program to compute the below math equations: y3 y = xx π/180 y5 y7 y⁹ + 3! 5! 7! 9! We decompose the eqations into 5 simplified expressions as follows: 1. y=r(x) = x x π/180 2. p(y, k) = yk 3. f(k)= k! 4. q(y, k) = p(y,k) f(k) 5. S(y, n)=y-q(y, 3) + q(y, 5)-q(y, 7)+q(y,9)-q(y, 11)+q(y,n) S(y, n) = y- + Method 1 int fact, sign=-1; float y = x*M_PI/180, p, sum = 0; for(int i=1;i<=n; i+=2) { where k is an integer smaller than or equal to n p= 1; fact= 1; for(int j=1; j<=i; j++) { } y11 p= p*y; fact=fact*j; 11! Write C++ functions to calculate each of the above expressions. In your main, you prompt the user to enter x and n, control the user input, then call function S(x, n) to compute expression 5. Function S(x, n) will call function r(x) to get y, then it calls other functions as needed to complete the computations and display your result at the end. e.g. Value of x: 30 Number of interations n: 12 S(30,12): 0.5 Two other programmers calculated the above expression using different methods, in Method 1 (see below), the programmer used two loops, in Method 2 the programmer used one single loop. Method 2 int fact, sign=-1; float y = x*M_PI/180, p, sum = 0; for(int i=1;i<=n; i+=2) { sign=-1*sign; sum += sign*p/fact; + ... ± if(i==1){ p= y; fact= 1; } else { yn n! p= p*y*y; fact fact* (i-1)*(i); } sign=-1*sign; sum += sign*p/fact; Try to understand the above methods, test them, then compare the three implementations (yours, method1, and method2) to find out: which one is the best, what are the advantages and disadvantages of each method. By the way, when you test the three methods: They all should GIVE THE SAME RESULT. This is one way to verify that your implementation is correct. e.g. S(30,12)= 0.5 S(45,12)= 0.707107 S(60,12)= 0.866025
Expert Solution
steps

Step by step

Solved in 8 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY