ASSIGNMENT 1
Emanuele Trucco .
1. THE PROBLEM
Calculating the value of ?, the ratio between circumference and diameter,
has been a classic problem of mathematics throughout history.
One of the many formulae proposed in modern times is an infinite series created in 1699 by the
English mathematician Abraham Sharp:
Write a program which computes and prints Sharp’s approximation of pi using a number of terms to be
input from the keyboard (via scanf ). The program must also print the difference between the
approximation and the value of pi stored in the macro constant M_PI, defined in .
To help you in this first assignment, here is a sketchy pseudo-code from which you can develop your
code. The variable estimate is an accumulator, that is, a variable to which you keep adding iteratively.
We also use a variable pow3 in which we build increasing powers of 3 through the iterations, and a
variable sign which takes care of the alternating plus and minus.
1. Initialize: estimate = 1; pow3 = 1;
2. Input number of iterations, N
3. For (k=1 to N, step 1) {
4. if k is even, sign = 1; else sign = -1;
5. pow3 = pow3*3;
6. add k-th term to estimate, using sign and pow3;
7. }
8. Print estimate and (PI-estimate)
To decide whether k is even or odd, use the remainder function of C. Its operator is the percentage
sign, %: for example, 11%3 evaluates to 2, as 3*3=9 and 11-9 = 2. So, if val%2 is null, then val
is even.