c++ program homework problem

Sharky Forums


Results 1 to 6 of 6

Thread: c++ program homework problem

  1. #1
    Tiger Shark joeyw99's Avatar
    Join Date
    Mar 2001
    Location
    Cleveland, MS
    Posts
    912

    Post c++ program homework problem

    I have to write a program that figures the remaining balance for a loan. the formula they give is:

    balance = payment ( 1 - (1 + i)^k-n / i)

    k = payment number
    payment = amount of monthly payment
    i = interest rate per month (annual rate/12)
    n = total number of payments to be made

    The code I have is:

    interest2 = interest / 12;
    balance = payment * (1 - (pow((1 + interest2), (number - totalnum))) / interest2);

    but it doesn't work out right. i get a negative number. Any help?

    ------------------
    Long live SFOT!

    And now...your moment of Zen!

    Sure you can trust the government! Just ask an Indian!

  2. #2
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    these are taken from your post:
    balance = payment ( 1 - (1 + i)^k-n / i)
    balance = payment * (1 - (pow((1 + interest2), (number - totalnum))) / interest2);

    they um.. do not really correspond to each other. your code is really saying this:
    balance = payment(1 - (1+i)^(k-n) / i)

    try this instead:
    balance = payment * (1 - pow(1 + interest2, number) - totalnum/interest2); --> more parenthesis could make the code harder to understand

    ------------------
    Keep it brief

    [This message has been edited by dighn (edited September 13, 2001).]
    .

  3. #3
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    balance = payment ( 1 - (1 + i)^k-n / i)
    k = payment number
    payment = amount of monthly payment
    i = interest rate per month (annual rate/12)
    n = total number of payments to be made

    The code I have is:

    interest2 = interest / 12;
    balance = payment * (1 - (pow((1 + interest2), (number - totalnum))) / interest2);

    but it doesn't work out right. i get a negative number. Any help?
    More info is needed. Is payment negative or positive? What value are you getting for pow(...)/i? Is it greater than 1 (so 1 minus it would be negative)? Also, is it (((1 + i)^k) - n) or ((1 + i)^(k-n))? Double check that.

    Fully parenthesizing the function you wrote would give:

    (1 - ((1 + i)^k) - (n/i))

    That doesn't seem right.
    System specs:


    | Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
    | Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
    | Windows 7 Home 64-bit |

  4. #4
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448

    Post

    Remember that when you do division that it is strong practice to make all the variables float or double (usually double).

    For instance:

    a = b/5;

    When b = 10, a = 2.. simple, right?

    Ok, how about this:

    when b = 12, a = 2... what?, you say.

    Remember, intergers are truncated when divided. So make sure all of your variables are double or float. Then write your division problems like so:

    a = b / 12.0;

    See what happens after that.

    ------------------
    ## root is the greed of all evil ##

    [This message has been edited by biosx (edited September 13, 2001).]
    ## root is the greed of all evil ##

  5. #5
    Catfish
    Join Date
    Feb 2001
    Posts
    116

    Post

    balance = payment ( 1 - (1 + i)^k-n / i)

    This has got to be wrong. What you have (with more parenthesis for clarity) is:

    balance = payment * (1 - (1+i)^k - (n/i))

    Break it down:

    (1+i)^k is between 1 and 2, probably very close to 1

    (n/i) is a large number

    so (1 - ~1 - large #) is going to be negative. You multiply that by payment, which is positive, and your answer is negative.



    ------------------
    "On the side of the software box, in the 'System Requirements' section,
    it said 'Requires Windows 95 or better'. So I installed Linux."
    -Anonymous
    "On the side of the software box, in the 'System Requirements' section,
    it said 'Requires Windows 95 or better'. So I installed Linux."
    -Anonymous

  6. #6
    Catfish
    Join Date
    Feb 2001
    Posts
    116

    Post

    I just got back from lunch, took a look at it again, and I think what you're looking for is this:

    balance = payment * ((1-(1+i)^(k-n))/i)

    Your code is dividing by (1+i)^(k-n) by i insteaad of dividing 1-(1+i)^(k-n) by i.

    Originally posted by joeyw99:
    I have to write a program that figures the remaining balance for a loan. the formula they give is:

    balance = payment ( 1 - (1 + i)^k-n / i)

    k = payment number
    payment = amount of monthly payment
    i = interest rate per month (annual rate/12)
    n = total number of payments to be made

    The code I have is:

    interest2 = interest / 12;
    balance = payment * (1 - (pow((1 + interest2), (number - totalnum))) / interest2);

    but it doesn't work out right. i get a negative number. Any help?



    ------------------
    "On the side of the software box, in the 'System Requirements' section,
    it said 'Requires Windows 95 or better'. So I installed Linux."
    -Anonymous
    "On the side of the software box, in the 'System Requirements' section,
    it said 'Requires Windows 95 or better'. So I installed Linux."
    -Anonymous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •