Am I using the power function correctly?

Thanks!
Last edited on
payment = (((amount_of_loan)*(rate_of_interest))*(pow((1+(rate_of_interest)),(number_of_years)))/((pow((1+(rate_of_interest)),(number_of_years)))-1));

This formula should be :
payment = pow((amount_of_loan)*(interest_rate) * (1+interest_rate), (number_of_years)) / pow((1+interest_rate), (number_of_years)) - 1;
Does that help? :)
Unfortunately it did not :-(.

I copied and paste your formula code over mine, and there is still an error.

The below is what it says:

`payment' undeclared (first use this function)

In function `float calculatePayment(double&, double&, double&, double&, double, int&)':
Then here is this one :
int final_payment = pow((amount_of_loan)*(rate_of_interest) * (1+rate_of_interest), (number_of_years)) / (pow((1+rate_of_interest), (number_of_years)) - 1);

Anyway, can you let us know about your assignment sample output?
closed account (E0p9LyTq)
Change the prototype:
void calculatePayment(double&, double&, double&, double&, double, int&, double&);

Change the definition of the function:
void calculatePayment(double& selling_price, double& rate_of_interest, double& down_payment, double& amount_of_loan, double downpayment_percentage, int& number_of_years, double& payment)

Change your function call in main:
calculatePayment(selling_price, rate_of_interest, down_payment, amount_of_loan, downpayment_percentage, number_of_years, payment);

Add an output line to show your monthly payment:
1
2
3
   cout << "Amount of Loan: " << amount_of_loan << endl;
   cout << "Down Payment: " << down_payment << endl;
   cout << "Monthly Payment: " << payment << "\n";


What is the selling price? :
10000
What is the rate of interest?:
15
What is the number of years in loan?:
20

Output Test:
Selling Price: 10000
Rate of Interest: 15
Number of Years: 20
Amount of Loan: 8000
Down Payment: 2000
Monthly Payment: 120000

Purpose: Monthly House Cost Calculation


Yeah, I'd say your monthly payment algorithm is seriously whacked! ;)
closed account (E0p9LyTq)
And closed account's algorithm is even worse, with the corrected code:

What is the selling price? :
10000
What is the rate of interest?:
15
What is the number of years in loan?:
20

Output Test:
Selling Price: 10000
Rate of Interest: 15
Number of Years: 20
Amount of Loan: 8000
Down Payment: 2000
Monthly Payment: 3.83376e+101

Purpose: Monthly House Cost Calculation
closed account (E0p9LyTq)
Using the way to calculate loan payments here, http://www.wikihow.com/Calculate-Loan-Payments I did the loan calculation in intermediate steps instead of one huge indecipherable statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void calculatePayment(double& selling_price, double& rate_of_interest, double& down_payment, double& amount_of_loan, const double& downpayment_percentage, int& number_of_years, double& payment)
{
   down_payment = (selling_price * downpayment_percentage);
   amount_of_loan = (selling_price - down_payment);

   double J = (rate_of_interest / 100.0) / 12.0;

   int N = 12 * number_of_years;

   double intermediate_step = (1.0 / (std::pow((1.0 + J), static_cast<double>(N))));

   double second_intermediate = J / (1.0 - intermediate_step);

   payment = second_intermediate * amount_of_loan;
}


What is the selling price? :
10000
What is the rate of interest?:
15
What is the number of years in loan?:
20

Output Test:
Selling Price: 10000
Rate of Interest: 15
Number of Years: 20
Amount of Loan: 8000
Down Payment: 2000
Monthly Payment: 105.343

Purpose: Monthly House Cost Calculation
Thank you all,

I took FurryGuy's recommendation to do intermediate steps and it worked perfectly.

Thanks!
closed account (E0p9LyTq)
Glad I could help. :)

As you can see, intermediate steps makes it less error prone. They make understanding what the code does MUCH easier as well! :D

Easier to read means easier to maintain.

Using intermediate steps in a complex calculation makes it easier to track down errors. Printing out the results of each step would help pinpoint where the error occurs.
Last edited on
Topic archived. No new replies allowed.