0

An approximate value of pi can be calculated using the series given below:

pi = 4 * [ 1 - 1/3 + 1/5 - 1/7 + 1/9 … + ((-1)^n)/(2n + 1) ]

write a C++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of terms in the approximation of the value of pi and outputs the approximation. Include a loop that allows the user to repeat this calculation for new values n until the user says she or he wants to end the program.

The expect result is: Enter the number of terms to approximate (or zero to quit): 1 The approximation is 4.00 using 1 term. Enter the number of terms to approximate (or zero to quit): 2 The approximation is 2.67 using 2 terms. Enter the number of terms to approximate (or zero to quit): 0

I can get the correct result now but I don't know how to Include a loop that allows the user to repeat this calculation for new values n until the user says she or he wants to end the program.

#include <stdio.h> #include <iostream> #include <cmath> using namespace std; int main() { int n; double pi, sum; do { cout << "Enter the number of terms to approximate (or zero to quit):" << endl; cin >> n; if (n >0) { double sum=1.0; int sign=-1; for (int i=1; i <=n; i++) { sum += sign/(2.0*i+1.0); sign=-sign; } pi=4.0*sum; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "The approximation is " << pi << " using "<< n << " terms" << endl; } } while(n>0); cout << endl; return 0; } 
6
  • Note: Stop declaring variables up front . Declare them right before/right at usage. You mix that. Commented Jul 2, 2016 at 19:52
  • maybe loop int i should equal 0. for (int i=0; i <=n; i++) Commented Jul 2, 2016 at 20:28
  • sum += (double)sign/(2.0*i+1.0); Commented Jul 2, 2016 at 20:31
  • sign *=-1; -> sign=-sign would be better Commented Jul 2, 2016 at 20:37
  • You are losing the first term. Commented Jul 2, 2016 at 21:30

2 Answers 2

1

You have wrong initialization:

double sum=0.0; int sign=1; 

It should be

double sum = 1.0; int sign = -1; 

The loop is also wrong (has a typo?), it should be

for (int i = 1; i < n; i++) { /* please, notice "i < n" and "{" */ sum += sign / (2.0 * i + 1.0); sign = -sign; /* more readable, IMHO than sign *=-1; */ } pi = 4.0 * sum; /* you can well move it out of the loop */ 

EDIT if you want to repeat calculation a common practice is extrating a function (do not cram everything into single main):

double compute(int n) { if (n < 0) return 0.0; double sum = 1.0; int sign = -1; for (int i = 1; i < n; i++) { sum += sign / (2.0 * i + 1.0); sign = -sign; /* more readable, IMHO than sign *=-1; */ } return 4.0 * sum; } 

EDIT 2 the main function could be something like this:

int main() { int n = -1; /* quit on zero */ while (n != 0) { cout << "Enter the number of terms to approximate (or zero to quit):" << endl; cin >> n; if (n > 0) cout << "The approximation is " << compute(n) << " using "<< n << " terms" << endl; } } 
Sign up to request clarification or add additional context in comments.

8 Comments

when I input one, the expect result should be 4.00 not 2.66667.
@ZiWei Pan: then change i <= n to i < n in the loop condition (see my edit)
@ Dmitry Bychenko: thx! how can I make the loop to repeat the calculation?
@ZiWei Pan: if you want to repeat calculation, extract a function (see my edit)
@ Dmitry Bychenko: I am not very understand your code. I have already updated my code and my question. I just not sure how to use it in my code.
|
0

The alternating sign must be part of your loop. Include it into the loop body by using a compound statement:

for (int i=1; i <=n; i++) { sum += sign/(2.0*i+1.0); sign *=-1; } 

1 Comment

I have updated my code. I don't know why my code cannot repeat.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.