Question

/* Homework Lab 4 - Using manipulators to format output */ //***************************************************************** // Mortgage Payment Calculator...

/* Homework Lab 4

- Using manipulators to format output
*/

//*****************************************************************
// Mortgage Payment Calculator program
// This program determines the monthly payments on a mortgage given
// the loan amount, the yearly interest, and the number of years.
//*****************************************************************

#include <iostream> // Access cout
#include <cmath> // Access power function
#include <iomanip> // Access manipulators
using namespace std;

const float LOAN_AMOUNT = 50000.00; // Amount of the loan
const float YEARLY_INTEREST = 0.0524; // Yearly interest rate
const int NUMBER_OF_YEARS = 7; // Number of years

int main()
{
   // local variables
   float monthlyInterest; // Monthly interest rate
   int numberOfPayments; // Total number of payments
   float monthlyPayment; // Monthly payment

   // Calculate values
   monthlyInterest = YEARLY_INTEREST / 12;
   numberOfPayments = NUMBER_OF_YEARS * 12;
   monthlyPayment = (LOAN_AMOUNT * pow(monthlyInterest + 1, numberOfPayments)
       * monthlyInterest)
       / (pow(monthlyInterest + 1, numberOfPayments) - 1);

   // Code to produce the Original Program Output

   cout << "---------------- Original program output -----------------" << endl << endl;

   cout << fixed << setprecision(2) << "For a loan amount of "
       << LOAN_AMOUNT << ", an interest rate of " << YEARLY_INTEREST << "," << endl
       << "and " << NUMBER_OF_YEARS << " year mortgage, your monthly payments are $"
       << monthlyPayment << "." << endl << endl;

   /* TODO: Step 1

   Write additional code to produce the output results shown
   in Step 1 Output below to right justify all values.
   Set the floating point format just once using the fixed, showpoint,
   and setprecision() manipulators. Use the setw() manipulator
   repeatedly on each line (once for each of the two columns)
   of output to adjust the column widths.

   NOTE: You are NOT allowed to add blank spaces to the beginning
   or end of strings to make them line up properly. You MUST use
   'setw' properly to achieve the defined results.

   ALSO, make sure you use the defined constants and variables in
   your output statements - DO NOT "hard code" the numeric values
   within the cout statements.
   */

   cout << "----------------- Program Output Step 1 ------------------" << endl << endl;

   // <Step 1 code here>

   /* TODO: Step 2

   Write additional code to produce the output results shown
   in Program Output Step 2 below to add 4-digit precision
   to interest rate (while also keeping Interest Rate and Monthly
   Payments as 2 digits), and line up all numeric values by their
   decimal points. Use setprecision() more than once and adjust
   field width (via setw) as necessary.

   NOTE: You are NOT allowed to add blank spaces to the beginning
   or end of strings to make them line up properly. You MUST use
   'setw' properly to achieve the defined column results.
   */
  
   cout << "----------------- Program Output Step 2 ------------------" << endl << endl;

   // <Step 2 code here>
  


   return 0;
}

/*

---------------- Original program output -----------------

For a loan amount of 50000.00, an interest rate of 0.05,
and 7 year mortgage, your monthly payments are $712.35.

----------------- Program Output Step 1 ------------------

Loan Amount: 50000.00
Interest Rate: 0.05
Years: 7

Monthly Payments: 712.35

----------------- Program Output Step 2 ------------------

Loan Amount: 50000.00
Interest Rate: 0.0524
Years: 7

Monthly Payments: 712.35

Press any key to continue . . .

*/

Homework Answers

Answer #1

Program

/* Homework Lab 4
- Using manipulators to format output
*/

//*****************************************************************
// Mortgage Payment Calculator program
// This program determines the monthly payments on a mortgage given
// the loan amount, the yearly interest, and the number of years.
//*****************************************************************

#include <iostream> // Access cout
#include <cmath> // Access power function
#include <iomanip> // Access manipulators
using namespace std;

const float LOAN_AMOUNT = 50000.00; // Amount of the loan
const float YEARLY_INTEREST = 0.0524; // Yearly interest rate
const int NUMBER_OF_YEARS = 7; // Number of years

int main()
{
// local variables
float monthlyInterest; // Monthly interest rate
int numberOfPayments; // Total number of payments
float monthlyPayment; // Monthly payment

// Calculate values
monthlyInterest = YEARLY_INTEREST / 12;
numberOfPayments = NUMBER_OF_YEARS * 12;
monthlyPayment = (LOAN_AMOUNT * pow(monthlyInterest + 1, numberOfPayments)
* monthlyInterest)
/ (pow(monthlyInterest + 1, numberOfPayments) - 1);

// Code to produce the Original Program Output

cout << "---------------- Original program output -----------------" << endl << endl;

cout << fixed << setprecision(2) << "For a loan amount of "
<< LOAN_AMOUNT << ", an interest rate of " << YEARLY_INTEREST << "," << endl
<< "and " << NUMBER_OF_YEARS << " year mortgage, your monthly payments are $"
<< monthlyPayment << "." << endl << endl;

/* TODO: Step 1

Write additional code to produce the output results shown
in Step 1 Output below to right justify all values.
Set the floating point format just once using the fixed, showpoint,
and setprecision() manipulators. Use the setw() manipulator
repeatedly on each line (once for each of the two columns)
of output to adjust the column widths.
  
NOTE: You are NOT allowed to add blank spaces to the beginning
or end of strings to make them line up properly. You MUST use
'setw' properly to achieve the defined results.
  
ALSO, make sure you use the defined constants and variables in
your output statements - DO NOT "hard code" the numeric values
within the cout statements.

*/
  
cout << "----------------- Program Output Step 1 ------------------" << endl << endl;

// <Step 1 code here>

cout << left << setw(20) << "Loan Amount: " << fixed << setprecision(2) << right << setw(10) << LOAN_AMOUNT << endl
<< left << setw(20) << "Interest Rate: " << setprecision(2) << right << setw(10) << YEARLY_INTEREST << endl
<< left << setw(20) << "Years: " << right << setw(7) << NUMBER_OF_YEARS << endl
<< left << setw(20) << "Monthly Payment: " << setprecision(2) << right << setw(10) << monthlyPayment << endl << endl;

/* TODO: Step 2

Write additional code to produce the output results shown
in Program Output Step 2 below to add 4-digit precision
to interest rate (while also keeping Interest Rate and Monthly
Payments as 2 digits), and line up all numeric values by their
decimal points. Use setprecision() more than once and adjust
field width (via setw) as necessary.
  
NOTE: You are NOT allowed to add blank spaces to the beginning
or end of strings to make them line up properly. You MUST use
'setw' properly to achieve the defined column results.

*/
  
cout << "----------------- Program Output Step 2 ------------------" << endl << endl;

// <Step 2 code here>
  
cout << left << setw(20) << "Loan Amount: " << fixed << setprecision(2) << right << setw(10) << LOAN_AMOUNT << endl
<< left << setw(20) << "Interest Rate: " << setprecision(4) << right << setw(12) << YEARLY_INTEREST << endl
<< left << setw(20) << "Years: " << right << setw(7) << NUMBER_OF_YEARS << endl
<< left << setw(20) << "Monthly Payment: " << setprecision(2) << right << setw(10) << monthlyPayment << endl;
  
cout << "Press any key to continue . . ." << endl;

return 0;
}

Sample Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C ++ program that will read in prices and store them into a two-dimensional array //...
C ++ program that will read in prices and store them into a two-dimensional array // It will print those prices in a table form and the lowest price. // EXAMPLE // Input: // Please input the number of rows from 1 to 10 // 2 // Please input the number of columns from 1 to 10 // 3 // // Input price of item (0,0): 2 // Input price of item (0,1): 4 // Input price of item (0,2):...
// This program prints a table to convert numbers from one unit to another. // The...
// This program prints a table to convert numbers from one unit to another. // The program illustrates some implementation techniques. //Include the header file ostream for including all stream. ---------------------------------------------------------*/ #include <iostream> //Provides cout <v1.0> //Include iomanip that is used for set precision. #include <iomanip> //Provides setw function for setting output width <v1.1> //Header file for EXIT_SUCCESS. #include <stdlib.h>// Provides EXIT_SUCCESS <v1.2> //Header file for assert. #include <assert.h>// Provides assert function <1.3> using namespace std; // Allows all standard...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee). Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero. Write the chargeFee member function that updates the balance by...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
Part B: Planning problems using Excel Mortgage payments : ( 10 % of this assignment) Loan...
Part B: Planning problems using Excel Mortgage payments : ( 10 % of this assignment) Loan amount : $200,000 Interest rate (annual): 4.00 % Term : 30 years Calculate monthly payments: ( Hint : Rate and number of periods should be adjusted for monthly) Total interest amount over the life of the loan In reference to (1, above). ( 10 % of this assignment) Calculate PMT ( Monthly payments) if you want to pay of the above loan in 20...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
EXHIBIT 5.9 A Table of Monthly Mortgage Payments (Monthly Payments Necessary to Repay a $10,000 Loan)...
EXHIBIT 5.9 A Table of Monthly Mortgage Payments (Monthly Payments Necessary to Repay a $10,000 Loan) The monthly loan payments on a mortgage vary not only by the amount of the loan, but also by the rate of interest and loan maturity. LOAN MATURITY Rate of Interest 10 Years 15 Years 20 Years 25 Years 30 Years 5.0% $106.07 $79.08 $66.00 $58.46 $53.68 5.5 108.53 81.71 68.79 61.41 56.79 6.0 111.02 84.39 71.64 64.43 59.96 6.5 113.55 87.11 74.56 67.52...
Required information [The following information applies to the questions displayed below.] NOTE: Throughout this lab, every...
Required information [The following information applies to the questions displayed below.] NOTE: Throughout this lab, every time a screenshot is requested, use your computer's screenshot tool, and paste each screenshot to the same Word document. Label each screenshot in accordance to what is noted in the lab. This document with all of the screenshots included should be uploaded through Connect as a Word or PDF document when you have reached the final step of the lab. In this lab, you...
You take out a 25-year mortgage for $300,000 to buy a new house. What will your...
You take out a 25-year mortgage for $300,000 to buy a new house. What will your monthly payments be if the interest rate on your mortgage is 8 percent? Now, calculate the portion of the 48th monthly payment that goes toward interest and principal. Complete the steps below using cell references to given data or previous calculations. In some cases, a simple cell reference is all you need. To copy/paste a formula across a row or down a column, an...