Thread: Question regarding loop and mathematics

  1. #16
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Sorry, I should have said 'principal' instead of "principle'. Which are the confusing terms?

  2. #17
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by nonoob View Post
    Sorry, I should have said 'principal' instead of "principle'. Which are the confusing terms?
    Don't worry, took me a few hours of typing principle into my code to get used to typing it right.

  3. #18
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by NinjaFish View Post
    Ah, I assume pow uses double, since i'm getting conversion errors all over the place now *facepalm*
    Well yeah. I thought you already did a

    double myarray[3];

  4. #19
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by nonoob View Post
    Well yeah. I thought you already did a

    double myarray[3];
    Well I just tried changing the variables from 'int' to double, and everything just went to hell.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Your code's formula does not match the "given" formula. You're missing a "1+" term it seems.

    How about:

    Code:
    #define LOAN_PRINCIPAL 0
    #define LOAN_RATE      1
    #define LOAN_TERM      2
    
    payment = (myarray[LOAN_PRINCIPAL] * myarray[LOAN_RATE] / 100 ) / (1 - ( pow (1 + myarray[LOAN_RATE], -myarray[LOAN_TERM] ) ) );

  6. #21
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Do you need floating point math, or can you get away with integer? (There are tricks to use)

    Is rate given in decimal (0.05) or percent (5)?

  7. #22
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    mike65535, I have already given the correct formular incoporating the 1 +
    I would assume it's given as decimal 0.05, because that way the (1 + percent)^(-num_payments) makes sense.

  8. #23
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by mike65535 View Post
    Do you need floating point math, or can you get away with integer? (There are tricks to use)

    Is rate given in decimal (0.05) or percent (5)?
    Its given in percent, and apparently that is supposed to change it do decimal(Thats what the explanation with the formula said anyways.

    And floating point was optional of three others to make the program better, I already picked another option which is in there.

  9. #24
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Yup, as I thought. You need to get this straight - what your requirements are for input and what you are allowed (or forced to) to do within the code.

    Even if you can use float, I suggest you essentially use integers * 100 so that you can get exact pennies. (or not...)
    Last edited by mike65535; 04-18-2011 at 02:32 PM.

  10. #25
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Quote Originally Posted by NinjaFish View Post
    Its given in percent, and apparently that is supposed to change it do decimal(Thats what the explanation with the formula said anyways.
    I DON'T THINK SO!

    On this page they give your formula...
    Mortgage calculator - Wikipedia, the free encyclopedia

    The fixed monthly payment for a fixed rate mortgage is the amount paid by the borrower every month that ensures that the loan is paid off in full with interest at the end of its term. The monthly payment formula is based on the annuity formula. The monthly payment c depends upon:
    r - the monthly interest rate, expressed as a decimal, not a percentage (i.e., divide the quoted yearly percentage rate by 100 and then by 12 to obtain the monthly interest rate),
    N - the number of monthly payments, called the loan's term, and
    P - the amount borrowed, known as the loan's principal.
    c is given by the formula:

  11. #26
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by mike65535 View Post
    Yup, as I thought. You need to get this straight - what your requirements are for input and what you are allowed (or forced to) to do within the code.

    Even if you can use float, I suggest you essentially use integers * 100 so that you can get exact pennies.
    Well as of now I can't do anything. Since my I'm going to have to use pow(), I need to convert my code to double. However, doing so doesn't seem to be producing the same output I want (as in, it produces nothing. or causes my program to see any number entered to be invalid and out of range. This sucks.

    EDIT: Now it's working slightly better. I should be able to work with the actual math of it now.
    Last edited by NinjaFish; 04-18-2011 at 02:34 PM.

  12. #27
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    The calculations for amortizing a loan are not secret, so I'll post a simple program that does a simple amortization table. You'll need to incorporate this into the program you're trying to write (i.e. the arrays).

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main( void )
    {
        double principal  = 10000.0;
        double annualRate = 12.0;
        double rate       = annualRate / ( 12 * 100 );
        int    termYears  = 5;
        int    termMonths = termYears * 12;
    
        double payment = principal * ( rate / ( 1 - pow( ( 1 + rate ), -termMonths ) ) );
    
        int month;
    
        for ( month = 1; month <= termMonths; month++ ) {
            double monthlyInterest  = principal * rate;
            double monthlyPrincipal = payment - monthlyInterest;
            double newPrincipal     = principal - monthlyPrincipal;
    
            printf( "%3d::%8.2lf::%6.2lf::%6.2lf::%6.2lf::%7.2lf\n",
                    month, principal, payment, monthlyInterest, monthlyPrincipal, newPrincipal );
    
            principal = newPrincipal;
        }
    
        return 0;
    }
    Good luck!
    Last edited by kmess; 04-18-2011 at 02:53 PM.

  13. #28
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by kmess View Post
    The calculations for amortizing a loan are not secret, so I'll post a simple program that does a simple amortization table. You'll need to incorporate this into the program you're trying to write (i.e. the arrays).

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main( void )
    {
        double principal  = 10000.0;
        double annualRate = 12.0;
        double rate       = annualRate / ( 12 * 100 );
        int    termYears  = 5;
        int    termMonths = termYears * 12;
    
        double payment = principal * ( rate / ( 1 - pow( ( 1 + rate ), -termMonths ) ) );
    
        int month;
    
        for ( month = 1; month <= termMonths; month++ ) {
            double monthlyInterest  = principal * rate;
            double monthlyPrincipal = payment - monthlyInterest;
            double newPrincipal     = principal - monthlyPrincipal;
    
            printf( "%3d::%8.2lf::%6.2lf::%6.2lf::%6.2lf::%7.2lf\n",
                    month, principal, payment, monthlyInterest, monthlyPrincipal, newPrincipal );
    
            principal = newPrincipal;
        }
    
        return 0;
    }
    Good luck!
    Thanks alot. I'll give this a good looking over and hopefully I can use this to make my own table and such. Thanks alot

  14. #29
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    How goes the battle? Any closer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mathematics in Programming
    By silk.odyssey in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-23-2006, 08:29 PM
  2. Mathematics <Derivatives of Sin>
    By xddxogm3 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-01-2005, 09:18 AM
  3. C++ mathematics????
    By strickey in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2005, 12:20 PM
  4. rotation mathematics....
    By EvBladeRunnervE in forum Game Programming
    Replies: 11
    Last Post: 12-10-2003, 03:13 PM
  5. mathematics
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2001, 12:32 PM