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)

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?)

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)

end quote.

this is what i have so far....

#include <iostream>
#include <iomanip>
#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;
}

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]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.