Suppose the numbers ($10,000, 6 percent, $500) were user selectable. Are there values for which the algorithm you developed would not terminate? If so, change the algorithm to make sure it always terminates
belongs to collection: Algorithm and pseudocode excersices
All Answers
total answers (1)
Yes, any interest rate that causes more money to be added each month than was taken out would prevent the solution to R1.12 from terminating. We can guarantee that our algorithm terminates by checking for this condition before the calculation starts:
need an explanation for this answer? contact us directly to get an explanation for this answerbalance = $10,000
monthly interest rate = 0.5%
monthly withdrawal amount = $500
if balance * monthly interest rate > monthly withdrawal amount
report that the balance will never reach zero
else
total months = 0
while balance is greater than $0
increase balance by 0.5% of its value
decrease balance by $500
add 1 to the total number of months
years to deplete = total months /12