calculating pi

i need help creating a program to calculate pi, been at it all weekend, i am stumped man
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
50
51
52
53
54
55
56
57
58
59
60
61
// Program Template

#include <iostream>
using namespace std;

//Function Prototypes
bool runAgain(void);

double piCalc(int n);

int main() {

	int n = 10;
	

	do {

		while (n <= 100000) {
			cout << "calculatePi(" << n << ")" << " : " <<  piCalc(n) << endl; 

			n = n + 10;
		}
		
		
	} while (runAgain());
	cout << "Pi:" << piCalc(n);
	return(0);
}

// Function Implementation
bool runAgain(void) {
	char userResponse;

	cout << "\nWould you like to run again (y or n): ";
	cin >> userResponse;
	cin.ignore(); // to clean up the input stream

	if (userResponse == 'y' || userResponse == 'Y')
		return(true);

	return(false);
}
double piCalc(int n)
{

	double sum = 4.0;
	double pi;
	

		for (int i = 1; i <  n; i++) {
			
			i = pow(-1, i);
			pi = 4.0 * (i) / (2.0 * i + 1.0);
			
			sum = sum - pi;
			
		
			return sum;

		}
}
Your piCalc function makes no sense. You have a loop, but the very first time through the loop the function returns. It's a loop that never loops.
desk check, debugger step-by-step, diagram flow...
50
51
52
53
		for (int i = 1; i <  n; i++) {
			//...
			return sum; //this is inside the loop
		}


also, ¿what's the purpose of `i'? because you seem to use it as a counter for the loop, and then you go and do i = pow(-1, i); resetting it.
Topic archived. No new replies allowed.