Tuesday, 15 February 2011

Problems with cout ( C++) -



Problems with cout ( C++) -

i having hardest time figuring out wrong here:

#include <iostream> #include <cmath> #include <iomanip> using namespace std; double fact(double); double sintaylor(double); double costaylor(double); int main() { double number, sineofnumber, cosineofnumber; cout << "enter number, calculate sine , cosine of number" << endl; cin >> number; sineofnumber = sintaylor(number); cosineofnumber = costaylor(number); cout << fixed << endl; cout << cosineofnumber << endl; cout << sineofnumber << endl; homecoming 0; } double fact(double n) { double product = 1; while(n > 1) product *= n--; homecoming product; } double sintaylor(double x) { double currentiteration, sumsine; for(double n = 0; n < 5; n++) { currentiteration = pow(-1, n)*pow(x, 2*n+1) / fact(2*n+1); sumsine += currentiteration; } homecoming sumsine; } double costaylor(double y) { double currentiteration, sumcosine; for(double n = 0; n < 5; n++) { double currentiteration = pow(-1, n)*pow(y, 2*n) / fact(2*n); sumcosine += currentiteration; } homecoming sumcosine; }

ok, here's code. i'm pretty content it. except 1 thing: sineofnumber , cosofnumber, after calling of sintaylor , costaylor, add together each other in next cout line print each other. in other words, if number equal lets say, .7853, 1.14 printed in line intended print cosineofnumber, , sineofnumber print result normally. can help me identify why is? give thanks much!

are ever initializing variables sumsine , sumcosine in functions? they're not guaranteed start @ zero, when phone call += within loop adding computed values garbage.

try initializing 2 variables 0 , see happens, other code seems okay.

c++ cout

No comments:

Post a Comment