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.
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...