1 A SSIGNMENT 4 –M ORTGAGE A MORTIZATION T ABLE –W ITH C OMMA S EPARATED CURRENC
ID: 3734683 • Letter: 1
Question
1
ASSIGNMENT 4 –MORTGAGE AMORTIZATION TABLE –WITH COMMA SEPARATED CURRENCY FORMAT
This assignment is to use your existing program of assignment 2 to include additional number formatting functions to format your report in the output file using comma separated currency numbers. Your Input Data Set for the test run is as follows:
Your submission should follow the format as shown in the following submission example (partial codes are provided here):
// Author: John Doe
// CIS 3100 BMWA Spring 2018
// Assignment 4
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
int length(double n)
{
if (n >= 10)
{
int d = log10(n);
return d + d / 3 + 4;
}
else
return 4;
}
void commaNumber(ofstream& o, double x)
{
int n, f; // n is integer part and f is the fraction
x = round(x * 100) / 100;
……Your code is here…..
}
2
void blanks(ofstream& o, int n)
{
for (int i = 1; i <= n; i++)
o << " ";
}
void printAccountNumber(ofstream& o, double x, int s, int c) // x is the number, s is the total number of spaces, c is position - 1 for left, 2 for centerm 3 for right-justified
{
int b = s - length(x);
int h = b / 2;
switch (c)
{
case 1:
commaNumber(o, x);
blanks(o, b);
break;
case 2:
blanks(o, h);
commaNumber, (o, x);
blanks(o, b - h);
break;
case 3:
blanks(o, b);
commaNumber(o, x);
break;
default:
cout << "Wrong code" << endl;
break;
}
}
int main()
{
const int SPACE = 12;
double loanAmount, intRate, addtlPrincipal;
int years;
………….Your code is here …………..
string fileName;
cout << "Send the mortgage amortization table to a file (enter file name): ";
cin >> fileName;
ofstream outFile(fileName);
double monIntRate = intRate / 1200;
double mthlyPymt = loanAmount*monIntRate / (1 - 1 / pow(1 + monIntRate, years * 12));
double actPymt = mthlyPymt + addtlPrincipal;
double balance = loanAmount;
double interest;
double principal;
3
outFile << endl << endl;
outFile << " MORTGAGE AMORTIZATION TABLE" << endl;
outFile << " Amount: " << "$";
printAccountNumber(outFile, loanAmount, SPACE, 3);
outFile << endl;
outFile << "Interest Rate: " << intRate << "%" << endl;
outFile << "Term(Years): " << years << endl;
outFile << "Monthly Payment: " << "$";
printAccountNumber(outFile, mthlyPymt, SPACE, 3);
outFile << endl;
outFile << "Additonal Principal: " << "$";
printAccountNumber(outFile, addtlPrincipal, SPACE, 3);
outFile << endl;
outFile << "Actual Payment: " << "$";
printAccountNumber(outFile, actPymt, SPACE, 3);
outFile << endl << endl;
outFile << " ";
blanks(outFile, SPACE - 9);
outFile << "Principal ";
blanks(outFile, SPACE - 8);
outFile << "Interest ";
blanks(outFile, SPACE - 7);
outFile << "Balance" << endl;
………………. Your code is here ………………
outFile.close();
system("pause");
return 0;
}
4
MORTGAGE AMORTIZATION TABLE
Amount: $ 298,000.00
Interest Rate: 4.625%
Term(Years): 10
Monthly Payment: $ 3,106.40
Additonal Principal: $ 709.64
Actual Payment: $ 3,816.05
Principal Interest Balance
1 $ 2,667.51 $ 1,148.53 $ 295,332.47
2 2,677.80 1,138.25 292,654.67
3 2,688.11 1,127.94 289,966.55
4 2,698.48 1,117.57 287,268.07
5 2,708.88 1,107.18 284,559.19
6 2,719.32 1,096.74 281,839.86
7 2,729.80 1,086.25 279,110.05
8 2,740.32 1,075.74 276,369.73
9 2,750.88 1,065.18 273,618.84
10 2,761.48 1,054.56 270,857.35
11 2,772.13 1,043.93 268,085.22
12 2,782.82 1,033.25 265,302.40
13 2,793.53 1,022.51 262,508.86
14 2,804.30 1,011.75 259,704.55
15 2,815.11 1,000.94 256,889.44
16 2,825.96 990.09 254,063.47
17 2,836.86 979.20 251,226.60
18 2,847.78 968.26 248,378.82
19 2,858.76 957.28 245,520.04
20 2,869.78 946.27 242,650.26
21 2,880.84 935.21 239,769.42
22 2,891.94 924.11 236,877.47
5
23 2,903.09 912.97 233,974.36
24 2,914.28 901.77 231,060.07
25 2,925.51 890.53 228,134.57
26 2,936.78 879.26 225,197.76
27 2,948.11 867.95 222,249.66
28 2,959.46 856.59 219,290.17
29 2,970.88 845.17 216,319.29
30 2,982.32 833.73 213,336.97
31 2,993.82 822.24 210,343.14
32 3,005.36 810.70 207,337.77
33 3,016.94 799.11 204,320.82
34 3,028.57 787.49 201,292.26
35 3,040.25 775.80 198,252.01
36 3,051.96 764.10 195,200.04
37 3,063.73 752.33 192,136.30
38 3,075.53 740.52 189,060.77
39 3,087.38 728.66 185,973.39
40 3,099.28 716.76 182,874.10
41 3,111.23 704.83 179,762.85
42 3,123.23 692.84 176,639.64
43 3,135.26 680.79 173,504.36
44 3,147.34 668.71 170,357.02
45 3,159.48 656.58 167,197.54
46 3,171.65 644.40 164,025.89
47 3,183.88 632.17 160,842.01
48 3,196.15 619.90 157,645.85
49 3,208.46 607.59 154,437.39
50 3,220.82 595.23 151,216.55
51 3,233.25 582.80 147,983.30
52 3,245.71 570.35 144,737.60
53 3,258.21 557.84 141,479.38
54 3,270.78 545.28 138,208.60
6
55 3,283.38 532.67 134,925.22
56 3,296.03 520.01 131,629.17
57 3,308.73 507.31 128,320.44
58 3,321.48 494.56 124,998.94
59 3,334.30 481.76 121,664.64
60 3,347.15 468.92 118,317.50
61 3,360.05 456.01 114,957.46
62 3,373.00 443.06 111,584.46
63 3,386.00 430.06 108,198.47
64 3,399.05 417.00 104,799.41
65 3,412.15 403.91 101,387.27
66 3,425.30 390.75 97,961.97
67 3,438.50 377.56 94,523.47
68 3,451.75 364.31 91,071.72
69 3,465.05 351.00 87,606.66
70 3,478.40 337.64 84,128.25
71 3,491.82 324.24 80,636.42
72 3,505.28 310.79 77,131.16
73 3,518.78 297.27 73,612.36
74 3,532.34 283.70 70,080.02
75 3,545.96 270.10 66,534.05
76 3,559.63 256.43 62,974.43
77 3,573.34 242.71 59,401.08
78 3,587.11 228.93 55,813.95
79 3,600.94 215.12 52,213.01
80 3,614.82 201.24 48,598.19
81 3,628.76 187.31 44,969.44
82 3,642.73 173.31 41,326.69
83 3,656.78 159.28 37,669.91
84 3,670.88 145.18 33,999.04
85 3,685.01 131.03 30,314.00
86 3,699.23 116.84 26,614.77
7
87 3,713.48 102.57 22,901.29
88 3,727.80 88.26 19,173.50
89 3,742.15 73.90 15,431.34
90 3,756.59 59.46 11,674.75
91 3,771.07 45.00 7,903.68
92 3,785.59 30.46 4,118.09
93 3,800.19 15.86 317.89
94 317.89 1.23 0.00
This assignment is to use your existing program of assignment 2 to include additional number formatting functions to format your report in the output file using comma separated currency numbers. Your Input Data Set for the test run is as follows: CWindowslsystem32cmd.exe Enter Loan amount G-9999999>. for example 300000.90 500600 Enter annual interest rate(0-50), for example 4.25 mean ing 4.25%: 4.125 Enter no. of years as integex 1-50for example 30: 15 Enter additional principal per month (0-9999999>. for example 300: 800 Send the mortgage amortization table to a fileExplanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//function prototypes
double calculateMonthlyPayment(double&, double&, double&, int&, int&);
void printAmortTable(double, double, double, double, int, int, ofstream&);
int main()
{
//variable declarations
int menuChoice; //allows program to branch between three options
double monthlyPayment;//holds return value from calculate function
double principal;//original amount of loan, passed to functions
double rate;//more specifically, this is the interest rate
int years;//length of loan to be converted into months
int months; //necessary conversion for sake of the equation
double monthlyPercentRate; //this value is 1/12 of the rate entered
ofstream outputFile;//necessary to write data to .txt document
//create the output file and check if creation was successful.
outputFile.open("mortgageData.txt");
if (outputFile)
{
//the program gives the user three options.
do //the do-while loop is for input validation
{
cout << "What would you like to do? Enter 1, 2, or 3. "
<< "1. Calculate a monthly payment "
<< "2. Print an amortization table "
<< "3. Quit ";
cin >> menuChoice;
} while (menuChoice <1 || menuChoice > 3);
}
//based on menuChoice, the program has branches.
if (menuChoice == 1)
{
//call monthlyPayment function, assign return value
monthlyPayment = calculateMonthlyPayment(principal, rate, monthlyPercentRate, years, months);
//display the monthly payment and write to a file
cout << fixed << showpoint << setprecision(2);
outputFile << fixed << showpoint << setprecision(2);
cout << "Monthly Payment: "
<< "Principal: " << principal << endl
<< "Annual Interest Rate: " << rate << endl
<< "Term: " << years << " years" << endl
<< "Payments: " << monthlyPayment << endl;
outputFile << "Monthly Payment: "
<< "Principal: $" << principal << endl
<< "Annual Interest Rate: " << rate << "%" << endl
<< "Term: " << years << " years" << endl
<< "Payments: $" << monthlyPayment << endl;
}
//option two prints the amortization table
if (menuChoice == 2)
{
//call monthlyPayment function, assign return value
monthlyPayment = calculateMonthlyPayment(principal, rate,monthlyPercentRate, years, months);
//call printAmortTable function, parameters by value
printAmortTable(monthlyPayment, principal, rate, monthlyPercentRate,
years, months, outputFile);
}
//option three thanks the user and stops the program
if (menuChoice == 3)
cout << "Thank you for using this program. Close the window "
<< "anytime ";
//close the file
outputFile.close();
return 0;
}
//output is printed in main to demonstrate pass by reference
double calculateMonthlyPayment(double &principal, double &rate,double &monthlyPercentRate, int &years, int&months)
{
//function variables
double monthlyPayment; //value to be returned to main
double decimalRate; //holds conversion from annual rate entered as a percentage
//ask the user to enter the principle, interest rate, and years
cout << "Please enter the principal on the loan. ";
cin >> principal;
cout << "Next, enter the interest rate at as a percentage. ";
cin >> rate;
cout << "Finally, enter the number of years on the loan. ";
cin >> years;
//make necessary caluclations and conversions
decimalRate = rate / 100;//convert percentage to a decimal value
monthlyPercentRate = decimalRate / static_cast<float>(12);//convert rate to monthly rate for equation
months = years * 12;//convert years to months for equation
//calculate the monthly payment
monthlyPayment = (monthlyPercentRate * principal) / (1 - (1 / (pow((1 + monthlyPercentRate), months))));
return monthlyPayment;
}
//output is printed in function because it is void and the name suggests so
void printAmortTable(double monthlyPayment, double principal, double rate,double monthlyPercentRate, int years, int months, ofstream& outputFile)
{
//function variables
int paymentNumber;//counter for loop, compared against months in the loan
double interest;//dollar amount found when decimalRate * principal
double paidOnPrincipal;//monthlyPayment - interest, amount paid off of loan
//display and write the headers of the table
cout << fixed << showpoint << setprecision(2);
outputFile << fixed << showpoint << setprecision(2);
cout << " Mortgage Amortization Table "
<< " Loan: $" << principal << endl
<< " Rate: " << rate << "% "
<< " Years: " << years << " "
<< " Monthly Payment: $" << monthlyPayment << endl << endl;
cout << "Payment Beginning Paid On "
<< "Number Balance Payment Interest Principal NewBalance ";
outputFile << " Mortgage Amortization Table "
<< " Loan: $" << principal << endl
<< " Rate: " << rate << "% "
<< " Years: " << years << " "
<< " Monthly Payment: $" << monthlyPayment << endl << endl;
outputFile << "Payment Beginning Paid On "
<< "Number Balance Payment Interest Principal NewBalance ";
//main table loop
for (paymentNumber = 1; paymentNumber <= months; paymentNumber++)
{
//check to see if this iteration will be the last payment
if (paymentNumber == months)
{
interest = principal * monthlyPercentRate;
monthlyPayment = principal + interest;//final payment must be remaining balance
}
//the loop prints as it calculates so that arrays are not needed
if (paymentNumber <= 6 || paymentNumber >= (months - 5))//prints original values
{
//print out currently held values
cout << left << setw(6) << paymentNumber << " " << setw(10) << principal << " " << setw(7)
<< monthlyPayment << " ";
outputFile << left << setw(6) << paymentNumber << " " << setw(10) << principal << " " << setw(7)
<< monthlyPayment << " ";
}
if (paymentNumber == 7)//add blank lines
{
cout << endl << endl << endl;
outputFile << endl << endl << endl;
}
//calculate interest, paid on principal, and new balance
if (paymentNumber < months)//on final iteration, must be calculated before payment.
{
interest = principal * monthlyPercentRate;
}
//calculations regardless of iteration
paidOnPrincipal = monthlyPayment - interest;
principal = principal - paidOnPrincipal;
if (paymentNumber <= 6 || paymentNumber >= (months - 5))//prints calculated values
{
//print out results of calculations, .005 added to round values to change amounts
cout << setw(8) << (interest + .005) << " " << setw(6) << paidOnPrincipal << " " << principal << endl;
outputFile << setw(8) << (interest + .005) << " " << setw(6) << paidOnPrincipal << " " << principal << endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.