Tuesday, August 13, 2013

Bug-Checking a Program

Thought I might go ahead and post this, cuz why not. Work has somewhat slowed down as I begin writing this at 2:48 on a Tuesday afternoon (though I am tempting fate with that statement), and I've had enough free time to work on this so far...

The program is supposed to ask for the total amount of a loan, the interest rate the loan was taken at, and the monthly payment. Note that I am posting this despite the fact that I KNOW there is something up with my calculations -- otherwise, I wouldn't be having a problem!

The example in the book states:

Amount of Loan: 20000.00
Interest Rate: 6.0
Monthly Payment: 386.66

Balance remaining after first payment: $19713.34
Balance remaining after second payment: $19425.25
Balance remaining after third payment: $19135.71

Seeing as this is the easiest way to tell if my own calculations are correct, I'm using this as a constant. So, here's the code I've come up with, after tweaking it for a bit and adding some variables:

#include <stdio.h>
#define months 12

int main (void)
{
   float loan_total, interest, interest_rate;
   float monthly_pymt, balance;
   float balance_first, balance_second, balance_third;

   printf("Enter Loan Amount: ");
   scanf("%f", &loan_total);

   printf("Enter Interest Rate: ");
   scanf("%f", &interest);

   printf("Enter Monthly Payment: ");
   scanf("%f", &monthly_pymt);

   interest_rate = (interest/100)/months;
      /* 100 is used to turn the value given as the interest rate
      into a percentage */
   balance = loan_total - monthly_pymt;
   balance_first = balance + (balance * interest_rate);

   printf("Balance after first payment: %.2f\n", balance_first);

   balance = balance_first - monthly_pymt;
   balance_second = balance + (balance * interest_rate);

   printf("Balance after second payment: %.2f\n", balance_second);

   balance = balance_second - monthly_pymt;
   balance_third = balance + (balance * interest_rate);

   printf("Balance after third payment: %.2f\n", balance_third);

   return 0;

}

This was a test on my part, and I've already ruled out the possibility that balance is retaining its first value throughout the program. When entering the same credentials, here is what the above program returns:

Enter Loan Amount: 20000.00
Enter Interest Rate: 6.0
Enter Monthly Payment: 386.66

Balance after first payment: 19711.41
Balance after second payment: 19421.37
Balance after third payment: 19129.88

Pennies of a difference! Okay, maybe a few dollars. But still! Naturally, the next step is to check over the equations to make sure they are doing what they need to do...

And I think I found it. 

balance_first = balance + (balance * interest_rate);

The balance after the first payment is off by approximately 2.50 because the interest is being calculated against the updated balance here -- I think it needs to be calculated against the original loan amount.

balance_first = balance + (loan_amount * interest_rate);

This should even out the rest... Gonna run a quick test.

Enter Loan Amount: 20000.00
Enter Interest Rate: 6.0
Enter Monthly Payment: 386.66

Balance after first payment: 19713.34
Balance after second payment: 19423.31
Balance after third payment: 19131.84

Well, it fixed the first payment. But I see exactly what needs to change now!

balance_second = balance + (balance_first * interest_rate);

balance_third = balance + (balance_second * interest_rate);

Testing with this minor change...

Enter Loan Amount: 20000
Enter Interest Rate: 6.0
Enter Monthly Payment: 386.66

Balance after first payment: 19713.34
Balance after second payment: 19425.25
Balance after third payment: 19135.71

Hah! Yes! It is now returning the correct amounts!! I never did like accounting... It can be somewhat confusing. The problem essentially came from the interest being calculated with the newly initialized balance value, instead of the balance that had been reported for the previous month. Of course, interest isn't going to take into consideration that you are making a payment this month -- it's going to accrue based on the last month's balance. I swapped out those variables so the interest calculated correctly, and voila!

So there. A bug-checking process for you all! I am now off to take notes on Chapter 3!! See you all when its time for more programming exercises!!

No comments:

Post a Comment