Correct the code by using the global variable to move the calculations such as (downPayment = sellingPrice * DOWN_PAYMENT_RATE / 100.0) out of the output function. In fact, those calculations have been done in calcMortgagePayment(). Use the reference parameter when needed.in calcMortgagePayment().
Max Function
Statistical function is of many categories. One of them is a MAX function. The MAX function returns the largest value from the list of arguments passed to it. MAX function always ignores the empty cells when performing the calculation.
Power Function
A power function is a type of single-term function. Its definition states that it is a variable containing a base value raised to a constant value acting as an exponent. This variable may also have a coefficient. For instance, the area of a circle can be given as:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
#define PROPERTY_TAX_RATE_PER_YEAR 1.25
#define COST_OF_UTILITIES_PER_MONTH 300.00
#define COST_OF_INSURANCE_PER_YEAR 550.00
void inputData(double& sellingPrice, double& interestRate, int& durationOfLoan);
void handleAllComputations(double sellingPrice,
double interestRate,
int durationOfLoan,
double downPaymentRate,
double& downPayment,
double& amountOfLoan,
double& mortgagePayment,
double& propertyTax,
double& costOfInsurance,
double& totalMonthlyHouseCost);
double calculateDownPayment(double sellingPrice, double downPaymentRate);
double calculateAmountOfLoan(double sellingPrice, double downPayment);
double calculateMortgagePayment(double amountOfLoan,
double interestRate,
int durationOfLoan);
void outputResults(double sellingPrice,
double downPayment,
double amountOfLoan,
double interestRate,
int durationOfLoan,
double mortgagePayment,
double propertyTax,
double costOfInsurance,
double totalMonthlyHouseCost);
int main()
{
const double DOWN_PAYMENT_RATE = 20.00;
double sellingPrice = 0.0;
double interestRate = 0.0;
int durationOfLoan = 0;
double downPayment = 0.0;
double amountOfLoan = 0.0;
double mortgagePayment = 0.0;
double propertyTax = 0.0;
double costOfInsurance = 0.0;
double totalMonthlyHouseCost = 0.0;
inputData(sellingPrice, interestRate, durationOfLoan);
handleAllComputations(sellingPrice, interestRate, durationOfLoan,
DOWN_PAYMENT_RATE, downPayment, amountOfLoan,
mortgagePayment, propertyTax, costOfInsurance,
totalMonthlyHouseCost);
outputResults(sellingPrice, downPayment, amountOfLoan, interestRate,
durationOfLoan, mortgagePayment, propertyTax, costOfInsurance,
totalMonthlyHouseCost);
return 0;
}
void inputData(double& sellingPrice,
double& interestRate,
int& durationOfLoan)
{
cout << "Enter the selling price: $";
cin >> sellingPrice;
cout << "Enter the rate of interest: ";
cin >> interestRate;
cout << "Enter the number of years for the loan: ";
cin >> durationOfLoan;
}
void handleAllComputations(double sellingPrice,
double interestRate,
int durationOfLoan,
double downPaymentRate,
double& downPayment,
double& amountOfLoan,
double& mortgagePayment,
double& propertyTax,
double& costOfInsurance,
double& totalMonthlyHouseCost)
{
downPayment = calculateDownPayment(sellingPrice, downPaymentRate);
amountOfLoan = calculateAmountOfLoan(sellingPrice, downPayment);
mortgagePayment = calculateMortgagePayment(amountOfLoan, interestRate, durationOfLoan);
propertyTax = sellingPrice * (PROPERTY_TAX_RATE_PER_YEAR / 100) / 12;
costOfInsurance = COST_OF_INSURANCE_PER_YEAR / 12;
totalMonthlyHouseCost = mortgagePayment + COST_OF_UTILITIES_PER_MONTH + propertyTax + costOfInsurance;
}
double calculateDownPayment(double sellingPrice, double downPaymentRate)
{
return sellingPrice * downPaymentRate / 100.0;
}
double calculateAmountOfLoan(double sellingPrice, double downPayment)
{
return sellingPrice - downPayment;
}
double calculateMortgagePayment(double amountOfLoan,
double interestRate,
int durationOfLoan)
{
double a = amountOfLoan;
double i = (interestRate / 100) / 12;
int n = durationOfLoan * 12;
double mortgagePayment = (a * i * pow(1 + i, n)) / (pow(1 + i, n) - 1);
return mortgagePayment;
}
void outputResults(double sellingPrice,
double downPayment,
double amountOfLoan,
double interestRate,
int durationOfLoan,
double mortgagePayment,
double propertyTax,
double costOfInsurance,
double totalMonthlyHouseCost)
{
cout << fixed << setprecision(2);
cout << endl
<< "MONTHLY COST OF HOUSE" << endl
<< endl;
cout << left << setw(30) << "SELLING PRICE"
<< "$" << right << setw(9) << sellingPrice << endl;
cout << left << setw(30) << "DOWN PAYMENT" << right << setw(10) << downPayment
<< endl;
cout << left << setw(30) << "AMOUNT OF LOAN" << right << setw(10)
<< amountOfLoan << endl;
cout << left << setw(30) << "INTEREST RATE" << right << setw(10)
<< interestRate << "%" << endl;
cout << left << setw(30) << "TAX RATE" << right << setw(10)
<< PROPERTY_TAX_RATE_PER_YEAR << "%" << endl;
cout << left << setw(30) << "DURATION OF LOAN (YEARS)" << right << setw(10)
<< durationOfLoan << endl;
cout << endl
<< "MONTHLY PAYMENT" << endl;
cout << setw(5) << "" << left << setw(25) << "MORTGAGE" << right << setw(10)
<< mortgagePayment << endl;
cout << setw(5) << "" << left << setw(25) << "UTILITIES" << right << setw(10)
<< COST_OF_UTILITIES_PER_MONTH << endl;
cout << setw(5) << "" << left << setw(25) << "PROPERTY TAXES" << right
<< setw(10) << propertyTax << endl;
cout << setw(5) << "" << left << setw(25) << "INSURANCE" << right << setw(10)
<< costOfInsurance << endl;
cout << left << setw(30) << ""
<< "__________" << endl;
cout << setw(30) << ""
<< "$" << right << setw(9) << setprecision(2) << totalMonthlyHouseCost
<< endl;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images