Translating a math expression

Mar 3, 2013 at 1:58am
I need to translate this expression into C++ code, without using cmath functions.

Payment = ((Rate(1+Rate)^N)/((1+Rate)^N - 1)) * L

my particular concern though is only with the (Rate + 1)^N

I need to loop this expression so as to evaluate it from N=1 to N=24. How can i do that in an elegant way?

Thank you.
Mar 3, 2013 at 3:15am
bump. anyone?
Mar 3, 2013 at 6:03pm
once again BUMP for this.
Mar 3, 2013 at 6:10pm
I need to loop this expression so as to evaluate it from N=1 to N=24. How can i do that in an elegant way?

Use a for loop:
1
2
3
  for (int N=1; N<=24; N++)
  {  //  perform calculation
  }


Mar 3, 2013 at 6:17pm
Sry, I still dont understand. What should I write in the calculation part?

(rate + 1) * (rate + 1)

wouldn't that give me an answer that is more than (rate + 1)^N...

Mar 3, 2013 at 6:38pm
Raising a number to the power N where N is an integer is simply a matter of repeated multiplication.

Although you could write a separate function to carry out a loop to do this, if you already have a looping structure in your program, you can just use some value which is set to an initial value of 1.0, then each time around the loop, you multiply it by the number.
Mar 3, 2013 at 6:42pm
Do you have any C or C++ experience?
Your question sounds like a homework assignment and it's the policy of this site not to do homework assignments for people. You should be trying to write this yourself.

Mar 3, 2013 at 6:58pm
Actually it is a homework assignment, but I haven't asked you to write my homework assignment for me have I? Like anyone else I have stumbled on something I don't understand and I sought help. I am not very good with programming and mathematics, I am a History major. Nevertheless here i am putting in the time and the effort and learning. I am not asking someone to write my program for me, just to guide me in understanding a concept.

Thank you.

anyway I have come up with this, will this work?

for(int N=1; N<=24; N++)

N *=(1 + Rate)
Mar 3, 2013 at 7:12pm
It won't work as written, because you are trying to use the variable N for two completely separate purposes (a) to control how many times the loop is executed and (b) to store the value of the number raised to a particular power. In addition, N is of type int, which is fine for the loop counter, but the other variable would be better as a type double.

Chervil wrote:
use some value which is set to an initial value of 1.0, then each time around the loop, you multiply it by the number.

before the loop begins, define a variable of type double, set its value to 1.0.
Then use that inside the loop like this:
number *= (1 + Rate);
Mar 3, 2013 at 7:37pm
Ok. Sweet. Thank you!
Apr 10, 2013 at 9:27am
@DanStirletz

1
2
3
4
5
6
for(N=1; N<=24; N++){
    b[N]=pow(Rate,N);
    Payment[N] = ((Rate*b[N])/( b[N]- 1)) *L;
    
    cout<<"Payment no. "<<N<<": "<<Payment[N]<<endl;
    }
Topic archived. No new replies allowed.