# Creating C++ program to make a Sine fuction without any library other than iostream?

• November 10th, 2012, 03:46 AM
weirdom3
Creating C++ program to make a Sine fuction without any library other than iostream?
Hi. I am a beginner in C++ programming. My assignment is to create a C++ Program to find the sine of a number without any library other than iostream by using Taylor Series:

sin (x) = (x/1!) - (x^3/3!) + (x^5/5!) - (x^7/7!) + (x^9/9!) ...... (x^n/n!).

I've spent 4-5 hours on this but i just cant seem to make it right. I have to submit it within 6hrs.

Here is what i have done till now:

#include <iostream>

double fact (int f); //declaration of factorial function

double power(double x, int y); //declaration of power function

double sin(int x); //declaration of sine function

//double cos(int x); //declaration of cosine function

//double tan(int x); //declaration of tangent function

using namespace std;

int main()
{
float x=0;

cout << "Enter the value of x in Sin(x): " << endl;
cin >> x;

cout << "Sine of " << x << " is " << sin(x);
cout << endl;
}

return 0;
}

//Function for Factorial
double fact (int x)
{
double f=1;

if (x==0)
{
return f;
}

else
for (int i=1; i<=x; i++)
{
f=f*i;
}

return f;
}

//Function for Power
double power (double x, int y)
{
double p=1;

for (int i=1; i<=y; i++)
p=p*x;

return p;
}

//Function for Sin
double sin (int x);
{
double sum_pos = 0;
double sum_neg=0;
double t_sum=0;

for (int i=1; i<=1000; i+=4)
{
sum_pos = sum_pos + (power (x,i) / fact (i));
}

for (int i=3; i<=1000; i+=4)
{
sum_neg = sum_neg + (power (x,i) / fact (i));
}

t_sum = sum_pos - sum_neg;

return t_sum;
}

• November 10th, 2012, 05:30 AM
VictorN
Re: Creating C++ program to make a Sine fuction without any library other than iostre
Please get back, read what guys wrote you in response to your problem and try to implement what they recommended.
• November 10th, 2012, 05:35 AM
weirdom3
Re: Creating C++ program to make a Sine fuction without any library other than iostre
Quote:

Originally Posted by VictorN