 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

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

1. Junior Member Join Date
Nov 2012
Posts
6

## 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;
}  Reply With Quote

2. ## 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.  Reply With Quote

3. Junior Member Join Date
Nov 2012
Posts
6

## Re: Creating C++ program to make a Sine fuction without any library other than iostre Originally Posted by VictorN Please get back, read what guys wrote you in response to your problem and try to implement what they recommended.
that was some other problem. i cannot use math library and i have to call the function of factorial and power in the function of sine. i dont know how to do that. without using sine function, i can make it work but by using sine function, the program is not compiling. i really need some help here.  Reply With Quote

4. ## Re: Creating C++ program to make a Sine fuction without any library other than iostre Originally Posted by weirdom3 ... the program is not compiling. i really need some help here.
What compiler errors do you get?  Reply With Quote

c++, c++ beginner, function, function calling, power #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
• 