The homework question said to rearrange statements and remove part of code that made it calculate incorrectly. I'm lost on this and have been searching multiple places and trying different things. This is a beginning C++ class, too, so, for homework in the first half I can't very well be going into complicated strings of advanced coding and pretend I'm that great when I can't even do this. I'd mostly appreciate hints and nudges toward understanding it, but straight answers suffice, too.

#include <iostream>
#include <iomanip>
using namespace std;


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

	cout << "Enter the value of n: ";
	cin >> n;
	cout << endl;

	for (i = 1; i < n; i++ )
		pi = 4 * pi;

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

	cout << endl << "Pi: " << pi << endl;

	return 0;
}

I believe that the algorithm that you are supposed to arrive to is this:
[TEX]
\pi = 4\sum^\infty_{k=0} \frac{(-1)^k}{2k+1} = \frac{4}{1}-\frac{4}{3}+\frac{4}{5}-\frac{4}{7}+\frac{4}{9}-\cdots
[/TEX]

Ask yourself:

What should be the starting value of pi?

What should be inside the for-loop?

Is the line "pi = 4 * pi" really correct?


Try to compute the above formula with a pen and paper, detailing the steps, and the algorithm to implement will be pretty clear as you see the recurring patterns in the calculation of the different terms.

I wouldn't know where to begin in regards to doing this in pseudocode. I've really been a bit lost during this course and have been trying to make sense of this. I understand functions, their uses, how to call and return values, but many times can't figure out how to write my own or figure out why one is written the way it is (unless highly simple such as larger (x, y).

As for the starting value of pi, since the program's goal is to calculate the value of pi, I would think it couldn't start as 3.14. So, initializing it to 0 seems the only way to start it off to me.

The exercise lists the series for calculating pi as follows:

[IMG]http://i.imgur.com/xbQ3S.jpg[/IMG]

Attachments picalcseries.JPG 10.37 KB

We aren't asking you if you know how to program or write psuedocode. We're asking if you can calculate pi. Sitting at your desk. Without a computer.

Interesting. I really don't know how to program something I simply do not understand. If you don't have the formula (or the steps), IMO it's impossible to program it.

Well then! I suppose I'll look up the necessary formula and steps for pi in order to better understand. Thank you for your time and help.

This article has been dead for over six months. Start a new discussion instead.