Please help me improve my code for ps1c: http://pastebin.com/3Gq3ZJdC When searching for a value with a precision of $0.01, my logic causes an infinite loop, because the upper and lower bounds converge to one value, thus the minimum payment never changes. Should I just include a break statement that exits the loop should upper == lower? I would like to know if there is a better way to structure this. (And yes, I am aware that I am far behind the actual course. I am just following along for my own learning/benefit.)
i think the problem i had was that upper was too low initially, so you never paid enough. finally, i just set upper to the total balance initially, and it eventually worked it's way down.
I figured out what I was doing wrong. First, I was rounding too often - it was throwing off my calculations. I changed the code to wait until the return statement before rounding any values. Secondly, the test condition for my while loop should have been while upper - lower > epsilon:, rather than abs(remainingBalance) > epsilon. I was checking to see if the answer had been found within epsilon, rather than if the search space had been reduced to being contained within epsilon, which is not the correct logic.
Join our real-time social learning platform and learn together with your friends!