C++ 如何发现金额计算中的错误
我试图完成一项计算金额的任务。任务为Sn=(cosx/1)+((cosx+cos2x)/2)+…+((cosx+…+cosxn)/n);x-浮点,n-整数。 但是程序在任何情况下都输出零C++ 如何发现金额计算中的错误,c++,math,C++,Math,我试图完成一项计算金额的任务。任务为Sn=(cosx/1)+((cosx+cos2x)/2)+…+((cosx+…+cosxn)/n);x-浮点,n-整数。 但是程序在任何情况下都输出零 其C++代码写在DeVC++ 5.11上。我几乎完成了代码,但我找不到错误 float funct(float x, float s, int n) { if (n < 1) { cout << s; return 0; } for (i
其C++代码写在DeVC++ 5.11上。我几乎完成了代码,但我找不到错误
float funct(float x, float s, int n)
{
if (n < 1) {
cout << s;
return 0;
}
for (int i = n; i < 1; i--) {
float a = (cos(i * x)) / n;
s = s + a;
}
return funct(x, s, n - 1);
}
int main(void)
{
float x = 1, s = 0;
int n;
cin >> n;
funct(x, s, n);
}
float函数(float x,float s,int n)
{
if(n<1){
cout>n;
函数(x,s,n);
}
我希望输出类似于可数浮点数,但实际输出为零。for(int I=n;I<1;I--)
for (int i = n; i < 1; i--)
应该是
for (int i = 1; i <= n; i++)
for(int i=1;我已经用调试器完成了吗?for(int i=n;i不应该通过引用传递)或者i>1
如果你想倒计时到基本情况。