Loops - HELP!

closed account (G2Eh0pDG)
Write a program that asks the user to input a positive number (declare an integer variable n). Your program should calculate the sum of the following alternating series. Note that the sign alternates between + and -. Also make sure that the divisions performed within the loop are not integer type divisions. Otherwise, sum of series will always be 1 for any n. For example, 1/2 is 0 in C++ since it is an integer division. So, you need to do static_cast<double> in the division operation. Display the sum in fixed, show point format with exactly 6 significant digits after the decimal point.

1 −1/2 + 1/3 − 1/4 + 1/5 − 1/6 + ⋯1/𝑛

I have no clue what algorithm I'm using in order to add and subtract following each other.

Can someone tell me what I can change in order to properly run this and add/subtract the sequence of divided numbers leading up to n?


-------------------------------------------------------------------------------



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
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
	int n;
	int num = 1;

	cout << "Input a positive integer." << endl;

	cin >> n;
	
	if (n > 0)
	{
		int result = 0;
		for (int count = num; count <= n; count++)
		{
			result +=n;
			cout<<setprecision(6)<<static_cast<double>(1/n)<<endl;
		}

	}
	else
	{
		cout << "Please input a positive integer." << endl;
	}

	system("pause");
	return 0;

}
Last edited on
What is the pattern of the series?
Even denominators are subtracted, while odd denominators are added.
So the test is simple. Determine if n is even or odd.
Hint: Use the modulo operator (%).

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
closed account (G2Eh0pDG)
Sorry, I fixed the format. Okay, I'll see if I can figure it out again.
closed account (G2Eh0pDG)
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
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
	int n;
	int num = 1;

	cout << "Input a positive integer." << endl;

	cin >> n;
	
	if (n > 0)
	{   for (int count = 1; count <= n; count++)
		{   term = 1/static_cast<double>(count); 
		    if (count%2 == 0)
		    {   cout << " - 1/" << count << "=";
		        result -= term;        
		    }
	        else 
		    {   cout << " + 1/" << count << "=";
		        result += term; 
		    }		    
			cout << showpoint << setprecision(6) << result << endl;
		}
	}
	else
	{
		cout << "Please input a positive integer." << endl;
	}

	system("pause");
	return 0;
}
Last edited on
You've got the right idea checking count for even or odd.

However, you're not computing each term. Each term is 1/count. Pay attention to what was stated in the problem and be careful to NOT do integer division. Incrementing and decrementing num makes no sense.

Line 23: You can't put a condition on an else clause. In fact, you don't need the condition whatsoever. If it's not even, then guess what, it must be odd.

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
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{   int n;
    double result = 0;
    double term;
    
	cout << "Input a positive integer." << endl;
	cin >> n;
	if (n > 0)
	{   for (int count = 1; count <= n; count++)
		{   term = 1/static_cast<double>(count); 
		    if (count%2 == 0)
		    {   cout << " - 1/" << count << "=";
		        result -= term;        
		    }
	        else 
		    {   cout << " + 1/" << count << "=";
		        result += term; 
		    }		    
			cout << showpoint << setprecision(6) << result << endl;
		}
	}
	else
	{   cout << "Please input a positive integer." << endl;
	}
	system("pause");
	return 0;
}
Input a positive integer.
5
 + 1/1=1.00000
 - 1/2=0.500000
 + 1/3=0.833333
 - 1/4=0.583333
 + 1/5=0.783333
Press any key to continue . . .


edit: revised code to compute term separately.
Last edited on
closed account (G2Eh0pDG)
So I am supposed to divide by the count, but I just couldn't figure out exactly what I was doing wrong.. I really do appreciate the help. So i had the modulus correct, it was just the math after that. -= and += divided by count.
I made a slight revision to the above code to compute the term at line 14, then subtract or add the term at lines 17 and 21.

This might make it a little clearer.
Topic archived. No new replies allowed.