lost in the homework sauce

Mar 25, 2016 at 6:30pm
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 Mar 26, 2016 at 3:39pm
Mar 25, 2016 at 6:32pm
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.
Mar 25, 2016 at 6:34pm
In C++, an int divided by an int is an int.

For example, 1/5 gives 0.
Mar 26, 2016 at 3:43pm

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;
}
Mar 26, 2016 at 10:21pm
¿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 Mar 26, 2016 at 10:24pm
Mar 26, 2016 at 10:36pm
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.
Mar 27, 2016 at 1:35am
let the OP explain themselves.
Mar 30, 2016 at 1:41am
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?
Mar 31, 2016 at 9:13am
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/

Mar 31, 2016 at 11:16am
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.