2016-09-19 433 views
-2

該程序應該添加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,這是不正確的。我怎樣才能解決這個問題?C++如何計算總分數的總和?

#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; 
} 
+4

歡迎堆棧溢出!這聽起來像你可能需要學習如何使用調試器來遍歷代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。進一步閱讀:** [如何調試小程序](http:// ericlippert。com/2014/03/05/how-to-debug-small-programs /)** –

+0

'和'sum + = pow(denom,-1);'太多了:只要做'sum + = 1.0/denom ;' –

+0

@BaummitAugen:對!我讀** 2,但不能在C中完成。對不起...對不起...... –

回答

0

你迭代計數器不是denom,它是nthTerm。因此,您的if else語句應該針對nthTerm而不是denom檢查finalDenom

此外,您看到的結果高於1,因爲:您可能在繼續執行(使用do while循環)中測試了代碼,而無需在每次執行時將sum變量重置爲零。

也作爲用戶讓在評論中提到,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]。

+1

不要發佈文本結果的圖像。您可以輕鬆地複製和粘貼文本到答案。 – 1201ProgramAlarm

+0

謝謝,那工作 – softengstu

+0

@ 1201ProgramAlarm我試過這看起來很亂,我會爲你做:) –

-1

我相信它更容易有一個值Something。每循環一半,或somthing = 1/pow(2,n)(1除以n的冪)。然後總和是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)); 

如果你覺得這個保險業監督不好回答,解釋爲什麼我可以改進我的答案。

+0

'^'並不意味着在C++ – Barry

+0

指數我知道。但這很容易解決。我沒有時間去查看功能 – Kriso

+0

現在修復我認爲 – Kriso

2

我認爲你的方法效率低下,不準確。這是一個幾何級數。這個序列的總和可以用單個表達式找到。

enter image description here

只是替代R 2與該式中1/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)