November 10th, 2012, 04:46 AM
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:
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;
cout << "Enter the value of x in Sin(x): " << endl;
cin >> x;
cout << "Sine of " << x << " is " << sin(x);
cout << endl;
//Function for Factorial
double fact (int x)
for (int i=1; i<=x; i++)
//Function for Power
double power (double x, int y)
for (int i=1; i<=y; i++)
//Function for Sin
double sin (int x);
double sum_pos = 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;
Please help me. I would be grateful to any sort of help.
Tags for this Thread
Click Here to Expand Forum to Full Width