Showing posts with label happppyyyyy. Show all posts
Showing posts with label happppyyyyy. 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, August 12, 2013

Quietly Lurking, Quietly Working

Oh man, I've been pulled in so many different directions lately.

Over the past few weeks, I have:

  • Watched Last of Us from start to finish as my fiance played it (WOW, what an amazing game)
  • Resubbed to World of Warcraft (ugh, Mists is terrible)
  • Started playing Guild Wars 2 again
  • WRITTEN A LOT OF STUFF
  • Watched Minority Report, Tron, Tron: Legacy, The Fifth Element, Daybreakers, and kept up with Breaking Bad and Dexter
  • Messed around with C
  • Done some really fulfilling sketches for character designs
I've been really really busy. But the main reason I'm posting today is to say, "LOOK, LOOK!! I'm coding things on my own!!"

Take a gander! :)

These are exercises in my textbook -- which I have since decided that taking the notes down in my notebook is time-consuming enough... I don't think I'll be uploading them to this blog anymore. Instead, I'll be sharing the source that I write, when I feel like it. C: I'm stingy, I know.

So, the program I'm particularly fond of at this time is this exercise. I was prompted to find the volume of a sphere using a constant. So I did:

#include <stdio.h>
#define RADIUS 10.0f

int main(void)
{
   float volume;

   volume = (4.0f/3.0f * 3.14159 * (RADIUS * RADIUS * RADIUS));

   printf("Radius = %.1f\n", RADIUS);
   printf("Volume = %.3f\n", volume);

   return 0;

}

It was pretty nifty! Compiled and performed just fine out of Xcode (which is what I use on Mac because I haven't had time to investigate any others -- on the Zombie, I've just been using Mousepad to make changes so far). But I wondered if there was another way to do it. So I experimented...

#include <stdio.h>

int main(void)
{
   float radius, volume;

   radius = 10.0f
   volume = (4.0f/3.0f * 3.14159 * (radius * radius * radius));

   printf("Radius = %.1f\n", radius);
   printf("Volume = %.3f\n", volume);

   return 0;

}

Wouldn't you know! It compiled and worked just fine, too!! Gave the same answer I calculated manually on my calculator (which is 4188.787, rounded as the final decimal repeats). I'm so thrilled!

The second part of the exercise stated to alter the program so the user enters the radius before the calculation is made. Here goes:

#include <stdio.h>

int main(void)
{
   float radius, volume;

   printf("Enter radius of sphere: ");
   scanf("%f", &radius);

   volume = (4.0f/3.0f * 3.14159 * (radius * radius * radius));
   printf("Volume of sphere: %.3f\n", volume);

return 0;
}

Ahh, works perfectly. So I now have a fifteen line program that calculates the volume of a sphere... You know, just in case. ;D

Friday, June 14, 2013

I've not forgotten anyone...

I promise I've not abandoned this blog. If anything, I am more enthusiastic to learn how to code than I was before.

I've been promoted to an immortal position in the MUD I currently play. I've been building there instead of in The Builder's Academy for the past week or so, and I can't overstate how surreal it feels to have a position like this in this particular MUD. I've logged over 70 hours since last Saturday night... out of a total of 168 hours I could have spent on there this week, that is rather considerable, I think. My fiance got a position as well, and he only has 30 hours logged...

Anyway, I'm gushing and probably talking about it too much. The point is, at least until my zone is complete and ready for launch, I will be on coding homework hiatus. Hopefully I don't forget too much... But I will also be working extensively with something called m/o/rprogs, and my notes on those may come here, much like my notes on DG Scripts.

I'll be around!