computer science lab HW

profilefaisal0z
post_8_final_editing.docx

#include <iostream>

#include <iomanip>

#define PI 3.14159265358979 //define pi

using namespace std;

double myCos(double n); //declare myCos

double mySin(double n); //declare mySin

int main()

{

double i;

double n, f, g, h; //declare variables n,y,g(n),h(n)

cout << "n 5sin(n) cos(4000 PI n + PI /3) f(n)";

cout << endl;

cout << "---------------------------------------" << endl;

for (i = 0; i < 7; i++)

{

n = i;

g = 5 * mySin(n); //g(n)

h = myCos((4000 * PI*n) + (PI / 3)); //h(n)

f = 5 * mySin(n) + myCos((4000 * PI*n) + (PI / 3)); //f(n)

cout << n << " " << setprecision(10);

cout << g << " " << setprecision(10);

cout << h << " " << setprecision(10);

cout << f << " " << setprecision(10) << endl;

}

system("pause");

}

double mySin(double n) //mySin function

{

int i, j;

double nsmall, temp, mySin, n_power, factorial; //declare functions

//set up temp and nsmall

temp = n / (2 * PI);

nsmall = (temp - int(temp)) * 2 * PI;

mySin = nsmall;

for (i = 0; i < 10; i++)

{

factorial = 1;

n_power = 1;

for (j = 0; j< 2 * i + 3; j++)

{

factorial = factorial * (j + 1); //calculate 1*2*3*(j+1)!

n_power = n_power * nsmall; //calculate n_power

}

if (i % 2 == 0)

mySin = mySin - n_power / factorial; //for i even

else

mySin = mySin + n_power / factorial; //for i odd

}

return mySin; //validates for the intmain

}

double myCos(double n) //myCos function

{

int i, j;

double nsmall, temp, myCos, n_power, factorial; //declare functions

//set up temp and nsmall

temp = n / (2 * PI);

nsmall = (temp - int(temp)) * 2 * PI;

myCos = 1;

for (i = 0; i < 10; i++)

{

factorial = 1;

n_power = 1;

for (j = 0; j< 2 * i + 2; j++)

{

factorial = factorial * (j + 1); //calculate 1*2*3*(j+1)!

n_power = n_power * nsmall; //calculate n_power

}

if (i % 2 == 0)

myCos = myCos - n_power / factorial; //for i even

else

myCos = myCos + n_power / factorial; //for i odd

}

return myCos; //validates for the intmain

}