# Thread: Question regarding loop and mathematics

1. ## 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.

2. 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. 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.

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. Originally Posted by mike65535
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. Originally Posted by mike65535
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.

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. Can you calculate the first month's payment?

7. Originally Posted by mike65535
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. 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. need a comma
Code:
`pow( myarray[1] , -myarray[2] )`

10. 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. Originally Posted by nonoob
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. Originally Posted by kmess
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. 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. Originally Posted by mike65535
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. edited