Your job is to write a C program, almostpi.c,
that approximates Π/4 in three rather bad ways and displays the results
of these three calculations.
Write your program so that it obtains a single argument N
from the command line when it is started. This is very similar
to how the quizzer
routine of
Program 2 was given its argument,
and you might revisit that assignment to see how this was done.
Once your program has its argument, it can start its three approximations. In each of these algorighms we will use N to refer to your program's single argument.
Often beginning programmers use arrays unnecessarily. These is no need to use arrays when completing this homework!
Now, let's look at the three approaches.
In this approximation, you are to sum the following series +1/1, -1/3, +1/5, -1/7, +1/9, -1/11, ... all the way to the N'th term which will involve either adding or subtracting 1/(2*N-1).
For this approximation, calculate N pairs of
random numbers <X, Y> distributed
uniformly between 0 and 1.
Use the oddly-named built-in C function drand48
to
generate your random numbers.
Determine the proporation of these random pairs where
X2 + Y2 is
less than 1.
This is similar to the Monte Carlo method, but without the randomness. Assume that the unit square has been divided into a grid of N2 cells. The four corners of the cell in row i and column j are given below. (Like all good C programmers we are assuming that i and j range from 0 to N-1.)
You are going to use these points in your third calculation of Π/4. You must be clever to avoid an O(N2) algorithm in this calculation. We're going to embed Homework 16 into this Program description to increase the probability for obtaining clever coding for a bad solution.
Here are the six parts of Homework 16. This is a written homework that you can turn-in in class or email to brock@cs.unca.edu. I'd prefer than you turn in a written copy, because you must do some drawing for this homework.
Draw the grid for N equal to 10.
Determine a formula for < Xi, Yj >, the center of the cell in row i and column j.
Write a mathematician's test, involving square roots and other hard-to-compute stuff, that determines if the distance from the center point < Xi, Yj > to the origin < 0, 0 > is less that 1.
Write a programmer's test, involving no square roots and no divides, that determines if the distance from the center point < Xi, Yj > to the origin < 0, 0 > is less that 1.
Now we have a nice formula for the N2 center points
and that suggests a natural O(N2) algorithm
with a nested for
loop.
However, we can actually do an O(N) algorithm, by taking advantage
of two properties.
First, when working in a column i, if
< Xi, Yj+1 > is outside the circle
and < Xi, Yj > is inside the circle;
then < Xi, YN-1 > to
< Xi, Yj+1 > are all outside the circle and
< Xi, Yj > to
< Xi, Y0 > are all inside the circle.
This significantly reduces the amount of checking we have to
do within a column, because we can stop as soon as we find this boundary point.
Second, when moving from column to column, if
< Xi, Yj > is outside the circle, then
< Xi+1, Yj > is also outside the circle. This gives up a very good starting
point for finding the boundary point.
For this subproblem, make a nice drawing that illustrates the points that must be checked. Your drawing should show a serious of move though the boundary points of the circle.
Write some psuedo-code to go along with your drawing.
Submit almostpi.c via Wolfware into Program_4 by April 20 to turn in your program.
Some students have requested a more formal statement of the "rules" for grading. It's difficult to give rules with this sort of inflexibility, but here are some strong guidelines.