# 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

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

Expert's answer

#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;

}

#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;

}

## Comments

## Leave a comment