Thread: Question regarding loop and mathematics

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    Question regarding loop and mathematics

    As of right now, i'v spent the better half of the past 24 hours trying to make my program output the right answers to a given formula, using printf. I'll post the given equation, what I got out of it, and then what I need. I'm hoping someone can point me in the right direction on a couple things, which I will ask below the equation::

    Code:
    myarray[0] = principal, myarray[1] = rate, myarray[2] = months
    
    //what was given//
    
                    payment =         principal x rate  
                                     1 - ( (1+rate) to the power (-m) ) )
    
    
    //What I got from it//
    
    payment = (myarray[0] * myarray[1]) / 1 - ( (myarray[1] ^(-myarray[2])) );
    
    Total of what I need::
    
    printf("Month::Old Balance::Payment::Interest::Principal::New Balance\n");

    Basically, I need the outputs from the final line, and i'm running into two main issues::

    1:: I'm not overly intelligent with math.
    2:: I need my for loop to count down each month, and each month, modify all the data until it reaches the final month, and the loans balance is set to 0.

    Now i'm not looking for direct answers, but I am really bad in math and I could definitely use some guidance. And below is the loop i'm decided to test::

    Code:
    for(i = 0;i < myarray[2]; myarray[2]--);
    I figure this loop is probably wrong, however it's been my best guess so far, and atleast it's counting down in the loop when I run a printf statement.

    Thanks for any help you can give on this. I figured it'd be best to ask for some help since I haven't really left ground-zero after numerous attempts.
    Last edited by NinjaFish; 04-18-2011 at 01:20 PM. Reason: error

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    1.
    Code:
    payment = (myarray[0] * myarray[1]) / 1 - ( (myarray[1] ^(-myarray[2])) );
    Raising to a power is not '^'

    2.
    Code:
    for(i = 0;i < myarray[2]; myarray[2]--);
    Generally, for loops are written like this
    Code:
    for(i = 0;i < myarray[2]; i++)
    In other words, you increase (or decrease) the counter, 'i'.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    OK, I don't see why you have things like months stored in myarray. Seems you only need one variable per parameter (say, three integers: term, rate, principal)

    Doesn't seem you have a handle on the algorithm you need to implement. You need to think about how you'd perform the calculation using pencil and paper.

    If I'm reading your requirements right...

    1. Enter all the pertinent data (principal, term in mos, rate in percent) and store each in a sensible variable.
    2. Calculate the first month's payment (call it, say current_payment) using your formula.
    3. Subtract current_payment from principal
    4. subtract one from term.
    5. Go to 2 until term is zero.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by mike65535 View Post
    1.
    Code:
    payment = (myarray[0] * myarray[1]) / 1 - ( (myarray[1] ^(-myarray[2])) );
    Raising to a power is not '^'

    2.
    Code:
    for(i = 0;i < myarray[2]; myarray[2]--);
    Generally, for loops are written like this
    Code:
    for(i = 0;i < myarray[2]; i++)
    In other words, you increase (or decrease) the counter, 'i'.
    There are also dozens of variations to that, such as --i, ++i and i--.
    It's also why I said my loop is probably wrong. You didn't really say anything I hadn't already pointed out.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by mike65535 View Post
    OK, I don't see why you have things like months stored in myarray. Seems you only need one variable per parameter (say, three integers: term, rate, principal)

    Doesn't seem you have a handle on the algorithm you need to implement. You need to think about how you'd perform the calculation using pencil and paper.

    If I'm reading your requirements right...

    1. Enter all the pertinent data (principal, term in mos, rate in percent) and store each in a sensible variable.
    2. Calculate the first month's payment (call it, say current_payment) using your formula.
    3. Subtract current_payment from principal
    4. subtract one from term.
    5. Go to 2 until term is zero.
    I probably should of pasted my entire code, I will if you'd like, but it's because the requirements of the program require me to use a separate function to get the three data values you said, and return them. I needed to use a pointer to get those values to bring them back. But ya, you basically got it right.

    As the months count down, all the values have to go down until they hit 0. Which has to be persistent to hitting the last month as well.

    I tried doing a pencil and paper route, however I'm lousy with math. I can't even figure out how to calculate the stuff. Putting it into the code shouldn't be to hard, but after that, it's rather confusing to me.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Can you calculate the first month's payment?

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by mike65535 View Post
    Can you calculate the first month's payment?
    As of right now, no. I tried
    Code:
    payment = (myarray[0] * myarray[1]) / 1 - (pow(myarray[1] (-myarray[2])) );
    However, now 'i'm getting myarray[1] cannot be used as a function' and it worked a minute ago, so let me try to make this work again.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    First thing:
    Code:
    payment = (myarray[0] * myarray[1]) / (1 - pow((1 + myarray[1]), -myarray[2]));
    Then in the loop which will go for the number of payments (months), you will likely want to print out the principle, the interest accrued on that amount. You will calculate the new principle inflated by the interest and subtracting the payment made.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    need a comma
    Code:
    pow( myarray[1] , -myarray[2] )

  10. #10
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Hi NinjaFish. Can you clarify whether arrays are a specific requirement of the assignment, or just the means you've chosen to solve the problem? Unless they're required, their use complicates things a bit.

    Is there a specific term to the loan?

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by nonoob View Post
    First thing:
    Code:
    payment = (myarray[0] * myarray[1]) / (1 - pow((1 + myarray[1]), -myarray[2]));
    Then in the loop which will go for the number of payments (months), you will likely want to print out the principle, the interest accrued on that amount. You will calculate the new principle inflated by the interest and subtracting the payment made.
    A few of those term's didn't really make much sense to me, unfortunately, lol.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by kmess View Post
    Hi NinjaFish. Can you clarify whether arrays are a specific requirement of the assignment, or just the means you've chosen to solve the problem? Unless they're required, their use complicates things a bit.
    I would of done with with scanf and printf the entire time if I could of aha. It's a requirement, which is stupid, I know.
    And term is entered by user.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    If you MUST use an array, you could create macros for the array indices
    Code:
    #define LOAN_PRINCIPAL 0
    #define LOAN_TERM      1
    #define LOAN_RATE      2
    Then use
    Code:
    myarray[LOAN_PRINCIPAL]
    etc

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by mike65535 View Post
    need a comma
    Code:
    pow( myarray[1] , -myarray[2] )
    Ah, I assume pow uses double, since i'm getting conversion errors all over the place now *facepalm*

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    edited
    Last edited by NinjaFish; 04-18-2011 at 05:22 PM.

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