Sinx in C++ :: Forum

Start Date Last Update Igniter Last Update By Status
4:14am on Sunday 21st July 2013 11 years ago

HTHVampire

HTHVampire




HTHVampire

Hi, here's the code I wrote to compute sinx in C++, but it doesn't give me the correct value.




# include <iostream>
# include <math.h>
using namespace std;

int main()
{
int count=1;
double x;
double sine, num, dem, sign, term;
sine=0;
sign = 1;

cout << "Get x: ";
cin >> x;
num = x;
dem = count;

while ( count <= 10 )
{
term = (num/dem);
sine = sine + term*sign;
num = num*x*x;
count = count + 2;
dem = dem * count * (count-1);
sign = -sign;
}


cout << "The result is: ";
cout << sine;
return 0;
}


 



can someone points me out the mistakes I made? Thanks!



suzzett

The problem I see in this code is that, you forgot to convert the given values from degree to radians. This algorithm works for radian angles. That is the initial problem I saw. Even after that, you could not get the right answer, let me know.




HTHVampire
# include
# include
using namespace std;

double sin_value( double x);
double x;
double value;
int main()
{

cout << "Welcome!";
cout << "Let's calculate sine value. " << endl;
cout << "Enter value x: ";
cin >> x;
value = sin_value(x);

cout << "The value is: ";
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
cout << sin_value(x);
return 0;
}

double sin_value( double x)
{
int count=1;

double sine,num,term,dem;
num=x;
dem=count;
sine=0;

while ( count <= 100 )
{
term=(num/dem);
term = ((-term) * x * x) / (count * (count-1));
sine =sine + term;
count = count +2;

}

return (sine);
}

Hi, Thanks for the reply, actually i'm testing on this function, just assume that all the values are in radian.
according to the code above, if I input 1.570796326794897 which is 90 degree, my sinx should be 1.0000 , but the program gives me an error output. I keep trying and trying but ended up nothing...



HTHVampire




# include
# include
using namespace std;

double sin_value( double x);
double x;
double value;
int main()
{

cout << "Welcome!";
cout << "Let's calculate sine value. " << endl;
cout << "Enter value x: ";
cin >> x;
value = sin_value(x);

cout << "The value is: ";
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
cout << sin_value(x);
return 0;
}

double sin_value( double x)
{
int count=1;

double sine,num,term,dem;
num=x;
dem=count;
sine=0;

while ( count <= 10 )
{
term=(num/dem);
term = ((-term) * x * x) / (count * (count-1));
sine =sine + term;
count = count +2;

}

return (sine);
}






Please login to post your reply!
 

Login

Username:
Password:
Don't have an account?
Register here
Register

Subscribe Youtube



Network DigitalStage.org


Some interesting stuffs