Showing posts with label bug-checking. Show all posts
Showing posts with label bug-checking. Show all posts

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!!

Monday, May 13, 2013

Slow Work Day

That means I can work on other things at my desk, yay!

Throughout the day, I'm going to be working on finalizing the last two triggers for my zone in TBA. The first one, I referenced in this post I made the other day, about the rose in the rose bush. I still haven't bugchecked it, but I'm going to today. I also found this from my early posts on the TBA forums:




From Fizban,
The following was written in this post, not in trigedit, and as such is completely untested, but should work, or at the very least work with very minor changes.
Type: Random
Numeric Arg: 100
wait 300 s
if !%self.contents%
%load% mob <insert mob vnum>
else
set curr_item %self.contents%
while %curr_item%
if %curr_item.vnum% == 59320
halt
else
set %curr_item% %curr_item.next_in_list%
end
done
%load% mob <insert mob vnum>
end
...you'd just need to have the load script on her have a check for the vnum of the room she is currently in and then have her load a rose and put it in the container if she is in the appropriate room.




Comparing this to what I have, they are rather different. :/ Gotta work on fixing that.

The next trigger I'm going to work on is going to give a druid healer a few protective spells to cast on people present within the room. Ideally, I'd love for him to have a couple of spells he casts if someone stands in the room long enough, and then stops casting once the effects of those spells are present on the character(s), but that's going to be much more elaborate than a list of random spells he casts every 10 seconds haha.

We'll see what happens. I'm going to be updating the post referenced above with my changelog!! If anyone here is actually paying attention for my bug-checking methods, that's where to keep an eye out!