CS 205 ­ Programming for the Sciences Spring 2011 ­ Programming Assignment 4 20 points Out: February 11, 2011

advertisement
CS 205 ­ Programming for the Sciences
Spring 2011 ­ Programming Assignment 4
20 points
Out: February 11, 2011
Due: February 16, 2011 (Wednesday)
The purpose of this assignment is to continue practicing creating C# programs using MS Visual Studio. Please name the project files and program files exactly as given in the assignment, and please write your name in a comment at the top of the program file. Problem Statement
Several irrational mathematical constants can be approximated using a series approximation. For example, 
can be approximated using:
−1k
1 1 1 1 1
=4 ∑
=4 −  −  
1 3 5 7 9
k= 0 2 k1
∞
We would like a program that will repeatedly ask the user the number of terms she would like to be used in computing an approximation of  using this series. When the user inputs 0, the program should exit.
The program output might look like:
Enter the number of terms for this approximation of PI: 10
10 terms give an approximate value for PI of 3.232316.
Enter the number of terms for this approximation of PI: 100
100 terms give an approximate value for PI of 3.151493.
Enter the number of terms for this approximation of PI: 1000
1000 terms give an approximate value for PI of 3.142592.
Enter the number of terms for this approximation of PI: 0
Good­bye!
Assignment
Create a C# console project named ApproximatePiProgram. Rename file "Program.cs" to "ApproximatePi.cs" (and the class "Program" to "ApproximatePi").
Write a C# program that implements a solution to this problem. It must meet the following specifications:
It must define a method GetInput. This method prompts the user for the number of terms for the approximation, and reads this input from the console. It passes back this data to the method caller in numeric form an integer.
● It must define a method ComputePI. This method receives the number of terms in the approximation. It computes an approximation of  using the above series for the number of terms ●
02/10/2011
Page 1 of 2
D. Hwang
received and returns this approximation. This method must be implemented using either a while­
loop or a for­loop. Note that the terms alternate sign. There are numerous ways to handle this, but you should choose one that does not use Math.Pow (­1,k). Reminder: to get real division, at least one of the operands must be a real number.
● It must define a method DisplayResults. This method displays the number of terms in the approximation and the computed approximation as shown above. I.e., this method will receive an integer and a real number. This method does not return a value The approximation should be formatted in fixed notation with a precision of 6.
● To complete this assignment, the Main method must solve this problem in the following way. It must consist of a do­while loop. The body of the do­while loop should do the following: 1) call the GetInput method to get the user's input, 2) if the user entered a value greater than 0, do the approximation computation and display the results by calling the other two methods. The loop condition will be to test whether the user entered 0. (A result of this is that if a user enters a negative number, the program will just ask for input again without trying to do a computation.) After the loop exits, the good­bye message should be displayed.
One of the interesting things about this series approximation is how slowly it converges to the actual value of . Try it out on some larger numbers. E.g. see how many terms it takes to get the first digits of the approximation to be 3.14159.
What to turn in
Make sure your name is in a comment at the top of the program file. Create a zipfile of your entire ApproximatePiProgram project folder and submit it using the submission system as explained in the handout Submission Instructions for CS 205.
02/10/2011
Page 2 of 2
D. Hwang
Download