Wrote program to sum terms, but can't get it to give me the correct summation...

I wrote this program to sum a user-defined number of terms for the series
1-1/2+1/3-1/4+1/5-...
But for some reason it's not working.
I have went line by line and everything makes since to me.
Can any body tell me where I went wrong?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Hannah Winsor, Uses loop to sum fractions, Last modification 3/15/2015
 This program uses a loop to sum a user-defined number of terms, n, in the series
 1-1/2+1/3-1/4+1/5-...
*/

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main()
{
//Declare variables
	int n;
	//float n;
	double s, t, sum1, sum2, sum;
//Output program description, and ask for number of terms	
	cout << "This program uses a loop to sum a user-defined number of terms, n, in the series"
	     << "1-1/2+1/3-1/4+1/5-..." << endl;
	cout << "Please indicate how many terms you would like in the summation?\n";
		cin >> n;
//For loop selected, most straightforward way to do the summation		
if (n%2 ==0) //For even number of selected terms
	{
		for(n; n>0; n-=2) //adds the first n terms
		{
		s=(-1.0)*1.0/n;  //All even terms
		t=1.0/(n-1.0); //All odd terms
		sum1+=s; // This sums all even terms, which are negative for this series, thus added separately
		sum2+=t; // This sums all odd terms, which are positive for this series	
		}
	}
else //(n%2==1), For odd number of selected terms
	{
		for(n; n>0; n-=2) //adds the first n terms
		{
		s=1.0/n;  //All odd terms
		t=(-1.0)*1.0/(n-1.0); //All even terms
		sum1+=s; // This sums all odd terms, which are positive for this series	
		sum2+=t; // This sums all even terms, which are negative for this series, thus added separately	
		}
	}

sum=1+sum1+sum2;
cout << "Using your indicated number of terms, your summation is " << setprecision(7) << sum;
	

	return 0;
}		


However now all my output is the same


This program uses a loop to sum......
1-1/2+1/3....
Please indicate how many terms....
(You can insert whatever term you like)
Using your indicated number of terms, your summation is -inf
Last edited on
Its a lot simpler if you use the formula
Eni(-1)(i+1)(1/i) = 1-1/2+1/3-1/4+1/5-...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int n;
	cout<<"Enter n: ";
	cin>>n;
	
	double sum = 0.0;
	
	for(int i = 1; i <= n; i++)
		sum += pow(-1,i+1)*(1.0/i);
	
	cout<<"sum of the first "<<n<<" terms is "<<sum<<endl;
	return 0;
}
Last edited on
Thanks a lot! That is awesome and works beautifully!

But I'm still not sure mine isn't working i even re-wrote it and it's still not summing correctly, do you know why?
Sorry I'm just still learning and the logic behind my program makes sense to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main()
{
//Declare variables
		double n, s, t, sum1=0, sum2=0, sum;
//Output program description, and ask for number of terms	
	cout << "This program uses a loop to sum a user-defined number of terms, n, in the series"
	     << "1-1/2+1/3-1/4+1/5-..." << endl;
	cout << "Please indicate how many terms you would like in the summation?\n";
		cin >> n;
//For loop selected, most straightforward way to do the summation		
for(double i=1; i<=n; i++)
{
	if (i%2 ==0) //For even number of selected terms
		{
		s=1.0/i;  //All even terms
		sum1+=s; // This sums all even terms, which are negative for this series, thus added separately
		}

    else //(i%2==1), For odd number of selected terms
		{
		t=1.0/i;  //All odd terms
		sum2+=t; // This sums all odd terms, which are positive for this series	
		}
	
}

sum=1-sum1+sum2; //If you subtract all the even terms and add all the odd terms, it will give you your approximation

cout << "Using your indicated number of terms, your summation is " << setprecision(7) << sum;

return 0
}
Last edited on
To solve a problem like this, it's often helpful to look at easy input. Consider what happens when n=1:
- The loop at line 16 runs once, setting t=1.0/1 and sum2=1.0
- Line 32 sets sum=1-0+1 = 2. Oops!

So you need to change line 32 to sum = sum2-sum1;
Topic archived. No new replies allowed.