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

3. Write a c++ program that uses a recursive function, and the following algorit

ID: 3683296 • Letter: 3

Question

3. Write a c++ program that uses a recursive function, and the following algorithm, to approximate the sin function. The result is output to the screen, as part of the main function. To do this, create a recursive function to calculate the sum of series of the sin function, namely

Note that this sum involves an infinite series. Thus, a maximum n = 5 iterations is to be used. Moreover, this sum involves a factorial, specifically (2n + 1)! This factorial is to be calculated iteratively. Your submission should include a screenshot of the execution of the program using each of the angles (in radians) -?/6, 2?/3 and 3?/4.

2n+1 n=0 (2n+1)!

Explanation / Answer


#include<iostream>
#include<math.h>

using namespace std;

int fact(int x);
double sinValue(double x, int n, int t);
double getRadian(float angle);

int main()
{
   float x,s=0;

   int n = 5;

   //cout<<"Enter the value of x(in degree) & n ";
   //cin>>x>>n;

   float radian = getRadian(30); //PI/6
   s = sinValue(radian, n, 0);

   cout<<" Sum of series for 30 degree = "<<s<<endl;

   radian = getRadian(120); //2*PI/3
   s = sinValue(radian, n, 0);

   cout<<" Sum of series for 120 degree = "<<s<<endl;

   radian = getRadian(135); // 3*PI/4
   s = sinValue(radian, n, 0);

   cout<<" Sum of series for 135 degree = "<<s<<endl;


   return 0;
}

double sinValue(double x, int n, int t){
  
   if(t > n)
       return 0;
      
   return (pow(-1,t)*(pow(x,2*t+1)))/((float)fact(2*t+1)) + sinValue(x, n, t+1);
}

int fact(int x)
{
   int i,f=1;
   for(i=1; i<=x; i++)
       f=f*i;
   return(f);
}

double getRadian(float angle){
   return angle*(3.14/180);
}


/*

Output:

Sum of series for 30 degree = 0.49977

Sum of series for 120 degree = 0.866553

Sum of series for 135 degree = 0.70794

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote