Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.

Similar presentations


Presentation on theme: "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing."— Presentation transcript:

1 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing the for Repetition Statement and the Math Library Outline 8.1 Test-Driving the Interest Calculator Application 8.2 Essentials of Counter-Controlled Repetition 8.3 Introducing the for Repetition Statement 8.4 Examples Using the for Statement 8.5 Constructing the Interest Calculator Application 8.6 Wrap-Up

2 © 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.

3 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.1Test Driving the Interest Calculator Application

4 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 8.1 Running the completed Interest Calculator application. 8.1Test Driving the Interest Calculator Application (Cont.)

5 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.1Test Driving the Interest Calculator Application (Cont.) Figure 8.2 Completed Interest Calculator application output. Displaying the result in tabular format

6 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.2Essentials 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

7 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 8.3 Examining counter-controlled repetition using a while statement. Names control variable and initializes its value to 2 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 Increment counter by 1 for each iteration of the loop

8 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.3Introducing the for Repetition Statement for statement –Specifies all elements of counter-controlled repetition in header

9 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 8.4 Code segment for the Car Payment Calculator application that demonstrates the for statement.

10 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 8.5 for header components. 8.3Introducing the for Repetition Statement (Cont.) Common error –Controlling a counter-controlled loop with a floating-point value Always use integer values to control counter-controlled loops

11 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 8.6 for repetition statement UML activity diagram. 8.3Introducing the for Repetition Statement (Cont.)

12 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.4Examples 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

13 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing 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 Figure 8.7 Pseudocode for the Interest Calculator application.

14 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing the Interest Calculator Application (Cont.) Figure 8.8 Defining variables to store the principal, interest rate and investment duration. Defining variables to store user input

15 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing 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

16 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing the Interest Calculator Application (Cont.) Using the setw and left stream manipulators to display a table header Figure 8.10 Displaying a table header. Displaying a table header

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing the Interest Calculator Application (Cont.) Using the fixed and setprecision stream manipulators to format text Figure 8.11 Formatting floating-point numbers. Formatting floating- point numbers

18 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing the Interest Calculator Application (Cont.) for header declares all essential items of counter-controlled repetition Figure 8.12 Creating the for statement. Empty for statement

19 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing the Interest Calculator Application (Cont.) C++ Standard Math Library – Necessary for the pow function –Performs exponentiation (there is no exponential operator in C++) Also includes functions for square roots and trigonometry etc. Figure 8.13 Including the standard library header file. Including

20 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing the Interest Calculator Application (Cont.) 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 Figure 8.14 Calculating amount on deposit after specified number of years. Using function pow to calculate the amount on deposit after the specified number of years

21 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing the Interest Calculator Application (Cont.) Figure 8.15 Displaying the amount on deposit for each year. Displaying the amount on deposit for each year

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing the Interest Calculator Application (Cont.) Figure 8.16 Completed application output.

23 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InterestCalculator.cpp (1 of 3) Include the standard library header file Define variables to store user input

24 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InterestCalculator.cpp (2 of 3) Prompt for and input the principal, rate and year Display the table header and format floating-point values

25 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InterestCalculator.cpp (3 of 3) Use a for statement to calculate the amount on deposit and display it in tabular format


Download ppt "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing."

Similar presentations


Ads by Google