erstellt 09.08.99; übersetzt 01.04.03; überarbeitet 04.12.03; © Copyright 2003 Deutsche Übersetzung Heinrich Gailer

Kapitel 19 Programmieraufgaben


Aufgabe 1 --- Ratenzahlung

Angenommen Sie schulden Ihrer Kreditkartengesellschaft 1000.00$. Die Gesellschaft berechnet Ihnen monatlich 1.5% Zinsen auf den gewährten Kredit. Sie entscheiden sich die Kreditkarte nicht mehr zu verwenden und Ihren Kredit monatlich mit einem bestimmten Betrag n zurückzuzahlen. Schreiben Sie ein Programm, das nach dem monatlichen Rückzahlungsbetrag fragt, dann den Saldo und den Gesamtbetrag der Zahlungen für jeden Monat ausgibt, bis der Saldo Null oder weniger beträgt.

Geben Sie den monatlichen Betrag ein:
100
Monat: 1        Saldo: 915.0  Gesamtbetrag: 100.0
Monat: 2        Saldo: 828.725        Gesamtbetrag: 200.0
Monat: 3        Saldo: 741.155875     Gesamtbetrag: 300.0
Monat: 4        Saldo: 652.273213125  Gesamtbetrag: 400.0
Monat: 5        Saldo: 562.057311321875       Gesamtbetrag: 500.0
Monat: 6        Saldo: 470.4881709917031      Gesamtbetrag: 600.0
Monat: 7        Saldo: 377.54549355657866     Gesamtbetrag: 700.0
Monat: 8        Saldo: 283.20867595992735     Gesamtbetrag: 800.0
Monat: 9        Saldo: 187.4568060993263      Gesamtbetrag: 900.0
Monat: 10       Saldo: 90.26865819081618      Gesamtbetrag: 1000.0
Monat: 11       Saldo: -8.377311936321576     Gesamtbetrag: 1100.0

Berechnen Sie die monatlichen Zinsen auf Grund des Saldos. Berechnen Sie dann den neuen Saldo in dem Sie die Zinsen addieren und den zurückgezahlten Betrag subtrahieren.

Zurück zur Übersicht.



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

Click here to go back to the main menu.

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
Click here to go back to the main menu.
End of exercises.