I am trying to write a summation to aprox. pi.

I am using a for loop and I cannot figure out where I am messing it up.
I keep getting "program has exited with code 0"

for(int N = 0; N <= 15; N++)
{double x9 = 0.0;

for(int n = 0; n <= N; n++)
{ x9 = x9 + pow(double(-1), n)*(4/(2*n +1));
	
	
		cout<<x9<<endl;

this is my little bit of code where I cant figure it out. I looked and looked around and could not find any info on where i may have a problem. Any help would be appreciated. Thanks

int N=0, x9=0;
	for(N; N <= 15; N++)
		double x9 = 0.0;
	for(int n = 0; n <= N; n++)
		x9 = x9 + pow(double(-1), n)*(4/(2*n +1));
	cout<<x9<<endl;

This gives me the answer of '3', so it works.

Not entirely sure what I did, but the solution is not 0, which you said is what you kept getting. Hope this helps. (whatever I did.) Also not sure what this formula is attempting to achieve, which may have helped me come to a solution. :P

Thanks. I will try to figure it out from there. I just needed a little boost on it cause it was getting frustrating. I did similair to what you did and got 3 also. its a summation of the formula

pow(double(-1),n)*(4/(2*n +1))

so plug 0 then n++ to 15 starting at 4-4/3+4/5-4/7 etc..
and the answer is not 3.. its like 3.2

Thanks. I will try to figure it out from there. I just needed a little boost on it cause it was getting frustrating. I did similair to what you did and got 3 also. its a summation of the formula

pow(double(-1),n)*(4/(2*n +1))

so plug 0 then n++ to 15 starting at 4-4/3+4/5-4/7 etc..
and the answer is not 3.. its like 3.2

Ahh, all right..that would be because I declared the variables as integers instead of float or double. Minor error. :)

Thanks. I will try to figure it out from there. I just needed a little boost on it cause it was getting frustrating. I did similair to what you did and got 3 also. its a summation of the formula

pow(double(-1),n)*(4/(2*n +1))

so plug 0 then n++ to 15 starting at 4-4/3+4/5-4/7 etc..
and the answer is not 3.. its like 3.2

(4/(2*n +1)) is an integer division, so every term from 4/5 evaluates to 0. Try (4.0/(2*n +1)) .

I keep getting "program has exited with code 0"

This is referring to value sent back via the "return 0;" at the end of main not to any result in your program. In the past (and probably still currently)people have used different error codes to signify different events causing the program to exit this is why the IDE is reporting this to you.

(4/(2*n +1)) is an integer division, so every term from 4/5 evaluates to 0. Try (4.0/(2*n +1)) .

thanks alot that 4 did it and I should have noticed that but owell.

here is what I did which seems to work, but is something different from what I thought I would need.

double x9 = 0.0;

	for(int n = 0;n<=15;n++)

		x9=x9 + pow(double(-1),n)*(4.0/(2*n +1));
	
cout.precision(5);	
cout<<x9<<endl;

how can i make it output each value before it does its final evaluation?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.