softengstu - 1 year ago 80

C++ Question

This program is supposed to add 1/2^1 +1/2^2 + 1/2^3....1/2^n (user enters nth power). It should display the fractions (1/2 + 1/4 + 1/8....) then find their sum and display the total sum at the end (ex: 1/2 + 1/4 + 1/8 = .125)

It works when the user enters 5, but any other number displays the wrong total. I get sum greater than 1, which is incorrect. How can I fix this?

`#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;

}

Answer Source

Your iterations counter is not `denom`

, it is `nthTerm`

. Therefore, your if else statements should be checking `finalDenom`

against the `nthTerm`

and not `denom`

.

Also, you are seeing results above 1 because: You probably tested your code in continues executions (using your do while loop) without resetting your `sum`

variable to zero on each execution.

Also as user Jean mentioned in the comment, `sum += 1.0/denom;`

is enough.
This should work:

```
#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
if (nthTerm == finalDenom)
cout << " = ";
sum += 1.0 / denom;
}
cout << sum << endl << endl << endl;
cout << "Do you wish to compute another series? ";
cin >> repeat;
repeat = toupper(repeat);
} while (repeat == 'Y');
return 0;
}
```

Result:

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