Lab Assignment #4

Due Thursday, July 31 at the beginning of class

The assignment is to write a C++ program that approximates the value of p using the algorithm described below. The algorithm calculates p by approximating the area of a circular arc. The value of p is approximately 3.14159265358979.

The Circle Method
Consider the quarter circle

which has a radius r equal to 2. From the formula for the area of a circle:

area =

you can easily determine that the area of the quarter circle is p square inches. You can also approximate the area computationally by adding up the areas of a series of rectangles. Each rectangle has a fixed width and the height is chosen so that the circle passes through the midpoint of the top of the rectangle. For example:

Detailed picture

For each rectangle, the width w is the same, derived by dividing the radius of the circle by the number of rectangles. The height h, on the other hand, varies depending on the position of the rectangle. Rectangles closer to the center of the circle will be taller than those near the edge. If the midpoint of the rectangle in the horizontal direction is given by x, then the height of the rectangle can be computed using the distance formula:

The sum of the areas of the rectangles provides an approximation to the area of the quarter circle, hence, it is also an approximation of p . The more rectangles there are, the closer the approximation.

For this assignment you are to write a program to compute the area of the quarter circle by dividing it into 1000 rectangles. Your program should output the following:

The value of pi is X.XXXX

where X.XXXX is the approximate value of p with 4 decimal places of accuracy.

This assignment exercises your knowledge of repetition statements. As in all programs you write, you must use meaningful identifier names, and the code must be well commented and consistently formatted according to the style guide. Your functions must have appropriate declarations and the header comments must explain the purpose of the function, and describe any inputs and/or outputs.

 

Extra Credit

Leibniz's Method
There is another algorithm that approximates p using a formula derived by Gottfried Leibniz, also know as "the father of calculus." This method uses an infinite series of additions and subtractions to approximate p :

p/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + . . .

Notice that this approximates p/4. After the summation of the terms, you need to multiply it by 4 to arrive at the final approximation. Analogous to the previous method, the more terms in the series, the closer the approximation of p .

Program Specifications (please read carefully)

The result of this extra credit exercise is to create a table of approximations for p , comparing the two algorithms, the circle-arc method on the previous page, and Leibniz's algorithm described above. The table will look like this:


Iterations       Circle Method       Leibniz Method
---------------------------------------------------
         1        3.4641016151        4.0000000000
        10        3.1524114333        3.0418396189
       100        
      1000        
     10000          (fill in rest of the table)
    100000        
   1000000        

Each algorithm is executed 7 seven times. The first execution only iterates each algorithm once. The second run iterates each algorithm 10 times. The third run iterates 100 times, the fourth run iterates 1000 times, etc. The 7th run iterates 1,000,000 times. You can see how the approximations get closer to the actual value as the number of iterations increases. Since p is irrational and transcendental, there is no exact answer.

You will write two functions, each will approximate p using a different algorithm. The first function will be called computeCirclePI, and will implement the first algorithm. The second function will be called computeLeibnizPI, and it will implement the second algorithm.

The function computeCirclePI must use a for loop, and the function computeLeibnizPI must use a while loop.

In order to receive any extra credit, you must follow these specifications. Also, I look for solutions that are not implemented using "brute force." If you opt to do the extra credit assignment, you must do it all. I won't accept a partial implementation of this as "extra credit" work.

If you have any questions about the extra credit work, please ask!

How to submit your programs

Back to Outline