Lab Assignment 4

You are to write the following program. In addition, there are a series of questions to be answered. Be sure to do those as well. Once you are finished, you are to do the following...
  1. Print out a copy of your source code for the program, complete with your name, lab assignment number (and everything else defined in the coding style we covered), and submit the printout to either Kevin or myself before you leave.
  2. Print out your answers to the questions below and submit a copy of those as well.
  3. Submit a copy of your program source code electronically. You may do this either via email (to ryan.flannery@gmail.com), or using BlackBoard.



Approximating π

As many of you may know, π is an infinitely repeating decimal number that appears frequently in mathematics. The value of π accurate to 50 decimal places is
3.14159265358979323846264338327950288419716939937510
There are many equations that exist telling us the precise value of π. Most are rather complicated or involve calculus that some of you may or may not have had yet.

One method for calculating π is called the Gregory-Leibniz series, which looks like the following...




(Image from the Wikipedia entry for "Leibniz_formula_for_pi".)

A slightly easier-to-understand version of this is the following...




(Image from the Wikipedia entry for "Pi".)

We will use the above version to write a program to approximate π with various degrees of precision. Call each fraction on the right of the equals sign a segment.

Obviously, we can't calculate π exactly, since it has infinitely many segments. As such, you will first prompt the user for a number that indicates how many segments we will use to approximate π

Your program should proceed as follows
  1. Prompt the user for a number (positive integer) that will indicate how many segments to use in the approximation of π
  2. Note that the number of segments must be greater than or equal to 1, and a whole number. If the user enters 0 or some negative number, alert them that their input is invalid (provide an appropriate explanation) and keep prompting them until they enter a valid value.
  3. Once the user has input such a number, say n, you will then use the Gregory-Leibniz series with n segments to calculate an approximation of π
  4. Once you have the approximate value of π, output it.


Answer the Following Questions

  1. Report the approximate value of π your program computes for 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 200, and 300 segments.

  2. How many segments do you need to get an approximation where the first two numbers after the decimal place are correct (i.e. 3.14)?

  3. How many segments do you need to get an approximation where the first three numbers after the decimal place are correct (i.e. 3.141)?

  4. How many segments do you need to get an approximation where the first five numbers after the decimal place are correct (i.e. 3.14159)?

  5. How accurate of an approximation can you achieve? I.e. how many decimal places can you correctly calculate?

  6. If you can do anything to your program to increase the accuracy of your approximation, do so. Report what you did and how accurate you were able to get your approximation.