【发布时间】:2019-04-01 19:26:24
【问题描述】:
我在我的代码中找不到逻辑错误。 整个事情在数学上是错误的吗? 还是部分正确。
float x,term,fx;
int i, nterms;
cin>>x>>nterms;
for(i=1;i<=nterms;i+=2){
term=1;
fact=1;
for(j=1;j<=i;j++){
term=term*x;
fact=fact*j;
}
sign=-1*sign;
fx+=sign*term/fact;
}
cout << fixed << showpoint;
cout << setprecision(6);
cout<<fx;
【问题讨论】:
-
欢迎来到 Stack Overflow。请阅读the help pages、the SO tour、阅读how to ask good questions,以及this question checklist。最后学习如何创建Minimal, Complete, and Verifiable Example。
-
一个可能的提示:未初始化的局部变量真的是未初始化的。它们将有一个 indeterminate(并且看似随机)的值。以任何方式使用该不确定值会导致undefined behavior。现在想想你的变量,以及你如何使用它们。
-
哦,你一定要花点时间去learn how to debug your programs。
标签: c++