Answer to Question #39886 in C++ for peter

Question #39886
using the series definition of e^+-jx,
develop an algorithm using pseudocode for computing cos(x) and sin(x)
use a sentinel controlled while loop
your accuracy should be at least to two decimal please help me out i need the solution for submission before monday, please
please
1
Expert's answer
2014-03-24T11:10:17-0400
#include <iostream>
#include <math.h>

using namespace std;

const long N=12;
#define M_PI 3.1415926535897932384626433832795

// Module
double mabs(double x)
{
return (x>0)?x:-x;
}

// The degree x^y
double mpow(double x, long y)
{
double r = 1;
while((y--)>0) r*=x;
return r;
}

// Factorial x!
long fact(long x)
{
if (x<=1) return 1;
else return x*fact(x-1);
}



// Sinus series
double seq_sin(double x)
{
double r = 0;
for(long n=0;n<N;n++) {
r+=mpow(-1,n)*mpow(x,2*n+1)/fact(2*n+1);
}
return r;
}

// Cosine series
double seq_cos(double x)
{
double r = 0;
for(long n=0;n<N;n++) {
r+=mpow(-1,n)*mpow(x,2*n)/fact(2*n);
}
return r;
}



void main()
{
cout << "sin(pi/5)=" << sin(M_PI/5) << endl;
cout << "seq_sin(pi/5)="<< seq_sin(M_PI/5) << endl;
cout <<endl;
cout << "cos(pi/5)=" << cos(M_PI/5) << endl;
cout << "seq_cos(pi/5)="<< seq_cos(M_PI/5) << endl;

cin.get();cin.get();
return;
}

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

LATEST TUTORIALS
APPROVED BY CLIENTS