Basic C Programming Skills Test: N: P 1 + R + R + R
Basic C Programming Skills Test: N: P 1 + R + R + R
When this is done, add to your program a feature that halts the program
and prints an error message if P exceeds a given value PMAX, with PMAX defined
in a #define statement. Now, if I use #define PMAX 10 and take r = .95, the
program should produce something like:
P exceeded 10 with 13 payments, r = .950
1
Do this by adding a few more lines of code. You should hand in the before
(without the feature) and after (with the feature) programs. On the job, you
will probably spend more time adding features to existing programs than writing
programs from scratch.
for k in the range 0 ≤ k ≤ n. They can be computed using the relationship that
is the basis of Pascal’s triangle:
1
B(n, k) = (B(n − 1, k) + B(n − 1, k − 1)) . (3)
2
The formula (3) holds for k = 1, · · · , n − 1, and needs to be supplemented with
When n = 1, the conditions (4) define B completely. In this exercise, you will
use the recurrence relations (3) and boundary conditions (4) to compute values
of B(n, k). Do not use (2) in your program. The main ingredient is a procedure
NextB that computes all the values B(n, 0), . . ., B(n, n) (the values for a fixed
n and all integers k in the range 0 ≤ k ≤ n), given that the values B(n − 1, k)
for 0 ≤ k ≤ n − 1 have already been computed. Your procedure should have
signature
double NextB( double B[], int *firstK, int *lastK, int n);
When the procedure is called, the array entry B[k] should hold the value B(n −
1, k) for k in the range 0 ≤ k ≤ n − 1. When the procedure returns, B[k]
should have the value B(n, k) for k in the range 0 ≤ k ≤ n. To do this, the
procedure will use both (3) and (4). The procedure should return the value of
the largest binomial coefficient at level n, that is, Bmax = maxk B(n, k). For
large n, most of the binomial coefficients are very small relative to the largest
one. The largest one is for k about n/2, and they get smaller as k increases
or decreases from that value. When the procedure returns, firstK should hold
the first k value with B(n, k) > .01 ∗ Bmax and lastK should have the last k
value with B(n, k) > .01 ∗ Bmax .
Test your procedure by having it called by the following main program. It
may be interesting to see just how many large B values there are for a given n.
Feel free to experiment with larger values of NMAX.
2
double NextB( double B[], int *firstK, int *lastK, int n);
int main() {
double B[NMAX+1];
double maxB;
int n, firstK, lastK,
• We are using just one array, while the formula (3) uses two arrays (one
for level n and one for level n − 1). The statement
will produce the wrong answer. You need to save the old B(n, k − 1).
• To avoid computing 2−n for B(n, 0) and B(n, n), you can use the relation
2−n = .5 · B(n − 1, 0).
• A simple way to find firstK and lastK is to use a while loop, or a for
loop with a break statement, after the B(n, k) have been computed. That
is, loop over k at least twice.
• The normalization factor 2−n in (2) allows us to compute to much larger
values of n without overflowing the range of floating point arithmetic.
• The binomial tree method for pricing simple stock options leads to a pro-
gram that is very similar to this one.