The purpose of this lab is to create a program that performs a simple computation of the value $\pi$.


thumb The value of $\pi$ can be computed according to the following formula: $\frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \ldots$ Write a C++ program to compute $\pi$. Because the formula is an infinite series, you wont be able to compute $\pi$ exactly. Compute it with enough terms to arrive at the value 3.19419. (This is an estimated value, reached at 19 terms). You don't need to use a loop, but just add in as many terms as you need by making the expression longer. You may need to modify and run your program several times until you get the correct answer.

Think before programming

You may think that you don't know how to complete this lab, but you actually do. Write down the steps you would take to compute $\pi$ with a pencil and paper, then translate this into computer instructions.

  1. Inputs
  2. Outputs

In this case there are no Inputs, other than running the program itself. The Output is the correct value of $\pi$ that you will be printing out.

Getting Help

If you get stuck:

  1. First work the problem out on a piece of paper. Don't ask for help too quickly and don't use a solution you find on the internet. We want you to build the ability to write programs on your own.
  2. Second ask the person sitting next to you. They'll feel good helping you and if they don't know you will learn together!
  3. As a last resort if you need help from a TA (and you are working in the lab), please use the help request queue:


The TA's love to help, but you will gain much more in learning how to solve your problems on your own!


  • Take note that you are given the formula for $\frac{\pi}{4}$, nor for $\pi$ itself.
  • Ensure you know the difference between integer division and floating point division.

I need more than this

Some of you have already done some programming and need an additional challenge. Read ahead to find out how to use loops, then make your program calculate $\pi$ using as many terms (how many iterations the loop completes, NOT how many significant digits are listed when printed) as you ask it to. You will probably want to use a variable to tell your program how many terms you want to include.

If you are really curious about how computers actually do floating-point math you can take a look at the paper “What Every Computer Scientist Should Know About Floating-Point Arithmetic” by David Goldberg. you can find a link to it at http://dl.acm.org/citation.cfm?id=103163

Pass-off procedure

When you have your program working, you will need to show it to a TA.

Put yourself in the help request queue to pass off: http://taohelpqueue.appspot.com/requesthelp/YPK364KMSA5U5QF55UCL8LQ3JHCS5C.

The TA will evaluate your code based on the following criteria:

  1. Did you print anything at all? (5 points)
  2. Did you print out the correct value for $\pi$? (10 points)
  3. Did you format your code as shown in the book and in class, with separate lines and indenting. (5 points)

Extra credit: Did you find out how to print out $\pi$ with as many iterations (of your loop, NOT calculating pie to a certain decimal place) as needed? (1 point)

Electronic Submission

WHEN YOU PASS OFF, you will need to submit your code. To do this follow the instructions below:

  1. On your computer, navigate to the directory containing your Visual C++ project (clue: look at the top bar in Visual C++).
  2. Find the .cpp files for the classes you have written.
  3. Attach these files to an email and send them to


If you have trouble, ask a neighbor or a TA.

You can save the files to the desktop and then attach them to an email.

An example pass-off form is shown below for this lab. You will find printed pass-off forms in 1119 TMCB.


cs-142/pi.txt · Last modified: 2015/01/07 08:53 by ryancha
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0