Hello. Thanks in advance for helping a newbie. I am supposed to add a bottom loop (which I assume is a do while) that asks the user if he/she wishes to compute another series and as long as the answer is 'Y' or 'y' to iterate the loop until 'n' or 'N' is chosen. The loop works fine, but for some reason the answers I am supposed to get do not compute correctly. I am supposed to be getting less than 1 for every answer and after the first loop the number goes to 1.9 something. What am I doing wrong?

#include <iostream>
#include <cmath>
using namespace std;

int main()
{   
    int term, denom, finalDenom;
    double  sum = 0.0;
    char ag;
      
   cout << "Randy Levine \n";
   cout << endl;
      
  do
  { 
            cout << "What is the final term?";
            cin    >> term; 
           
           while (term < 2 || term > 10)
            {
            cout << "Invalid input. Please enter a number between 2 and 10.";
            cout << endl;
            cout << "What is the final term?";
            cin  >> term;     
            }
   
            finalDenom = static_cast<int>(pow (2.0, term));
    
   
      	
            for (denom = 2; denom <= finalDenom; denom *= 2)
   	        sum = sum + (1.0 / denom); 
    
   
            cout << "---------";
            cout << endl;
            cout << sum;
            cout << endl;
            cout << endl;
            
            cout << "Would you like to run another series? (Y/N): ";
            cin  >> ag;
            
            if (ag == 'N' || ag == 'n')
            break;
              
  } while (ag == 'Y' || ag == 'y');
         
    system ("pause");      
	return 0;
}

Dani AI

Generated

The jump to about 1.9 happens because the accumulator sum is never cleared between runs. double sum = 0.0; is set once at program start, so each new series gets added onto the previous total. As pointed out, resetting that accumulator at the start of each iteration (or declaring it inside the do block) fixes the problem.

A minimal, safe change is to put sum = 0.0; immediately after reading term (or at the top of the do block). Also make the loop variables local to the loop for clarity, for example for (int denom = 2; denom <= finalDenom; denom *= 2) and use braces so the body is explicit. The explicit break after checking ag is redundant when the loop condition already tests for Y/y.

For quick verification, the partial sum here is a finite geometric series with ratio 1/2; its closed form is 1 - 1/2^term. Expected outputs: term 5 -> 0.96875, term 6 -> 0.984375, term 8 -> 0.99609375, term 10 -> 0.9990234375. For integer powers of two, using a bit shift (1 << term) avoids floating results from pow and the need to cast.

Credit: fix idea from ; original report from . See Geometric series for the closed-form formula.

Recommended Answers

All 3 Replies

Oh...I forgot. I am supposed to be testing the terms 5, 6, 8, & 10. 5 works fine but then at 6 it seems to be adding the original loop with it or something weird, whatever it is doing.

Reset num back to 0..

while(blah..blah)
(
   num=0;
   //...
   num=num+(1.0/denom);
)

Cool! Thanks cikara21!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.