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.) Figure 8.2 Completed Interest Calculator application output. Displaying the result in tabular format

5 © 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 1.Data type and name of the control variable ( int counter; )  Initial value of the control variable ( int counter = 2;) 2.The condition that tests for the final value of the control variable ( counter <= 5; ) 3.The increment (or decrement) by which the control variable is modified during each iteration of the loop ( counter++; )

6 © 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. 1. Names control variable and initializes its value to 2 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 3. Increment counter by 1 for each iteration of the loop Error: move left << setw(10) before months (see slide 18)

7 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.3Introducing the for Repetition Statement The for statement specifies all elements of counter-controlled repetition in the header Figure 8.4 Code segment for the Car Payment Calculator application that demonstrates the for statement.

8 © 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

9 © 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.)

10 © 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

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

12 © 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

13 © 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

14 © 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. Note: setw() function used correctly. Displaying a table header Using the fixed and setprecision stream manipulators to format text Figure 8.11 Formatting floating-point numbers. Formatting floating- point numbers

15 © 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

16 © 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

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.5Constructing the Interest Calculator Application (Cont.) The 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

18 © 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 Figure 8.16 Completed application output.

19 © 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

20 © 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

21 © 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

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 8 − Interest Calculator Application. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 8 Questions 8.1 to 8.10. Always write the question followed by the answer. Remember to highlight the answer. Exercises 8.11, 8.12, and the Programming Challenge 8.17. For Exercise 8.11 start with your completed Tutorial 8 − Interest Calculator Application. Due next Wednesday

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


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