954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

User functions

hi,
I'm a beginner as far as c++ and i need help on writting a program.

Given

sin x = x – (x^3/3!) + (x^5/5!) – (x^7/7!) + (x^9/9!) ….. where x is in radian.

cos x = 1 – (x^2/2!) + (x^4/4!) – (x^6/6!) + (x^8/8!) ….. where x is in radian.


1. Write two functions g(x)= sin(x) and h(x)= cos(x) using the series above to obtain accuracy to 5 decimal places.

2. Write a C++ program that uses the functions above to calculate
f(n) for n=0 to 6 where

f(n)= 5g(n)* h(4000*PI*n + PI/3)
= 5 sin(n)* cos(4000*PI*n+PI/3)

Run Output Format Requirement

n 5sin(n) cos(4000*PI*n+PI/3) f(n)

red10975
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Please read the sticky posts at the top of this forum. In short, we don't do your work for you. Write what you can, ask questions about problems you encounter.

Here's two hints to get you going. You'll want to write a separate factorial function (many examples abound) and you'll need a way to multiply alternately by +1 and -1 in the loop that runs your sin/cos functions. (Or is that three hints?)

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

hi, I'm a beginner as far as c++ and i need help on writting a program.

Given

sin x = x – (x^3/3!) + (x^5/5!) – (x^7/7!) + (x^9/9!) ….. where x is in radian.

cos x = 1 – (x^2/2!) + (x^4/4!) – (x^6/6!) + (x^8/8!) ….. where x is in radian.

1. Write two functions g(x)= sin(x) and h(x)= cos(x) using the series above to obtain accuracy to 5 decimal places.

2. Write a C++ program that uses the functions above to calculate f(n) for n=0 to 6 where f(n)= 5g(n)* h(4000*PI*n + PI/3) = 5 sin(n)* cos(4000*PI*n+PI/3)

Run Output Format Requirement

n 5sin(n) cos(4000*PI*n+PI/3) f(n)


this is what i have so far....


#include
#include
#define PI 3.14159265358979
using namespace std;

int main()
{
int i,j, n;

double x, xsmall, temp, sin_x, cos_x, x_power, factorial;


temp = x / (2 * PI);
xsmall = ( temp - int (temp)) * 2 * PI;
sin_x = xsmall;

for (i=0; i < 6; i++)
{
factorial = 1;
x_power = 1;
for ( j=0; j< 2*i+3; j++)
{
factorial = factorial * (j+1);
x_power = x_power * xsmall;
}
if ( i%2 == 0)
sin_x = sin_x - x_power / factorial;
else
sin_x = sin_x + x_power / factorial;
}
cout << setprecision(10);
cout << "For " << n << " terms, the sin(" << x <<") value = "
<< sin_x << endl;


temp = x / (2 * PI);
xsmall = ( temp - int (temp)) * 2 * PI;

cos_x = 1;

for (i=0; i < 6; i++) //number of terms: n
{
factorial = 1;
x_power = 1;
for ( j=0; j< 2*i+2; j++)
{
factorial = factorial * (j+1); //calculate 1*2*3*(j+1)!
x_power = x_power * xsmall; //calculate x_power exp(k)
}
if ( i%2 == 0)
cos_x = cos_x - x_power / factorial; //for i even
else
cos_x = cos_x + x_power / factorial; //for i odd
}
cout << setprecision(10);
cout << "For " << n << " terms, the cos(" << x <<") value = "
<< cos_x << endl;
}

red10975
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Read your compiler errors. Fix those. I think you'll find the code is working. It seems overly complex in some ways, but it seems to work.

When you post code, please wrap it with code tags, like:
[code]
your code goes here
[/code]

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You