
November 8th, 2012, 03:28 AM
#1
Write a C++ program to compute Sin(x)
Hi. I'm having problem writing this program as my assignment. I've spent hours on this but still i can't get the right answer.
Here is the question:
Write a C++ program to compute Sin(x) where
x x^3 x^5 x^7 x^9 x^n
sin (x) =  ─  +  ─  +  ─ …………. 
1! 3! 5! 7! 9! n!
Your program should accept two values from the user (the angle x and the value of n) and then should compute and print the value of sin(x).
To make the program, do following tasks.
• Write two functions, i.e. function to calculate factorial and function to calculate power having following prototypes.
double Factorial (int n); //Factorial function prototype
double Power(double x, int y); //Power function prototype
• Use these functions in your main function to compute the series.
Till now, I've written the following program but I am not able to get the right answer.
#include <iostream>
using namespace std;
double fact (int f); //declaration of factorial function
double power(double x, int y); //declaration of power function
int main()
{
int x=0; //value of x in the series
float sum_pos = 0;
float sum_neg=0;
float t_sum=0;
cout << "Enter the value of x: " << endl;
cin >> x;
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;
cout << "Sin of " << x << " = " << t_sum << 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)
{
int p=1;
for (int i=1; i<=y; i++)
p=p*x;
return p;
}
I have to submit it this weekend. So please help me what am i doing wrong??
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
