1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
//*************************************************************
//This program generates a table of factors used to
//compute monthly payment amounts for money borrowed.
//Again, this program does NOT compute monthly payment,
//only a table of factor values.
//*************************************************************
#include <iostream>//Header files
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
ofstream outfile;//File descriptor
//Prototypes
int openOutfile(); //Function opens the answer outfile
void getValues(double &rateA, double &rateB, double &inc, int &yearA, int &yearB);//Function gets interest rate range, increment, and year range
void createTable(double& rateA, double& rateB, double &inc, int &yearA, int &yearB);//Function creates a table of factors using user-provided values to compute
//Begin main
int main()
{
double rateA, rateB, inc;//Declare variables
int yearA; int yearB;
openOutfile(); //Call function 1
getValues(rateA, rateB, inc, yearA, yearB); //Call function 2
cout << " Monthly Payment Factors Used in Computing Monthly Payments" << endl;//Display table
cout << " Interest Rates " << endl;//title
cout << endl;
outfile << " Monthly Payment Factors Used in Computing Monthly Payments" << endl;//Echo
outfile << " Interest Rates " << endl;
outfile << endl;
createTable(rateA, rateB, inc, yearA, yearB); //Call function 3
outfile.close(); //Close outfile
return 0; //Quit
}
int openOutfile()//Function definition 1
{
outfile.open("answers.out");//Open an outfile for answers
if( !outfile) //Check to see if outfile opens correctly
{
cout << "Error opening output file." << endl; //If it doesn't open correctly, display message
return 0; //and quit.
}
}
void getValues(double &rateA, double &rateB, double &inc, int &yearA, int &yearB)//Function definition 2
{
cout << "Enter interest rate range (two values)," << endl //Prompt for interest
<< "and increment amount separated by spaces: " << endl;//rate values and increment amount
cin >> rateA >> rateB >> inc; //Read in interest rate values and increment amount
outfile << "Enter interest rate range (two values)," << endl //Echo
<< "and increment amount separated by spaces: " << rateA << " " << rateB << " " << inc << endl;
cout << "Enter the year range amount separated by a space: " << endl //Prompt for year range
<< "ex. 1 15" << endl;
cin >> yearA >> yearB; // Read in year range
outfile << "Enter the year range amount separated by a space: " << yearA << " " << yearB << endl << endl;//Echo
return;
}
void createTable(double &rateA, double &rateB, double &inc, int &yearA, int &yearB)//Function definition 3
{
double tempRate = rateA;//declare variable for beginning rate value
while ( tempRate <= rateB) //while it is less than or equal to the max rate value
{
double factor = 0;// declare and initialze variable for computing the factors
cout << " "; //Insert a space for formatting
outfile << " ";//Echo
for (int count = 0; count < 6 && tempRate <= rateB; count++)//For as long as count is less than or
{ //equal to 6 and your beginning value is less than or equal to max value
cout << fixed << setw(13) << setprecision(2) << tempRate;//Display the rate in increments
outfile << fixed << setw(13) << setprecision(2) << tempRate;//Echo
tempRate = tempRate + inc;//Increment your rate value
}
cout << endl;//New line for formatting
outfile << endl;//Echo
cout << "________________________________________________________________________________" << endl;//Display a line for formatting
outfile << "________________________________________________________________________________" << endl;//Echo
for (int count = yearA; count <= yearB; count++)//For as long as count is less than or equal to your max year
{
tempRate = rateA;//Declare and initialize variable to beginning rate value
cout << setw(2) << count;//Display the rate value
outfile << setw(2) << count;//Echo
for (int i = 0; i < 6 && tempRate <= rateB; i++)//For as long as i is less than or equal to 6 and
{ //tempRate value is less than max value
double eyeplusone = (tempRate /1200) + 1;//Declare variable for part the numerator of the equation
double exp = pow(eyeplusone, count * 12);//Declare variable for the entire numerator
double factor =(exp * tempRate/1200) / (exp -1);//Declare variable for entire equation
cout << fixed << setw(13)<< setprecision(5) << factor;//Display factor
outfile << fixed << setw(13)<< setprecision(5) << factor;//Echo
tempRate = tempRate + inc;//Increment factor
}
cout << endl;//Display line for formatting
outfile << endl;//Echo
}
cout << endl;//Display line for formatting
outfile << endl;//Echo
rateA = tempRate;//Initialize variable to last value for next set of columns
}
}
|