Alright, what I'm trying to do is use a for loop to calculate a monthly payment using a position in the array, and using a pointer to change the original value of the array to what I calculate. I then want to move to the next position in the array and repeat it all over again until I've used all array positions.
I can successfully do this by using
however, I don't think that my teacher wants me to use this method for this assignment. The assignment says to "-create a function in a separate header file that uses a for loop to calculate the monthly payment for each of the loan amounts." and "-Use the pointer to the loan amounts to calculate the monthly payments to avoid returning them to main."Code:void calculatePayment( double *p ) { for ( int i = 0; i < 5; i++) { p[i] = ( .05 + .05 / (pow(1 + .05, 36) -1 ) ) * p[i]; } }
She includes the following example:
*p = (interest + interest / (pow(1 + interest,months) -1) ) *p;
p++;
I cannot figure out how to successfully include that in a for loop. Everything I've tried has resulted in errors that I can't make any sense out of. I would like some input on what I can do. I will include my entire program just to be safe.
Code:// loan.cpp : Defines the entry point for the console application. // #include "stdafx.h" using std::cout; using std::endl; using std::fixed; #include <iomanip> using std::setprecision; #include <cmath> using std::pow; #include "loan.h" int main() { Loan myLoan; double loan[] = {15000, 18000, 20000, 22000, 25000}; double *loanPtr; loanPtr = loan; for (int i = 0; i < 5; i++) { cout << "Loan amount" << i << " is $" << fixed << setprecision( 2 ) << loanPtr[i] << endl; } cout << endl; myLoan.calculatePayment( loanPtr ); for (int i = 0; i < 5; i++) { cout << "The payment amount for amount" << i << " is $" << fixed << setprecision( 2 ) << loanPtr[i] << endl; } return 0; }Code://loan.h #include <cmath> using std::pow; class Loan { public: void calculatePayment( double *p ) { for ( int i = 0; i < 5; i++) { p[i] = ( .05 + .05 / (pow(1 + .05, 36) -1 ) ) * p[i]; } } };



LinkBack URL
About LinkBacks


