lost in the homework sauce

My professor says there is a bug in my program but I cant figure it out. could anybody hint at what this bug is that is so obvious.

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 <iostream>
#include <iomanip>

using namespace std;

int main()
{
	double pi= 0;
	long i;
	long n;

	cout << fixed << showpoint << setprecision(2);
	
	cout << "Enter the value of n: ";
		cin >> n;
		cout << endl;

	for (i = 0; i < n; i++)
	{
		

		if (i % 2 == 0)
			pi = pi + (1 / (2 * i + 1));

		else
			pi = pi - (1 / (2 * i + 1));
	}

	cout << "pi = " << pi << endl;

	return 0;
}
Last edited on
Line 23, 26: You're doing integer arithmetic.

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.
In C++, an int divided by an int is an int.

For example, 1/5 gives 0.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	double pi= 0;
	long i;
	long n;

	cout << fixed << showpoint << setprecision(2);
	
	cout << "Enter the value of n: ";
		cin >> n;
		cout << endl;

                pi = 0;
                
	for (i = 0; i < n; i++)
	{
		

		if (i % 2 == 0)
			pi = pi + (1 % (2 * i + 1));

		else
			pi = pi - (1 % (2 * i + 1));
	}

	cout << "pi = " << pi << endl;

               pi = 4 * pi;

	return 0;
}
¿what's the purpose of your program?
¿what are the symptoms of the error?
¿did you at least try to run it?

% is the modulo (o remainder) operation
A = C*B + R
if A and B are integers > 0, then / will give you C and % will give you R.


1
2
cout << "pi = " << pi << endl;
pi = 4 * pi;
so, it wasn't pi then...
Last edited on
This is an attempt to approximate pi by adding a series of values.

OP ignored the hint he was given ( don't divide an int by an int ) and has now randomly experimented with the funny looking % operator.
let the OP explain themselves.
I'm still not picking up on the equation. I understand the use of the modulus and the int /int = 0, but what's eluding me is the proper form for the correct equation. Really I don't know why the program uses "i". Is that supposed to be representing an imaginary number?
If I had to guess, you're trying to implement this:

https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80

I've no idea why you introduced modulus to lines 25 and 28.

i is not the imaginary number. It's a way to understand how many times you've gone round a loop. You should stop what you're doing and learn about the "for loop", because if you don't understand it you've got no chance of doing this. It's discussed in the middle of this page: http://www.cplusplus.com/doc/tutorial/control/

I understand the use of the modulus and the int /int = 0, but what's eluding me is the proper form for the correct equation.
Assuming that the formula is right, you can fix the integer division by ensuring that one side of the division is a double. That's easily done by changing 1 to 1.0:
1
2
3
4
		if (i % 2 == 0)
			pi = pi + (1.0 / (2 * i + 1));
		else
			pi = pi - (1.0 / (2 * i + 1));
Topic archived. No new replies allowed.