Tutorial 8 - Interest Calculator Application: Introducing

Download Report

Transcript Tutorial 8 - Interest Calculator Application: Introducing

Tutorial 8 - Interest Calculator
Application: Introducing the for
Repetition Statement and the Math Library
Outline
8.1
8.2
8.3
8.4
8.5
8.6
Test-Driving the Interest Calculator Application
Essentials of Counter-Controlled Repetition
Introducing the for Repetition Statement
Examples Using the for Statement
Constructing the Interest Calculator Application
Wrap-Up
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Objectives
• In this tutorial, you will learn to:
– Execute statements repeatedly with the for repetition statement.
– Use the math library to execute common mathematical functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.1
Test Driving the Interest Calculator
Application
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.1
Test Driving the Interest Calculator
Application (Cont.)
Figure 8.1 Running the completed Interest Calculator application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.1
Figure 8.2
Test Driving the Interest Calculator
Application (Cont.)
Completed Interest Calculator application output.
Displaying the result
in tabular format
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.2
Essentials of Counter-Controlled Repetition
• Essential elements of counter-controlled repetition
– Name of the control variable
– Initial value of the control variable
– The increment (or decrement) by which the control variable is
modified during each iteration of the loop
– The condition that tests for the final value of the control variable
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1
int counter = 2; // repetition counter
Names control variable and
initializes its value to 2
2
3
// format floating-point output with two digits of precision
4
cout << fixed << setprecision( 2 );
5
6
while ( counter <= 5 )
7
{
8
months = 12 * counter; // calculate payment period
Condition that tests whether value of
control variable is less than or equal
to 5, meaning that 5 is the final value
for which the condition is true
9
10
// get monthly payment
11
monthlyPayment = calculateMonthlyPayment(
12
monthlyInterest, months, loanAmount );
13
14
// display result
15
cout << months << left << setw( 10 ) << "$" << monthlyPayment
16
<< endl;
17
18
counter++; // increment counter
Increment counter by 1 for each iteration of the loop
19
20 } // end while
Figure 8.3 Examining counter-controlled repetition using a while statement.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.3
Introducing the for Repetition Statement
• for statement
– Specifies all elements of counter-controlled repetition in header
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1
// format floating-point output with two digits of precision
2
cout << fixed << setprecision( 2 );
3
4
for ( int counter = 2; counter <= 5; counter++ )
5
{
6
months = 12 * counter; // calculate payment period
7
8
// get monthly payment
9
monthlyPayment = calculateMonthlyPayment(
10
monthlyInterest, months, loanAmount );
11
12
// display value
13
cout << months << left << setw( 10 ) << "$" << monthlyPayment
14
<< endl;
15
16 } // end for
Figure 8.4 Code segment for the Car Payment Calculator application that
demonstrates the for statement.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Introducing the for Repetition Statement
(Cont.)
8.3
Figure 8.5
for header components.
• Common error
– Controlling a counter-controlled loop with a floating-point value
• Always use integer values to control counter-controlled loops
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.3
Figure 8.6
Introducing the for Repetition Statement
(Cont.)
for repetition statement UML activity diagram.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.4
Examples Using the for Statement
Vary the control variable from 1 to 100 in increments of 1
Vary the control variable from 100 to 1 in decrements of 1
Vary the control variable from 7 to 77 in increments of 7
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Figure 8.7
Constructing the Interest Calculator
Application
Pseudocode for the Interest Calculator application.
Prompt the user for and input the principal, interest rate and years
Display a table header
For each year, starting at 1 and ending with the number of years entered
Calculate and display the year
Calculate and display the current value of the investment
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Figure 8.8
Constructing the Interest Calculator
Application (Cont.)
Defining variables to store the principal, interest rate and investment duration.
Defining variables
to store user input
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Constructing the Interest Calculator
Application (Cont.)
Figure 8.9 Prompting the user for and inputting the principal, interest rate
and investment duration.
Prompting the user for
and inputting the
principal, interest rate
and investment duration
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Constructing the Interest Calculator
Application (Cont.)
Figure 8.10
Displaying a table header.
Displaying a
table header
• Using the setw and left stream manipulators to display a
table header
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Figure 8.11
Constructing the Interest Calculator
Application (Cont.)
Formatting floating-point numbers.
Formatting floatingpoint numbers
• Using the fixed and setprecision stream manipulators
to format text
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Figure 8.12
Constructing the Interest Calculator
Application (Cont.)
Creating the for statement.
Empty for statement
• for header declares all essential items of counter-controlled
repetition
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Figure 8.13
Constructing the Interest Calculator
Application (Cont.)
Including the <cmath> standard library header file.
Including <cmath>
• C++ Standard Math Library
– <cmath>
• Necessary for the pow function
– Performs exponentiation (there is no exponential operator in C++)
• Also includes functions for square roots and trigonometry etc.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Figure 8.14
Constructing the Interest Calculator
Application (Cont.)
Calculating amount on deposit after specified number of years.
Using <cmath> function
pow to calculate the
amount on deposit after
the specified number of
years
• pow function
– Takes two arguments
• First argument is value to be raised to a power
• Second argument specifies power to which the first argument will be raised
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Figure 8.15
Constructing the Interest Calculator
Application (Cont.)
Displaying the amount on deposit for each year.
Displaying the amount
on deposit for each year
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8.5
Figure 8.16
Constructing the Interest Calculator
Application (Cont.)
Completed application output.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1
// Tutorial 8: InterestCalculator.cpp
2
// Calculate the total value of an investment.
3
#include <iostream> // required to perform C++ stream I/O
4
#include <iomanip> // required for parameterized stream manipulators
5
#include <cmath>
InterestCalculator.cpp
(1 of 3)
// required to use the C++ math library functions
6
7
using namespace std; // for accessing C++ Standard Library members
8
9
// function main begins program execution
Include the <cmath>
standard library
header file
10 int main()
11 {
12
// define variables
13
double principal; // principal
14
double rate;
// interest rate
15
int years;
// investment duration
Define variables to store user input
16
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
17
// prompt the user for and input principal, interest rate
18
// and investment duration
19
cout << "\nEnter principal: ";
20
cin >> principal; // get principal
InterestCalculator.cpp
(2 of 3)
21
22
cout << "Enter interest rate: ";
23
cin >> rate; // get interest rate
24
25
cout << "Enter number of years for deposit: ";
26
cin >> years; // get years for deposit
Prompt for and input the
principal, rate and year
27
28
// display header for yearly balances
29
cout << “\n” << left << setw( 10 ) << "Year"
30
<< "Amount on Deposit" << endl;
31
32
// set the format for displaying dollar values
33
cout << fixed << setprecision( 2 );
Display the table header and
format floating-point values
34
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
35
// calculate the total balance for each year
36
for ( int count = 1; count <= years; count++ )
37
{
38
// calculate balance at end of current year
39
double amount =
40
principal * pow( ( 1 + rate / 100 ), count );
41
42
// display year and balance
43
cout << setw( 10 ) << count << "$" << amount << endl;
44
InterestCalculator.cpp
(3 of 3)
} // end for
Use a for statement to
calculate the amount on deposit
and display it in tabular format
45
46
cout << "\n"; // insert newline for readability
47
48
return 0; // indicate that program ended successfully
49
50 } // end function main
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.