Answer to Question #312354 in C++ for Engr.

Question #312354

Develop a C++ program that will compute the values of six trigonometric functions (sine, cosine, tangent, cotangent, secant, and cosecant) at any angle without using cmath/math.h library. (Hint : Transform Maclaurin Series of sine and cosine function into C++ codes)

1
Expert's answer
2022-03-16T02:43:57-0400
using namespace std;


/*
	Develop a C++ program that will compute the values of six trigonometric functions 
	(sine, cosine, tangent, cotangent, secant, and cosecant) at any angle without using cmath/math.h library. 
	(Hint : Transform Maclaurin Series of sine and cosine function into C++ codes)
*/


int fact(int n)
{
    int k = 1;
    for (int i = 2; i <= n; i++)	k *= i;
    return k;
}


float GetSin_x_using_Maclaurin_Series(float x, int n)
{
	float Sum = 0,temp,PI=3.14157;
	int u;
	x = (PI*x)/180;
	for(u=0;u<n;u++)
	{
		Sum = Sum + pow(-1,u)*(pow(x,u+1)/fact((2*u)+1));
	}
	return(Sum);
}


float GetCos_x_using_Maclaurin_Series(float x, int n)
{
	float Sum = 1,temp,PI=3.14157;
	int u;
	x = (PI*x)/180;
	for(u=0;u<n;u++)
	{
		
		Sum = Sum + pow(-1,u+1)*(pow(x,(2*u)+2)/fact((2*u)+2));
	}
	return(Sum);
}


int main()
{
	float x=30,Val;
	int n = 5;
	Val = GetSin_x_using_Maclaurin_Series(x,n);
	cout<<"\n\tUsing "<<n<<" terms,\tSin("<<x<<") = "<<setprecision(4)<<Val;


	Val = GetCos_x_using_Maclaurin_Series(x,n);
	cout<<"\n\tUsing "<<n<<" terms,\tCos("<<x<<") = "<<setprecision(4)<<Val;
	
	Val = GetSin_x_using_Maclaurin_Series(x,n)/GetCos_x_using_Maclaurin_Series(x,n);
	cout<<"\n\tUsing "<<n<<" terms,\tTan("<<x<<") = "<<setprecision(4)<<Val;
	
	Val = 1/GetSin_x_using_Maclaurin_Series(x,n);
	cout<<"\n\tUsing "<<n<<" terms,\tCosec("<<x<<") = "<<setprecision(4)<<Val;


	Val = 1/GetCos_x_using_Maclaurin_Series(x,n);
	cout<<"\n\tUsing "<<n<<" terms,\tSec("<<x<<") = "<<setprecision(4)<<Val;


	Val = GetCos_x_using_Maclaurin_Series(x,n)/GetSin_x_using_Maclaurin_Series(x,n);
	cout<<"\n\tUsing "<<n<<" terms,\tCot("<<x<<") = "<<setprecision(4)<<Val;
	return(0);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment