created 08/09/99


Chapter 19 Programming Exercises


Exercise 1 --- Credit Card Bill

Say that you owe the credit card company $1000.00. The company charges you 1.5% per month on the unpaid balance. You have decided to stop using the card and to pay off the debt by making a monthly payment of N dollars a month. Write a program that asks for the monthy payment, then writes out the balance and total payments so far for every succeeding month until the balance is zero or less.

Enter the monthly payment:
100
Month: 1        balance: 915.0  total payments: 100.0
Month: 2        balance: 828.725        total payments: 200.0
Month: 3        balance: 741.155875     total payments: 300.0
Month: 4        balance: 652.273213125  total payments: 400.0
Month: 5        balance: 562.057311321875       total payments: 500.0
Month: 6        balance: 470.4881709917031      total payments: 600.0
Month: 7        balance: 377.54549355657866     total payments: 700.0
Month: 8        balance: 283.20867595992735     total payments: 800.0
Month: 9        balance: 187.4568060993263      total payments: 900.0
Month: 10       balance: 90.26865819081618      total payments: 1000.0
Month: 11       balance: -8.377311936321576     total payments: 1100.0

For each month, calculate the interest due on the unpaid balance. Then calculate the new balance by adding the interest and subtracting the payment.

Improved Program:     Have the program prompt for the beginning balance, the monthly interest, and the payment amount. Also, when the balance falls below the amount of the monthly payment, write out the final payment that will bring the balance to exactly zero.

 

Exercise 2 --- Drug Potency

A certain drug looses 4% of its effectivness every month it is in storage. When its effectiveness is below 50% it is considered expired and must be discarded. Write a program that determines how many months the drug can remain in storage.

month: 0        effectiveness: 100.0
month: 1        effectiveness: 96.0
month: 2        effectiveness: 92.16
month: 3        effectiveness: 88.47359999999999
month: 4        effectiveness: 84.93465599999999
month: 5        effectiveness: 81.53726975999999
month: 6        effectiveness: 78.27577896959998
month: 7        effectiveness: 75.14474781081599
month: 8        effectiveness: 72.13895789838334
month: 9        effectiveness: 69.253399582448
month: 10       effectiveness: 66.48326359915008
month: 11       effectiveness: 63.82393305518407
month: 12       effectiveness: 61.27097573297671
month: 13       effectiveness: 58.82013670365764
month: 14       effectiveness: 56.46733123551133
month: 15       effectiveness: 54.20863798609088
month: 16       effectiveness: 52.04029246664724
month: 17       effectiveness: 49.95868076798135 DISCARDED
 

Exercise 3 --- ex

One of the more amazing facts from calculus is that the following sum gets closer and closer to the value ex the more terms you add in:

ex = 1 + x + x2/2! + x3/3! + x4/4! + x5/5! + x6/6! + . . . .

Remember that n! means n factorial, n*(n-1)*(n-2)* ... *1. For example, if x is 2 then

e2 = 1 + 2 + 22/2! + 23/3! + 24/4! + 25/5! . . . .
e2 = 1 + 2 + 4/2 + 8/6 + 16/24 + 32/120 + . . . .

e2 = 1 + 2 + 2 + 1.3333 + 0.6666 + 0.2666 + . . . .

e2 ~ 7.266

More exactly, e2 = 7.38907...

Write a program that asks the user to enter x, then calculates ex using a loop to add up successive terms until the current term is less than 1.0E-12. Then write out the value Math.exp(x) to see how your value compares.

To do this program sensibly, the loop will add in a term each iteration.

sum = sum + term;

Look carefully at the first equation for ex. Notice that each term is:

x N/N!

for some N. This is the same as:

x(N-1)/(N-1)!   *   x/N

This is the same as the previous term times x/N. So each iteration of the loop merely has to multiply the previous term by x/N and add it to the accumulating sum.

Don't let the math scare you away! This is actually a fairly easy program, and is typical of a type of calculation that computers are often used for.

Enter x:
2
n:1     term: 2.0               sum: 3.0
n:2     term: 2.0               sum: 5.0
n:3     term: 1.3333333333333333                sum: 6.333333333333333
n:4     term: 0.6666666666666666                sum: 7.0
n:5     term: 0.26666666666666666               sum: 7.266666666666667
n:6     term: 0.08888888888888889               sum: 7.355555555555555
n:7     term: 0.025396825396825397              sum: 7.3809523809523805
n:8     term: 0.006349206349206349              sum: 7.387301587301587
n:9     term: 0.0014109347442680777             sum: 7.3887125220458545
n:10    term: 2.8218694885361555E-4             sum: 7.388994708994708
n:11    term: 5.130671797338464E-5              sum: 7.389046015712681
n:12    term: 8.551119662230774E-6              sum: 7.3890545668323435
n:13    term: 1.3155568711124268E-6             sum: 7.389055882389215
n:14    term: 1.8793669587320383E-7             sum: 7.3890560703259105
n:15    term: 2.5058226116427178E-8             sum: 7.389056095384136
n:16    term: 3.1322782645533972E-9             sum: 7.389056098516415
n:17    term: 3.6850332524157613E-10            sum: 7.389056098884918
n:18    term: 4.094481391573068E-11             sum: 7.389056098925863
n:19    term: 4.309980412182177E-12             sum: 7.3890560989301735
n:20    term: 4.309980412182177E-13             sum: 7.389056098930604

my   e^x: 7.389056098930604
real e^x: 7.38905609893065
 
End of exercises.