该程序应该添加1/2 ^ 1 + 1/2 ^ 2 + 1/2 ^ 3 .... 1/2 ^ n(用户输入n次幂)。它应显示分数(1/2 + 1/4 + 1/8 ....)然后找到它们的总和并显示最后的总和(例如:1/2 + 1/4 + 1/8 =。 125) 它在用户输入5时有效,但任何其他数字显示错误的总数。我得到的总和大于1,这是不正确的。我该如何解决这个问题?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int denom, // Denominator of a particular term
finalDenom, // Denominator of the final term
nthTerm; // Nth term run
double sum = 0.0; // Accumulator that adds up all terms in the series
char repeat;
do
{
cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n\n";
cout << "What should n be in the final term (between numbers 2 and 10)? ";
cin >> finalDenom;
nthTerm = 0;
for (denom = 2; nthTerm <= (finalDenom - 1); denom *= 2)
{
cout << "1/" << denom;
++nthTerm;
if (denom != finalDenom)
{
cout << " + ";
}
else if (denom == finalDenom)
{
cout << " = ";
}
sum += pow(denom, -1);
}
cout << sum << endl << endl << endl;
cout << "Do you wish to compute another series? ";
cin >> repeat;
repeat = toupper(repeat);
} while ((repeat == 'Y'));
return 0;
}
答案 0 :(得分:2)
答案 1 :(得分:0)
您的迭代计数器不是denom
,而是nthTerm
。因此,您的if else语句应针对finalDenom
而非nthTerm
检查denom
。
此外,您看到的结果大于1,原因是:您可能在继续执行中测试了代码(使用do while循环),而不会在每次执行时将sum
变量重置为零。
同样在评论中提到的用户Jean,sum += 1.0/denom;
就足够了。
这应该有效:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int denom, // Denominator of a particular term
finalDenom, // Denominator of the final term
nthTerm; // Nth term run
double sum = 0.0; // Accumulator that adds up all terms in the series
char repeat;
do
{
cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n\n";
cout << "What should n be in the final term (between numbers 2 and 10)? ";
cin >> finalDenom;
sum = 0;
nthTerm = 0;
for (denom = 2; nthTerm < finalDenom; denom *= 2)
{
cout << "1/" << denom;
++nthTerm;
if (nthTerm != finalDenom)
cout << " + ";
else
cout << " = ";
sum += 1.0 / denom;
}
cout << sum << endl << endl;
cout << "Do you wish to compute another series? ";
cin >> repeat;
repeat = toupper(repeat);
} while (repeat == 'Y');
return 0;
}
结果:
This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n What should n be in the final term (between numbers 2 and 10)? 6 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 = 0.984375 Do you wish to compute another series? y This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n What should n be in the final term (between numbers 2 and 10)? 5 1/2 + 1/4 + 1/8 + 1/16 + 1/32 = 0.96875 Do you wish to compute another series? y This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n What should n be in the final term (between numbers 2 and 10)? 4 1/2 + 1/4 + 1/8 + 1/16 = 0.9375 Do you wish to compute another series? y This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n What should n be in the final term (between numbers 2 and 10)? 3 1/2 + 1/4 + 1/8 = 0.875 Do you wish to compute another series? n
另外请注意,您应该添加一些用户输入程序的验证,至少最终用语在[2,10]范围内。
答案 2 :(得分:0)
如上所述,你的问题在于传导。但是我会将它完全抛弃。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int denom, // Denominator of a particular term
finalDenom, // Denominator of the final term
nthTerm; // Nth term run
double sum = 0.0; // Accumulator that adds up all terms in the series
char repeat;
do
{
cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n\n";
cout << "What should n be in the final term (between numbers 2 and 10)? ";
cin >> finalDenom;
denom=2;
for (nthTerm = 0; nthTerm < finalDenom; nthTerm++)
{
denom*=2;
cout << "1/" << denom;
cout << " + ";
sum += pow(denom, -1);
}
cout << " = ";
cout << sum << endl << endl << endl;
cout << "Do you wish to compute another series? ";
cin >> repeat;
repeat = toupper(repeat);
} while ((repeat == 'Y'));
return 0;
}
输出:
This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n
What should n be in the final term (between numbers 2 and 10)? 1
1/4 + = 0.25
Do you wish to compute another series? y
This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n
What should n be in the final term (between numbers 2 and 10)? 2
1/4 + 1/8 + = 0.625
Do you wish to compute another series? y
This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n
What should n be in the final term (between numbers 2 and 10)? 4
1/4 + 1/8 + 1/16 + 1/32 + = 1.09375
Do you wish to compute another series? n
Exit code: 0 (normal program termination)
答案 3 :(得分:-1)
我相信它更容易获得值Something
。每个循环的一半,或somthing = 1/pow(2,n)
(1除以2的幂)。总和为1 - something
。更少的代码总是很好。 :)
另一项改进是:
for (denom = 2; nthTerm < finalDenom; denom *= 2)
{
cout << "1/" << denom;
++nthTerm;
cout << " + ";
sum += 1.0 / denom;
}
Cout << "=";
对此代码进行细微调整
所以我的代码将是
for (denom = 2; nthTerm < finalDenom; denom *= 2)
{
cout << "1/" << denom;
++nthTerm;
cout << " + ";
}
Cout << "=";
Sum = 1 - (pow(2,finalDenom));
如果您认为这是一个错误的答案,请解释原因,以便我可以改进我的答案。